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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
|
07-23-2013, 07:01 AM
|
#1
|
LQ Newbie
Registered: Apr 2010
Posts: 8
Rep:
|
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
|
|
|
07-23-2013, 07:08 AM
|
#2
|
Senior Member
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683
|
Code:
sed '/; Nodes/ a '"$variable"'' file.txt
|
|
|
07-23-2013, 07:11 AM
|
#3
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
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..... 
|
|
|
07-23-2013, 07:25 AM
|
#4
|
Senior Member
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683
|
Quote:
Originally Posted by druuna
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
|
|
|
07-23-2013, 07:26 AM
|
#5
|
LQ Newbie
Registered: Apr 2010
Posts: 8
Original Poster
Rep:
|
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
;
|
|
|
07-23-2013, 07:28 AM
|
#6
|
LQ Newbie
Registered: Apr 2010
Posts: 8
Original Poster
Rep:
|
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.
|
|
|
07-23-2013, 07:30 AM
|
#7
|
Senior Member
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683
|
@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?
|
|
|
07-23-2013, 07:33 AM
|
#8
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
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
;
|
|
|
07-23-2013, 07:41 AM
|
#9
|
LQ Newbie
Registered: Apr 2010
Posts: 8
Original Poster
Rep:
|
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
|
|
|
07-23-2013, 07:44 AM
|
#10
|
LQ Newbie
Registered: Apr 2010
Posts: 8
Original Poster
Rep:
|
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#
|
|
|
07-23-2013, 07:45 AM
|
#11
|
Senior Member
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683
|
Well, I think Druuna spotted it, see previous post.
Last edited by Firerat; 07-23-2013 at 07:47 AM.
Reason: new info
|
|
|
07-23-2013, 07:50 AM
|
#12
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
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 Its been a while since I used Solaris, it could be that sed is located in /opt/sfw/bin/ instead (called gsed??).
|
|
|
07-23-2013, 07:52 AM
|
#13
|
LQ Newbie
Registered: Apr 2010
Posts: 8
Original Poster
Rep:
|
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 ??
|
|
|
07-23-2013, 07:53 AM
|
#14
|
LQ Newbie
Registered: Apr 2010
Posts: 8
Original Poster
Rep:
|
Also any other way of doing this coding like using awk , or combination other cmd's ??
|
|
|
07-23-2013, 07:56 AM
|
#15
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
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 anon237; 07-23-2013 at 08:45 AM.
Reason: Ooops. Misread the output.....
|
|
|
All times are GMT -5. The time now is 04:51 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|