Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sed 1

Status
Not open for further replies.

dduuhh

Programmer
Jan 5, 2003
48
NZ
Hi all

I am having difficulty using the sed command.
I want to replace backward slashes in a file with forward slashes.

I can replace "\a" with "/a" using sed 's/\\a/\/a/g' ${ORIG_FILE}.01 > ${ORIG_FILE}.02

However if I use the for loop command below it doesn't work:

for LETTER in a b c d e f g
do
sed "s/\\${LETTER}/\/${LETTER}/g" ${ORIG_FILE}.01 > ${ORIG_FILE}.02
mv ${ORIG_FILE}.02 ${ORIG_FILE}.01
done

Cheers and thanks
Dduuhh
 
How about:

[tt]for LETTER in a b c d e f g
do
sed '/\\'${LETTER}'/\/'${LETTER}'/g' ${ORIG_FILE}.01 > ${ORIG_FILE}.02
mv ${ORIG_FILE}.02 ${ORIG_FILE}.01
done[/tt]

There's no reason why you can't temporarily close the ' quotes and re-open them after each shell variable that you want replaced with a letter (provided the variable doesn't contain spaces, etc.).

Annihilannic.
 
Ok after spending 3 hrs "and the rest" trying to resolve this (for myself satisfaction), I have this:

test file is called ted.txt and contains:
\a \b \c \d \e \q \r \i
\a \b \c \d \e \q \r \i
\a \b \c \d \e \q \r \i
\a \b \c \d \e \q \r \i

Now I want to change the slashes around for a b c +d ...

so I do:
sed 's#\\\([a,b,c,d]\)#\/\1#g' ted.txt

And I get:
/a /b /c /d \e \q \r \i
/a /b /c /d \e \q \r \i
/a /b /c /d \e \q \r \i
/a /b /c /d \e \q \r \i

Ta Dar!

Ok to explain:
No need for for LETTER in .... just give the list in sed inside the SQUARE brackets comma seperated [a,b,c,d], the round brackets are escaped with a \( \) and this records the value of the letter found "not the leading slash though", still with me ? this "recorded" value is placed back in to the replace string with the "\1" and an escaped "\/" is subistuted .. I think ... bugger that got me confused !:¬)

Oh and I used # marks as delimitters for sed to save escaping another set of \'s

Try it ...
Good Luck.
Laurie.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top