Search and replace text in many files

Hello,
I am very new to Linux and am trying to find a way for following problem.
I have a number of files in a folder as Export000.dat, Export001.dat ..and so on.
Each file has a string field 'Absolute velocity'. I want it to be replaced by 'Pixel shift' in all the files. I tried something like $ sed 's/Absolute velocity$/Pixel shift/g' Export000.datBut it does not seem to work.
Please help !! :slight_smile:

CJ

Can you remove the dollar $ present in the pattern match and try. $ matches end of line. Something like below..

sed 's/Absolute velocity/Pixel shift/g' Export000.dat

Thanks !.it works..but how do I save the changed file under the same file name?..

If yours is a GNU SED then

sed -i 's/Absolute velocity/Pixel shift/g' Export000.dat

else you could send the output to a temp file and move the temp file to original file name.

1 Like

Alternatively, you could use a for loop and ed, instead of resorting to temp files or gnu extensions:

for f in Export???.dat; do
    printf %s\\n '1,$s/Absolute velocity/Pixel shift/g' w q | ed -s "$f"
done

Or, using a here document:

for f in Export???.dat; do
    ed -s "$f" <<'EOED'
        1,$s/Absolute velocity/Pixel shift/g
        w
        q
EOED
done

Regards,
Alister

2 Likes