[SOLVED] echo text to a specific line in an existing file
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.