Reputation:
cat /opt/inventory.txt
###################################################################################
Begin_detail_of 5678
Request_No of the activity is 5678
testProject
Requester of the project is xyz
[email protected]
End_of 5678
####################################################################################
###################################################################################
Begin_detail_of 1234
Request_No of the activity is 1234
testProject
Requester of the project is xyz
[email protected]
End_of 1234
####################################################################################
When I use the number 1234 as below I get expected output as below
cat /opt/inventory.txt | sed -n -e '/Begin_detail_of\ 5678/,$p' | sed -e '/End_of\ 5678/,$d'
Begin_detail_of 5678
Request_No of the activity is 5678
testProject
Requester of the project is xyz
[email protected]
You have new mail in /var/spool/mail/root
But when I substitute the number with variable output is not as expected as below
[root@centoo script]# export num=5678
[root@centoo script]# echo $num
5678
[root@centoo script]# cat /opt/inventory.txt | sed -n -e '/Begin_detail_of\ $num/,$p' | sed -e '/End_of\ $num/,$d'
[root@centoo script]#
Please help to solve the issue.
Upvotes: 1
Views: 587
Reputation: 818
Variable(some word starts with a $) in double quotes will be sustituted to it's content while one in single quotes will not. It's the feature of shell. So if you want to generate sed commands dynamically, just use single quotes.
Upvotes: 0
Reputation: 203284
You're using the wrong tool, sed is not for anything that spans multiple lines. Just use the standard UNIX text processing tool, awk. Here's GNU awk for multi-char RS:
$ gawk -v tgt='5678' -v RS='#+\n' -v ORS= '$0~"Begin_detail_of "tgt"\n"' file
Begin_detail_of 5678
Request_No of the activity is 5678
testProject
Requester of the project is xyz
[email protected]
End_of 5678
$ gawk -v tgt='1234' -v RS='#+\n' -v ORS= '$0~"Begin_detail_of "tgt"\n"' file
Begin_detail_of 1234
Request_No of the activity is 1234
testProject
Requester of the project is xyz
[email protected]
End_of 1234
and if you want to remove the first and last lines:
$ gawk -v tgt='5678' -v RS='End_of[^\n]+\n#+\n' -v ORS= 'sub("#+\nBegin_detail_of "tgt"\n","")' file
Request_No of the activity is 5678
testProject
Requester of the project is xyz
[email protected]
$ gawk -v tgt='1234' -v RS='End_of[^\n]+\n#+\n' -v ORS= 'sub("#+\nBegin_detail_of "tgt"\n","")' file
Request_No of the activity is 1234
testProject
Requester of the project is xyz
[email protected]
Whatever you want to do is brief and trivial since this is what awk is designed to do.
Upvotes: 1
Reputation: 20980
Using awk
:
awk -v RS="###################################################################################" /"End_of $num"/ file
Upvotes: 0
Reputation: 785038
You don't need 2 sed commands and make sure to use double quotes while using a shell variable in expression. You can do this in same sed
like this:
num=5678
sed -n "/Begin_detail_of $num/,/End_of $num/{/End_of $num/d;p;}" file
Begin_detail_of 5678
Request_No of the activity is 5678
testProject
Requester of the project is xyz
[email protected]
Upvotes: 3