tr and sed remove spaces. how to stop this?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting tr and sed remove spaces. how to stop this?
# 1  
Old 02-10-2011
tr and sed remove spaces. how to stop this?

if the answer is obvious, sorry, I'm new here.

anyway, I'm using tr to encrypt with rot-13:
Code:
echo `cat $script | tr 'a-zA-Z' 'n-za-mN-ZA-M'` > $script

it works, but it removes any consecutive spaces so that there is just one space between words. I've had this problem before while using sed to delete lines of text in a file (it worked but removed consecutive spaces).

how to fix it?

Last edited by Franklin52; 02-10-2011 at 08:05 AM.. Reason: Please use code tags
# 2  
Old 02-10-2011
The problem is the "echo" which is losing end-of line characters and multiple spaces. It is possible to fix the echo with double quotes but you will still lose end-of-line characters.
Also I'd avoid writing back to the input file.

Code:
cp -p ${script} ${script}.sav
cat ${script}.sav | tr 'a-zA-Z' 'n-za-mN-ZA-M' > ${script}
# Uncomment when tested
# rm ${script}.sav

This User Gave Thanks to methyl For This Post:
# 3  
Old 02-10-2011
thanks, it works. how would I fix the same problem in sed though?

Code:
sed '/text/,/newtext/ d' filename > filename

ends up deleting everything in the file.

Code:
echo `sed '/text/,/newtext/ d' filename < filename` > filename

works but deletes the spaces again. What's the mistake I've made this time?
# 4  
Old 02-10-2011
Quote:
Originally Posted by Trichopterus
thanks, it works. how would I fix the same problem in sed though?

Code:
sed '/text/,/newtext/ d' filename > filename

ends up deleting everything in the file.

Code:
echo `sed '/text/,/newtext/ d' filename < filename` > filename

works but deletes the spaces again. What's the mistake I've made this time?
Code:
{ rm filename  ; sed '/text/,/newtext/ d'  > filename  ; } < filename

This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 02-10-2011
The "echo" issue is the same as in you previous problem.
Unless you have the sed version with the "-i" switch it is inadvisible to write back to the same file.

Code:
cp -p filename filename.sav
sed '/text/,/newtext/ d' filename.sav > filename
# Uncomment when tested
# rm filename.sav

This User Gave Thanks to methyl For This Post:
# 6  
Old 02-10-2011
Code:
 $ ruby -ne 'print $_.tr! "A-Za-z", "N-ZA-Mn-za-m"' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Can't remove spaces with sed when calling it from sh -c

The following command works echo "some text with spaces" | sh -c 'sed -e 's/t//g''But this doesn't and should echo "some text with spaces" | sh -c 'sed -e 's/ //g''Any ideas? (3 Replies)
Discussion started by: Tribe
3 Replies

2. UNIX for Dummies Questions & Answers

Using sed to remove spaces from middle of the line

Hi, I need to correct href portion of the lines to edit out spaces from the line starting with position "<a href=" and ending at "target=" Below are 2 examples of extra space added by the server: <td width=251 colspan=9 rowspan=22> <font size=2 face="courier"><tt><style>{font:7pt Courier ... (4 Replies)
Discussion started by: friedmi
4 Replies

3. Shell Programming and Scripting

remove spaces

Hi folks, I need to remove spaces at the end of each line in a *.txt file. it looks like this word 1 word 2 . . . word n i found some sed commands but any of them didnt work so far thank you for your posts (6 Replies)
Discussion started by: Jimmy7
6 Replies

4. Shell Programming and Scripting

How can I stop the unix script from trimming extra spaces?

I have a file which contains certain records about users. the row length is always fixed to 205 characters. Now I want to read each record line from the file, substring some portion out of it and put into another file. But I have observed that my script is trimming the extra spaces I have used for... (4 Replies)
Discussion started by: Pramit
4 Replies

5. Shell Programming and Scripting

sed remove newlines and spaces

Hi all, i am getting count from oracle 11g by spooling it to a file. Now there are some newline characters and blank spaces i need to remove these. pl provide me a awk/sed solution. the spooled file is attached. i tried this.. but not getting req o/p (6 Replies)
Discussion started by: rishav
6 Replies

6. Shell Programming and Scripting

How to remove spaces using awk,sed,perl?

Input: 3456 565 656 878 235 8 4 8787 3 7 35 878 Expected output: 3456 565 656 878 235 8 4 8787 3 7 35 878 How can i do this with awk,sed and perl? (10 Replies)
Discussion started by: cola
10 Replies

7. Shell Programming and Scripting

Remove Spaces

Hi All, I have a comma seperated file. I wanna remove the spaces from column 2. I mean i don't wanna remove the spaces those are presnt in column 1. ex: emp name, emp no, salary abc k, abc 11, 1000 00 bhk s, bhk 22, 2000 00 the output should be: emp name, emp no, salary abc k, abc11,... (4 Replies)
Discussion started by: javeed7
4 Replies

8. Shell Programming and Scripting

sed - remove spaces before 1rst occurence of string

seems easy but havent found in other posts... i want to delete any spaces if found before first occurence of ${AI_RUN} sed 's/ *\\$\\{AI_RUN\\}/\\$\\{AI_RUN\\}/' $HOME/temp1.dat i think i'm close but can't put my finger on it. :rolleyes: (6 Replies)
Discussion started by: danmauer
6 Replies

9. Shell Programming and Scripting

sed over writes my original file (using sed to remove leading spaces)

Hello and thx for reading this I'm using sed to remove only the leading spaces in a file bash-280R# cat foofile some text some text some text some text some text bash-280R# bash-280R# sed 's/^ *//' foofile > foofile.use bash-280R# cat foofile.use some text some text some text... (6 Replies)
Discussion started by: laser
6 Replies

10. Shell Programming and Scripting

how to remove spaces in a string using sed.

Hello, I have the following to remove spaces from beginning and end of a string. infile=`echo "$infilename" | sed 's/^ *//;s/ *$//` How do I modify the above code to remove spaces from beginning, end and in the middle of the string also. ex: ... (4 Replies)
Discussion started by: radhika
4 Replies
Login or Register to Ask a Question