bash - using sed to cut a part of file -
cat /opt/inventory.txt
################################################################################### begin_detail_of 5678 request_no of activity 5678 testproject requester of project xyz xyz@test.com end_of 5678 #################################################################################### ################################################################################### begin_detail_of 1234 request_no of activity 1234 testproject requester of project xyz xyz@test.com end_of 1234 ####################################################################################
when use number 1234 below expected output 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 activity 5678 testproject requester of project xyz xyz@test.com have new mail in /var/spool/mail/root
but when substitute number variable output not expected 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 solve issue.
you don't need 2 sed commands , make sure use double quotes while using shell variable in expression. can in same sed
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 activity 5678 testproject requester of project xyz xyz@test.com
Comments
Post a Comment