LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-23-2013, 08:01 AM   #1
ravikushal
LQ Newbie
 
Registered: Apr 2010
Posts: 8

Rep: Reputation: 0
how can i append the text after pattern match using variable in sed


Hi,
I am new in sed scripting.I am trying to use sed in my bash script.Problem is below .
My file formate for file.txt is

hello linux
; Nodes
this is my first node
this is second
this is third one
this is fourth
;

I want to add some text after pattern " ; Nodes " .
The text i am going to add , that text i am reading from the user and storing in VARIABLE.
So how i can use this variable and append or insert the variable value after the " ; Nodes " pattern match .

I tried doing below but not working.

#!/bin/bash
variable=this is my top node
sed '/; Nodes/
n
a\$variable
}' file.txt

could anyone please help here ??
Thanks in advance. >> RaviKushal
 
Old 07-23-2013, 08:08 AM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Code:
sed '/; Nodes/ a '"$variable"'' file.txt
 
Old 07-23-2013, 08:11 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Here is one way:
Code:
sed "s/^; Nodes/; Nodes\n$variable/" file.txt
Or using your script/sed solution:
Code:
#!/bin/bash

variable="this is my top node"

sed "/^; Nodes/a\
$variable" file.txt
I'm too slow.....
 
Old 07-23-2013, 08:25 AM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Quote:
Originally Posted by druuna View Post
I'm too slow.....
BUT Yours looks better than mine


Anyway, to insert before

Code:
sed '/; Nodes/ i '"$variable"'' file.txt

Actually,. just noticed something
I'm sure I've had to use 'newlines' in the past..
but don't seem to need them with sed (GNU sed) 4.2.2
 
Old 07-23-2013, 08:26 AM   #5
ravikushal
LQ Newbie
 
Registered: Apr 2010
Posts: 8

Original Poster
Rep: Reputation: 0
HI druuna ,

i tried with ur first solution but its not changing anything in file.txt

logs

bash-3.2# ./scr.sh
hello linux
; Nodes
this is my first node
this is second
this is third one
this is fourth
;
bash-3.2# cat file.txt
hello linux
; Nodes
this is my first node
this is second
this is third one
this is fourth
;
 
Old 07-23-2013, 08:28 AM   #6
ravikushal
LQ Newbie
 
Registered: Apr 2010
Posts: 8

Original Poster
Rep: Reputation: 0
HI druuna ,

i tried with ur second solution and its throwing

bash-3.2# ./scr.sh
sed: command garbled: /^; Nodes/athis is my top node
bash-3.2#

-------------------------------


Hi Firerat ,

the same garbled error trown by script.
 
Old 07-23-2013, 08:30 AM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
@ravikushal,

they both seem to be working well here.

Notice you have bash-3.2 ?
That is quite old ( but shouldn't be a problem here ), what version of sed do you have?
 
Old 07-23-2013, 08:33 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Did you put the content of variable between quotes?

WRONG: variable=this is my top node
CORRECT: variable="this is my top node"

It works on my side:
Code:
$ variable="this is my top node"
$ echo $variable
this is my top node
$ sed "s/^; Nodes/; Nodes\n$variable/" file.txt
hello linux
; Nodes
this is my top node
this is my first node
this is second
this is third one
this is fourth
;
Code:
$ cat file.sh 
#!/bin/bash

variable="this is my top node"

sed "/^; Nodes/a\
$variable" file.txt

$ ./file.sh 
hello linux
; Nodes
this is my top node
this is my first node
this is second
this is third one
this is fourth
;
 
Old 07-23-2013, 08:41 AM   #9
ravikushal
LQ Newbie
 
Registered: Apr 2010
Posts: 8

Original Poster
Rep: Reputation: 0
i have a solaris machine , so My sed version i think

PKGINST: SUNWcsu
NAME: Core Solaris, (Usr)
CATEGORY: system
ARCH: i386
VERSION: 11.10.0,REV=2005.01.21.16.34
 
Old 07-23-2013, 08:44 AM   #10
ravikushal
LQ Newbie
 
Registered: Apr 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Hi druuna ,

Yes .

bash-3.2# cat scr.sh
#!/bin/bash

variable="this is my top node"

sed "/^; Nodes/a\
$variable" /SATY/file.txt
bash-3.2#
bash-3.2# ./scr.sh
sed: command garbled: /^; Nodes/athis is my top node
bash-3.2#
 
Old 07-23-2013, 08:45 AM   #11
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Well, I think Druuna spotted it, see previous post.

Last edited by Firerat; 07-23-2013 at 08:47 AM. Reason: new info
 
Old 07-23-2013, 08:50 AM   #12
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
You are using Solaris, which you should have mentioned in your first post.

I'm assuming you use the standard sed that comes with Solaris. But there are more versions available.

Try the one that resides in /usr/xpg4/bin

I.e.:
Code:
/usr/xpg4/bin/sed ..........
instead of
Code:
sed .......
Its been a while since I used Solaris, it could be that sed is located in /opt/sfw/bin/ instead (called gsed??).
 
Old 07-23-2013, 08:52 AM   #13
ravikushal
LQ Newbie
 
Registered: Apr 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Hello ,

i ran like below :

bash-3.2# variable="this is my top node"
bash-3.2# echo $variable
this is my top node
bash-3.2# sed "s/^; Nodes/; Nodes\n$variable/" file.txt
hello linux
; Nodes
this is my first node
this is second
this is third one
this is fourth
;
bash-3.2#


but still no change .
Its not working becuase the version which i have for bash and sed ??
 
Old 07-23-2013, 08:53 AM   #14
ravikushal
LQ Newbie
 
Registered: Apr 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Also any other way of doing this coding like using awk , or combination other cmd's ??
 
Old 07-23-2013, 08:56 AM   #15
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Edit: Misread the output. Sorry about that

If you need the original file to change then do the following:
Code:
sed "/^; Nodes/a\
$variable" file.txt > new.file.txt

mv new.file.txt file.txt
If you have a modern sed (version 4+) you can use the -i flag:
Code:
sed -i"/^; Nodes/a\
$variable" file.txt

Last edited by druuna; 07-23-2013 at 09:45 AM. Reason: Ooops. Misread the output.....
 
  


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
[SOLVED] Replace multi line pattern by text variable in sed XXLRay Linux - Software 2 11-22-2012 11:05 AM
Help with sed regex to match words via a pattern. logar0 Linux - Newbie 3 10-24-2010 05:33 PM
How to use sed to delete all lines before the first match of a pattern? C_Blade Linux - Newbie 9 05-01-2010 05:18 AM
simple pattern match with awk, sed alenD Linux - Newbie 10 03-10-2008 03:31 PM
sed script to append variable text gmartin Linux - General 4 12-27-2006 05:44 PM


All times are GMT -5. The time now is 10:39 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration