LinuxQuestions.org
Review your favorite Linux distribution.
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 07-15-2010, 09:44 AM   #1
say_hi_ravi
Member
 
Registered: Jan 2008
Posts: 75

Rep: Reputation: 15
Insert a line in the test file with sed


Hi,

I have following file workers file: There are 800 entries like this.

# consensus/uat
worker.consensus-uat1.host=lonlx10409
worker.consensus-uat1.port=13702
worker.consensus-uat1.type=ajp13
worker.consensus-uat1.lbfactor=1
worker.consensus-uat2.host=lonlx20444
worker.consensus-uat2.port=13702
worker.consensus-uat2.type=ajp13
worker.consensus-uat2.lbfactor=1
worker.lb-consensus-uat.type=lb
worker.lb-consensus-uat.balance_workers=consensus-uat1,consensus-uat2
worker.lb-consensus-uat.sticky_session=0
worker.list=lb-consensus-uat
# nsam/integration
worker.nsam-integration1.host=lonlx20903
worker.nsam-integration1.port=12002
worker.nsam-integration1.type=ajp13
worker.nsam-integration1.lbfactor=1
worker.nsam-integration2.host=lonlx10977
worker.nsam-integration2.port=12002
worker.nsam-integration2.type=ajp13
worker.nsam-integration2.lbfactor=1
worker.lb-nsam-integration.type=lb
worker.lb-nsam-integration.balance_workers=nsam-integration1,nsam-integration2
worker.lb-nsam-integration.sticky_session=1
worker.list=lb-nsam-integration


I want to insert one line just below the line starting with "woker.list". I think sed can do it but I am not aware. Can anyone please help
 
Old 07-15-2010, 10:19 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,584

Rep: Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914
Have a look at the append option (/a) for sed
 
Old 07-15-2010, 10:24 AM   #3
ttessier
LQ Newbie
 
Registered: Oct 2009
Posts: 4

Rep: Reputation: 0
Try this - Please watch carefully

You can try the following:

cat consensus/uat |sed -e "s/^\(worker.list.*\)/\1\\`echo -e '\n\r'`__NEW_CONTENT__/" > consensus/uat.sed

