LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   echo text to a specific line in an existing file (https://www.linuxquestions.org/questions/programming-9/echo-text-to-a-specific-line-in-an-existing-file-907412/)

Jammyzx 10-10-2011 11:16 AM

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.

colucix 10-10-2011 12:01 PM

You might simply do:
Code:

sed -i '/MAXD 0.5/a SEIG' file
the a (add) command of sed adds the specified text immediately below the line containing the pattern. The -i option edits the file in place so that you can avoid temporary copies.

Jammyzx 10-10-2011 12:18 PM

Thanks colucix that is really helpful works perfectly and much better solution than I was trying.

crts 10-10-2011 12:39 PM

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
...

If the example is all you are doing in that loop then you could do this all with one command as provided by colucix. In this case you do not need the loop. Simply do:
Code:

sed -i '/MAXD 0.5/a SEIG' *.dmain
Again, only if there is nothing more happening inside your loop.

rikxik 10-11-2011 04:38 AM

Quote:

Originally Posted by crts (Post 4494742)
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
...


Except that if your directory is empty, using *.dmain will have interesting effects. As long as you check for that, this is ok.

crts 10-11-2011 04:54 AM

Quote:

Originally Posted by rikxik (Post 4495369)
Except that if your directory is empty, using *.dmain will have interesting effects. As long as you check for that, this is ok.

Can you elaborate on that? What do you consider interesting? The error message that you get? I do not think that this is interesting. You can redirect stderr to /dev/null and check for the exit status of sed (of course only if the sed command itself is correct) to determine the course of action.

rikxik 10-12-2011 12:17 AM

Quote:

Originally Posted by crts (Post 4495379)
Can you elaborate on that? What do you consider interesting? The error message that you get? I do not think that this is interesting. You can redirect stderr to /dev/null and check for the exit status of sed (of course only if the sed command itself is correct) to determine the course of action.

Well, I was replying from the point of view that using "for i in *" is the perfect solution - it is not in the case I described. You are correct that we can always do further tests and check etc.


All times are GMT -5. The time now is 10:34 PM.