LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-10-2011, 11:16 AM   #1
Jammyzx
LQ Newbie
 
Registered: Oct 2011
Posts: 2

Rep: Reputation: Disabled
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.
 
Old 10-10-2011, 12:01 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 10-10-2011, 12:18 PM   #3
Jammyzx
LQ Newbie
 
Registered: Oct 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks colucix that is really helpful works perfectly and much better solution than I was trying.
 
Old 10-10-2011, 12:39 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
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.
 
1 members found this post helpful.
Old 10-11-2011, 04:38 AM   #5
rikxik
Member
 
Registered: Dec 2007
Posts: 88

Rep: Reputation: 19
Quote:
Originally Posted by crts View Post
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.
 
Old 10-11-2011, 04:54 AM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by rikxik View Post
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.
 
Old 10-12-2011, 12:17 AM   #7
rikxik
Member
 
Registered: Dec 2007
Posts: 88

Rep: Reputation: 19
Quote:
Originally Posted by crts View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SED - replace text in file on specific line 3saul Linux - Software 1 03-04-2006 07:01 PM
vb.net writing to a specific line in a text file mrobertson Programming 3 12-30-2005 10:24 PM
Writing to a specific line in a text file mrobertson Programming 6 12-30-2005 04:02 PM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 04:32 PM
Shell script - how to show a specific line of a text file davi_cabral Linux - Software 3 09-28-2004 01:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration