LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 07: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, 07:08 AM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

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

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 07:25 AM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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, 07: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, 07: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, 07:30 AM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
@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, 07:33 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 07: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, 07: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, 07:45 AM   #11
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

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

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

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 07: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, 07: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, 07:56 AM   #15
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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 08:45 AM. Reason: Ooops. Misread the output.....
 
  


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
[SOLVED] Replace multi line pattern by text variable in sed XXLRay Linux - Software 2 11-22-2012 10:05 AM
Help with sed regex to match words via a pattern. logar0 Linux - Newbie 3 10-24-2010 04: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 04:18 AM
simple pattern match with awk, sed alenD Linux - Newbie 10 03-10-2008 02:31 PM
sed script to append variable text gmartin Linux - General 4 12-27-2006 04:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:14 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
Open Source Consulting | Domain Registration