the cat command will read the lines and pipe it to sed, then sed will search for anything that starts with worker.list and also matches the rest of the line. The replacement is the matched line and an echo with \n\r and whatever the __NEW_CONTEN__ is to be. I have tried without the double character, but the newline does not appear to get injected. I have also tried inplace sed ie ( sed -i -e [command] [filename] without success.

Perform a dos2unix if you want to remove the extra ^M chracters.
Also: the new file is consensus.sed so that you can inspect it and update only if it works the way you want it to.

I read if you split the command on multiple lines it might get the new line in also.

Something like:
cat consensus/uat |sed -e "s/^\(worker.list.*\)/\1
__NEW_CONTENT__/" > consensus/uat.sed

Good Luck
 
Old 07-15-2010, 10:27 AM   #4
radu.pop
LQ Newbie
 
Registered: Nov 2008
Posts: 12

Rep: Reputation: 0
Hello,

Assuming your file name is 'nsam/integration', you may do:

Code:
cat nsam/integration |sed '/worker\.list.*/a\appended_string'>new_file.txt
if you feel confident enough, you can replace 'new_file.txt' with the same input file, in the example above, 'nsam/integration', though not recommended. Better redirect output to a new file, run a diff between the original and the new file and if you're satisfied with the results, just overwrite.

Good luck.
 
Old 07-15-2010, 10:39 AM   #5
sanjayadash
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Rep: Reputation: 1
Hi

Try this.

sed '/^worker.list/G' YourFileName
 
Old 07-15-2010, 10:40 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,584

Rep: Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914
Well it seems everyone else has done the job for you about looking up how to append (remember people, we are trying to help people to learn and not just do the job for them, anyhoo), so back to the job at hand.

OP please do not fall into the same bad habit as other users and placing a useless 'cat' into things like sed, awk, grep, etc. All these programs
are designed to parse files.
radu.pop is on the right track but cat and redirection are not required at all, although I do agree with keeping a backup when first trying a script.
So this should do (using radu.pop's filename as an example):
Code:
sed -i.bak '/^worker\.list.*/a\appended_string' nsam/integration
This will append the necessary text and also create a backup file called - nsam/integration.bak

Last edited by grail; 07-15-2010 at 10:43 AM.
 
1 members found this post helpful.
Old 07-15-2010, 10:45 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,584

Rep: Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914
Quote:
Originally Posted by sanjayadash
sed '/^worker.list/G' YourFileName
Maybe you could explain how this would work?? (because I see a few issues, one being that you will also match - workerslist)
 
Old 07-15-2010, 11:07 AM   #8
say_hi_ravi
Member
 
Registered: Jan 2008
Posts: 75

Original Poster
Rep: Reputation: 15
Thanks you All

And "grail": yes I will surely keep in mind your advice :-)
 
Old 07-15-2010, 11:16 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,584

Rep: Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914
Remember to mark as SOLVED once you have an answer
 
Old 07-16-2010, 02:28 AM   #10
radu.pop
LQ Newbie
 
Registered: Nov 2008
Posts: 12

Rep: Reputation: 0
Indeed, grail's solution is simplier and legitimate. I can't recall when I'd adquired this cat/pipe habit. Thanks for pointing that out

Cheers.
 
Old 07-16-2010, 02:47 AM   #11
say_hi_ravi
Member
 
Registered: Jan 2008
Posts: 75

Original Poster
Rep: Reputation: 15
Sorry for bringing this topic again. But I am facing one issue

I tried sed to insert individual "worker.list=lb-nsam-integration" in that file which worked absolutely fine

the command I used was

Quote:
$sed -i -e '/=lb-nsam-integration/a \
worker.lb-nsam-integration.reference=worker.basic' workers.properties.integration
this clearly puts the line below "worker.list=lb-nsam-integration" like below

Quote:
worker.list=lb-nsam-integration
worker.lb-nsam-integration.reference=worker.basic
But when I tried to do it for bulk entries using for loop it does not work :-(

Example : I have file

Quote:
$cat workerlist
lb-consensus-uat
lb-nsam-integration
and now when I try following command

Quote:
$for i in `cat workerslist.orig`; do sed -i -e '/worker.list=$i/a \
worker.$i.reference=worker.basic' workers.properties.integration;done
it does not work. can anyone please help!
 
Old 07-16-2010, 03:02 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,584

Rep: Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914
So this is a separate issue. What you have now is the difference between full and partial quoting.

'' = full quoting which removes any special properties of certain characters, ie $i is exactly that a dollar sign and the letter i

"" = partial quoting allows you to keep a line together, ie whitespace is not separated by the shell, but special characters retain there abilities, $i will now expand

I will leave it there and see if you can make the changes.
 
Old 07-16-2010, 03:36 AM   #13
say_hi_ravi
Member
 
Registered: Jan 2008
Posts: 75

Original Poster
Rep: Reputation: 15
Thanks a lot grail! Below worked as a charm!

Quote:
for i in `cat workerslist`; do sed -i -e "/worker.list=$i/ a\worker.$i.reference=worker.basic" workers.properties.integration;done
 
Old 07-16-2010, 04:02 AM   #14
sanjayadash
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Rep: Reputation: 1
Hi Grail,

You are right. I missed "\" to escape "." in worker.list

Here is a sample output of this command.
# cat def# consensus/uat
worker.consensus-uat1.host=lonlx10409
worker.consensus-uat1.port=13702
worker.consensus-uat1.type=ajp13
worker.consensus-uat1.lbfactor=1
worker.consensus-uat2.host=lonlx20444
worker.consensus-uat2.port=13702
worker.consensus-uat2.type=ajp13
worker.consensus-uat2.lbfactor=1
worker.lb-consensus-uat.type=lb
worker.lb-consensus-uat.balance_workers=consensus-uat1,consensus-uat2
worker.lb-consensus-uat.sticky_session=0
worker.list=lb-consensus-uat
# nsam/integration
worker.nsam-integration1.host=lonlx20903
worker.nsam-integration1.port=12002
worker.nsam-integration1.type=ajp13
worker.nsam-integration1.lbfactor=1
worker.nsam-integration2.host=lonlx10977
worker.nsam-integration2.port=12002
worker.nsam-integration2.type=ajp13
worker.nsam-integration2.lbfactor=1
worker.lb-nsam-integration.type=lb
worker.lb-nsam-integration.balance_workers=nsam-integration1,nsam-integration2
worker.lb-nsam-integration.sticky_session=1
worker.list=lb-nsam-integration
workerslist=lb-nsam-integration
worker-list=lb-nsam-integration
# sed "/worker\.list/G" def# consensus/uat
worker.consensus-uat1.host=lonlx10409
worker.consensus-uat1.port=13702
worker.consensus-uat1.type=ajp13
worker.consensus-uat1.lbfactor=1
worker.consensus-uat2.host=lonlx20444
worker.consensus-uat2.port=13702
worker.consensus-uat2.type=ajp13
worker.consensus-uat2.lbfactor=1
worker.lb-consensus-uat.type=lb
worker.lb-consensus-uat.balance_workers=consensus-uat1,consensus-uat2
worker.lb-consensus-uat.sticky_session=0
worker.list=lb-consensus-uat

# nsam/integration
worker.nsam-integration1.host=lonlx20903
worker.nsam-integration1.port=12002
worker.nsam-integration1.type=ajp13
worker.nsam-integration1.lbfactor=1
worker.nsam-integration2.host=lonlx10977
worker.nsam-integration2.port=12002
worker.nsam-integration2.type=ajp13
worker.nsam-integration2.lbfactor=1
worker.lb-nsam-integration.type=lb
worker.lb-nsam-integration.balance_workers=nsam-integration1,nsam-integration2
worker.lb-nsam-integration.sticky_session=1
worker.list=lb-nsam-integration

workerslist=lb-nsam-integration
worker-list=lb-nsam-integration


Hope this helps.
 
Old 07-16-2010, 06:04 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,584

Rep: Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914
@sanjayadash - well not what I would call an explanation (more of a demonstration), but after some searching I found:

Quote:
If you want to append to the pattern space, use the "G" command. This adds a new line to the pattern space, and copies the hold space after the new line.
So, nice when you want to add a single new line ... cool thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Insert line on match only once with sed? lhouk Programming 38 08-10-2010 10:23 AM
How do I insert a line/value after a particular line, in file with sed Glenn D. Programming 3 01-21-2010 10:14 PM
sed insert line action_owl Linux - Newbie 3 08-24-2009 01:06 AM
using sed to insert line into file and overwrite the current file jadeddog Programming 3 06-11-2009 08:14 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM

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

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

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration