LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to use variable value in sed command (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-use-variable-value-in-sed-command-4175491995/)

PravinNagare 01-21-2014 04:55 AM

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.

zhjim 01-21-2014 05:27 AM

Use double quotes (") instead of single quotes ('). Only within double quotes variables get evaluated. In single quotes they are taken literaly.

PravinNagare 01-21-2014 06:31 AM

Thanks a lot zhjim. It worked :)

zhjim 01-21-2014 07:06 AM

You're welcome. Please mark thread as solved. See top of the page under "Thread Tools". Thanks.

PravinNagare 01-24-2014 10:01 AM

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.

zhjim 02-03-2014 03:05 AM

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/....

PravinNagare 02-05-2014 07:08 AM

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.


All times are GMT -5. The time now is 11:27 AM.