Pages

lundi 18 juin 2012

sed to replace a string in a file and save it

#!/bin/bash

for i in $(ls -d p*); do
    echo $i
    cd $i
    echo $PWD
    grep "elseif (( hrut .gt. 29.2)" *
    sed -i 's/(( hrut .gt. 29.2) then/( hrut .gt. 29.2) then/g'  file.f;   
   cd ..

done

sed -i  's/string1/string2/g'  file:
it can replace the string1 with string 2 in the file, and save the modified file with the original name.


____ to replace strings at certain lines:
set a = 50
set b = 60
sed -e "$a,${b}s/string1/string2/g"  file

This is to replace the string1 with string2 at lines between 50 and 60 in a file

Aucun commentaire:

Enregistrer un commentaire