echo text to a specific line in an existing file
Hi,
I am fairly new to scripting and am using bash; basically I have an existing file which is the output of one program which I wish to write a script to edit and use as input for another program. The files length will change each time. For example AABB BBCC CCDD DDEE EEFF MAXD 0.5 FFGG GGHH I would want to insert the phrase SEIG in between the lines MAXD 0.5 and FFGG I have so far written as follows which adds a blank line where I want to add text the new text and produces the line number of this blank line. #!/bin/bash for i in `ls *.dmain` do sed '/MAXD 0.5/G' $i >tmp3 # add a blank line below maxd 0.5 line grep -n 'MAXD 0.5' $i > tmp1 # find the line number of maxd 0.5 sed 's/:MAXD 0.5//g' tmp1 > tmp2 #leave only the line number read j < tmp2 echo $j k=$((j+1)) echo $k done Any help would be great. Thanks in advance. |
You might simply do:
Code:
sed -i '/MAXD 0.5/a SEIG' file |
Thanks colucix that is really helpful works perfectly and much better solution than I was trying.
|
Hi,
read here why you should not parse the output of ls: http://mywiki.wooledge.org/BashPitfa...8ls_.2A.mp3.29 So, your for-loop should look more like this: Code:
for i in *.dmain; do Code:
sed -i '/MAXD 0.5/a SEIG' *.dmain |
Quote:
|
Quote:
|
Quote:
|
All times are GMT -5. The time now is 10:34 PM. |