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 01-21-2014, 04:55 AM   #1
PravinNagare
LQ Newbie
 
Registered: Jan 2014
Posts: 4

Rep: Reputation: Disabled
How to use variable value in sed command


Hi All,

I want to delete a node from a file say node.txt

CREATE NODE abc
OP-SYSTEM UNIX
SYS-NAME abc
LOCAL-REPOSITORY /usr/adm/best1_default/collect
TRANSFER AUTOMATIC

CREATE NODE xyz
OP-SYSTEM UNIX
SYS-NAME xyz
LOCAL-REPOSITORY /usr/adm/best1_default/collect
TRANSFER AUTOMATIC

CREATE NODE pqr
OP-SYSTEM UNIX
SYS-NAME pqr
LOCAL-REPOSITORY /usr/adm/best1_default/collect
TRANSFER AUTOMATIC

Following sed command will delete node xyz

bash$ sed '/CREATE NODE xyz/,/TRANSFER AUTOMATIC/d' node.txt

o/p:
CREATE NODE abc
OP-SYSTEM UNIX
SYS-NAME abc
LOCAL-REPOSITORY /usr/adm/best1_default/collect
TRANSFER AUTOMATIC

CREATE NODE pqr
OP-SYSTEM UNIX
SYS-NAME pqr
LOCAL-REPOSITORY /usr/adm/best1_default/collect
TRANSFER AUTOMATIC

But when I take node name in variable and use variable in sed command, its not working.

bash$ node=xyx
bash$ sed '/CREATE NODE $node/,/TRANSFER AUTOMATIC/d' node.txt

I would really appreciate your help.
 
Old 01-21-2014, 05:27 AM   #2
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Use double quotes (") instead of single quotes ('). Only within double quotes variables get evaluated. In single quotes they are taken literaly.
 
1 members found this post helpful.
Old 01-21-2014, 06:31 AM   #3
PravinNagare
LQ Newbie
 
Registered: Jan 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks a lot zhjim. It worked
 
Old 01-21-2014, 07:06 AM   #4
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
You're welcome. Please mark thread as solved. See top of the page under "Thread Tools". Thanks.
 
Old 01-24-2014, 10:01 AM   #5
PravinNagare
LQ Newbie
 
Registered: Jan 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
I have 2 more problems with this script and want help to resolve them

Number 1

The files I am working on contain the nodes in below format, However there are some nodes where I see only one space between CREATE NODE and server name (e.g. abc) while as some have multiple spaces. I would like to know how i can overcome this problem.

CREATE NODE xyz
OP-SYSTEM UNIX
SYS-NAME xyz
LOCAL-REPOSITORY /usr/adm/best1_default/collect
TRANSFER AUTOMATIC

Command used: bash$ sed "/CREATE NODE $node/,/TRANSFER AUTOMATIC/d" node.txt ( it will delete the above node (xyz) because it has only one space) but it wont delete if node is configued in below format:

CREATE NODE xyz
OP-SYSTEM UNIX
SYS-NAME xyz
LOCAL-REPOSITORY /usr/adm/best1_default/collect
TRANSFER AUTOMATIC

I want this command to delete the node irrespective of the no of spaces between CREATE NODE and Node Name (xyz)

Number 2

Also when i delete multiple servers from a single file it leaves 2 blank lines after every node name i would like to leave only one blank line after every node name

Current State

CREATE NODE abc
OP-SYSTEM UNIX
SYS-NAME abc
LOCAL-REPOSITORY /usr/adm/best1_default/collect
TRANSFER AUTOMATIC

CREATE NODE xyz
OP-SYSTEM UNIX
SYS-NAME xyz
LOCAL-REPOSITORY /usr/adm/best1_default/collect
TRANSFER AUTOMATIC

CREATE NODE pqr
OP-SYSTEM UNIX
SYS-NAME pqr
LOCAL-REPOSITORY /usr/adm/best1_default/collect
TRANSFER AUTOMATIC

Suppose i want to delete node name "xyz" it would leave 2 blank lines(line above node and line below node), I would like to leave only one blank line. Please suggest can it be done using the same command.
 
Old 02-03-2014, 03:05 AM   #6
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Lets start with problem 2. Think about what you delete. You leave the empty line after "TRANSFER AUTOMATIC". Either use something like
Code:
sed "/CREATE NODE $node/,/\n/d" node.txt
Also not sure if this will work. Or after deleting use tr -s '\n' when done. You might also search the net for "sed removing double empty lines".

Regarding your first problem you have to adjust your regex.
Code:
/CREATE NODE[[:space:]]*$node/....
 
1 members found this post helpful.
Old 02-05-2014, 07:08 AM   #7
PravinNagare
LQ Newbie
 
Registered: Jan 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks for response zhjim. sed '/^$/N;/^\n$/D' will remove double lines. Just for curiosity, I tried to delete all the lines in between CREATE NOTE $node and next blank line (including blank line). I came up with following sed command:

/usr/bin/sed "/CREATE NODE *$node_name/,/""/d" node.txt

But don't know why it is deleting everything from CREATE NODE $node_name to the end of the file. I would really appreciate your help here.
 
  


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 command to change variable names globally shridhar22 Linux - Newbie 21 02-06-2013 05:28 PM
[SOLVED] sed command using variable nwalsh88 Linux - Newbie 2 12-06-2012 01:53 PM
how to use variable in sed command ? anandg111 Linux - Newbie 3 01-11-2012 11:27 AM
sed substitution of variable with a variable ngyz86 Linux - Newbie 6 01-05-2011 07:44 AM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM

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

All times are GMT -5. The time now is 09:57 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