Pages

vendredi 30 novembre 2012

insert text after one line which contains patterns

In a file I would like to insert a line after the line which contains "MRINI".

1) firstly, find the line number which contains the pattern
 set line_number =  `sed  -n  '/MRINI/='   filename `
-n : to only print the line number, not print the pattern space

2) use sed to insert a line after the line_number. The line to insert is:
    1 7840    0    0    0    0    0    0               fin correction mesures

The command is:   sed -e "${line_number}a \line_to_insert"   filename

sed   -e   "${line_number}a\    1 7840    0    0    0    0    0    0               fin correction mesures"       filename   > new_filename


Note: 
i) the double quote is used in 2) since the variable ${line_number} is used in the command.

ii) In the command sed -e "${line_number}a \line_to_insert"   filename ,
a\line_to_insert represents that we append the line_to_insert 
If use i\line_to_insert: insert the line_to_insert in the $line_number, ie., before the line which contains the pattern MRINI

iii) in 1) we use   '/MRINI/='  
= represents that we print the current line number where it contains the pattern MRINI




Aucun commentaire:

Enregistrer un commentaire