Monday, 15 April 2013

regex - Using grep to find specific line - pass that line to sed - edit character at position x -



regex - Using grep to find specific line - pass that line to sed - edit character at position x -

i need find lines begin 1 , replace position 14. need find lines begin 5 , replace @ position 41. - et.c.

i want replace occurences of string:

sed 's/string/replace/g' -

the string can change, , want replace first position - position same , occurs @ position 14, therefore:

sed 's/string/replace/14'

different lines not need character replaced. want replace character position if line begins code.

this string occurs @ place 14 on line begins 1 string occurs @ place 41 on line begins 5 string occurs @ place 45 on line begins 8

find string

grep -e '^1' $dest/$file_name$date.txt

therefore:

grep -e '^1' $dest/$file_name$date.txt | sed 's/./'$code'/14' grep -e '^5' $dest/$file_name$date.txt | sed 's/./'$code'/41' grep -e '^8' $dest/$file_name$date.txt | sed 's/./'$code'/45'

the above doesn't write file though, seems want.

grep -e '^8' $dest/$file_name$date.txt | sed -i 's/./'$code'/45' $dest/$file_name$date.txt

the above writes file, ignores grepped line, , replaces character in position 45 each line.

also, multiple similar files may concatenated - hence need find lines begin 1 , replace position 14. need find lines begin 5 , replace @ position 41. - et.c.

sorry ahead of time if answered post - using awk , regex. haven't found particular case though.

sed can create changes lines match regex, grep isn't necessary. matching look comes first, substitution expression:

sed -i "/^1/ s/./$code/14" $dest/$file_name$date.txt sed -i "/^5/ s/./$code/41" $dest/$file_name$date.txt sed -i "/^8/ s/./$code/45" $dest/$file_name$date.txt

selecting lines sed (addresses)

regex bash shell sed grep

No comments:

Post a Comment