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. |
Use double quotes (") instead of single quotes ('). Only within double quotes variables get evaluated. In single quotes they are taken literaly.
|
Thanks a lot zhjim. It worked :)
|
You're welcome. Please mark thread as solved. See top of the page under "Thread Tools". Thanks.
|
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. |
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 Regarding your first problem you have to adjust your regex. Code:
/CREATE NODE[[:space:]]*$node/.... |
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. |