Operating System - HP-UX
1824994 Members
2164 Online
109678 Solutions
New Discussion юеВ

Editing a file using 'sed'

 
SOLVED
Go to solution
Paul Booth (IAD)
Occasional Advisor

Editing a file using 'sed'

I've got a text file which I need to make numerous changes to. The changes need to be made in the following sequence.

1. Remove all / from file
2. Remove all spaces that follow =
3. Remove all spaces that follow +
4. Remove all spaces that follow :
5. Reduce remaining multiple spaces on lines to 1 space
6. Remove blank lines from file
7. Change all occurances of \ to spaces

Hope you can help!
6 REPLIES 6
Steve Labar
Valued Contributor
Solution

Re: Editing a file using 'sed'

sed "s$/$$g" file > file.tmp;mv file.tmp file
sed "s/= /=/g" file > file.tmp;mv file.tmp file
sed "s/+ /+/g" file > file.tmp;mv file.tmp file
sed "s/: /:/g" file > file.tmp;mv file.tmp file
sed "s/\w+/\w/g" file > file.tmp;mv file.tmp file
sed "s/\\/ /g" file > file.tmp;mv file.tmp file

Good Luck.

Steve
James R. Ferguson
Acclaimed Contributor

Re: Editing a file using 'sed'

Hi Paul:

1. # sed -e 's%/%%'g
2. # sed -e 's%= %=%'g
3. # sed -e 's%= %+%'g
4. # sed -e 's%= %:%'g
5. # sed 's/ */ /g' #...that 2-spaces before the asterisk character.
6. # sed '/^$/d
7. # sed -e 's%\\% %'g

Note that doing step-5 first simplifies the remaining edits. In fact, depending on your objective, the point at which you do step-7 might differ too.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Editing a file using 'sed'

Hi (again) Paul:

Oops, I fat-fingered my own cut-and-paste for #3 and #4 (which is obvious if you look at my original post!):

3. # sed -e 's%+ %+%'g
4. # sed -e 's%: %:%'g

Regards!

...JRF...
Paul Booth (IAD)
Occasional Advisor

Re: Editing a file using 'sed'

Thanks to both of you!

Sridhar Bhaskarla
Honored Contributor

Re: Editing a file using 'sed'

Hi Paul,

This will delete all spaces and tabs followed by =,+ and : along with the others you mentioned. 'data' is your input file.

sed -e 's/\///g' \
-e 's/=[ ]*/=/g' \ #[]
-e 's/+[ ]*/+/g' \ #[]
-e 's/:[ ]*/:/g' \ #[]
-e 's/ */ /g' \ #/*/
-e '/^ *$/d' \ #/^*$/
-e 's/\\//g' data

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Rory R Hammond
Trusted Contributor

Re: Editing a file using 'sed'

sed -e 's/[=+:][ \t]*//g'
-e 's/\///g'
-e '/./!d'
-e 's/ */ /g'
-e 's/\\/ /g' test.txt > new.txt

There are a 100 ways to do things and 97 of them are right