I need to edit file , the main issue is to append text between two known lines in the file
for example I need to append the following text
a b c d e f
1 2 3 4 5 6
bla bla
Between the first_line and the second_line
first_line=")"
second_line="NIC Hr_Nic ("
remark: first_line and second_line argument can get any line or string
How to do this by perl ? ( i write bash script and I need to insert the perl syntax in my script)
lidia
You could read the file in as a single string and then use a regular expression to do the search and replace:
By unsetting
$/we put Perl into “slurp mode” and so can easily read the whole file into$file.We use the
s///operator to do a search and replace using our two search lines as a pattern.The
\Qand\Etell Perl to escape the strings between them, i.e. to ignore any special characters that happen to be in$first_lineor$second_line.You could always write the output over the original file if desired.
The problem as you state it is not solvable using the
-icommand line option since this option processes a file one line at a time; to insert text between two specific lines you’ll need to know about two lines at once.