Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

am using

!/bin/bash
     for fl in *.txt; do
     mv $fl $fl.old
     sed 's/files.php?file=/file/g' $fl.old > $fl
     rm -f $fl.old
     done

I need the output replaced with file/ not only file , couldnt figure it out

share|improve this question

4 Answers 4

up vote 0 down vote accepted

Use:

sed 's/files.php?file=/file\//g;' $fl.old > $fl

Notice the \ used to escape the / after file in the replacement ...

share|improve this answer

You can use # or | instead of / to make life easier when using slashes in patterns:

!/bin/bash
     for fl in *.txt; do
     mv $fl $fl.old
     sed 's#files.php?file=#file/#g' $fl.old > $fl
     rm -f $fl.old
     done

This keeps you from having to escape all the slashes you want to match or use as a replacement like \/

Also, just to be clear:

/ is a slash

\ is a backslash

share|improve this answer

You can use escaping of the backslash:

sed 's/files.php?file=/file\//g' $fl.old > $fl

And that will produce desired output.

share|improve this answer

You don't haver to use / with sed you can use something else like # or |

sed 's#files.php?file=#file/#g'

or

sed 's|files.php?file=|file/|g'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.