Bash 使用sed剪切文件的一部分

Bash 使用sed剪切文件的一部分,bash,sed,Bash,Sed,cat/opt/inventory.txt ################################################################################### Begin_detail_of 5678 Request_No of the activity is 5678 testProject Requester of the project is xyz xyz@test.com End_of 5678 ###################

cat/opt/inventory.txt

###################################################################################
Begin_detail_of 5678
Request_No of the activity is 5678
testProject
Requester of the project is xyz
xyz@test.com
End_of 5678
####################################################################################
###################################################################################
Begin_detail_of 1234
Request_No of the activity is 1234
testProject
Requester of the project is xyz
xyz@test.com
End_of 1234
####################################################################################
当我使用下面的数字1234时,我得到了下面的预期输出

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
xyz@test.com
You have new mail in /var/spool/mail/root
但当我用变量输出替换数字时,结果并不像下面所期望的那样

[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]#

请帮助解决此问题。

您不需要2个sed命令,并确保在表达式中使用shell变量时使用双引号。您可以在相同的sed中执行此操作,如下所示:

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
xyz@test.com

您不需要2个sed命令,并确保在表达式中使用shell变量时使用双引号。您可以在相同的sed中执行此操作,如下所示:

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
xyz@test.com
使用awk:

使用awk:


您使用了错误的工具,sed不适用于跨多行的任何内容。只需使用标准的UNIX文本处理工具awk即可。以下是用于多字符RS的GNU awk:

$ 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
xyz@test.com
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
xyz@test.com
End_of 1234
如果要删除第一行和最后一行:

$ 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
xyz@test.com

$ 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
xyz@test.com

无论您想做什么,都是简单而琐碎的,因为这是awk的设计目的。

您使用了错误的工具,sed不适用于跨多行的任何内容。只需使用标准的UNIX文本处理工具awk即可。以下是用于多字符RS的GNU awk:

$ 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
xyz@test.com
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
xyz@test.com
End_of 1234
如果要删除第一行和最后一行:

$ 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
xyz@test.com

$ 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
xyz@test.com

无论你想做什么,都是简单而琐碎的,因为这是awk的设计目的。

可变有些单词以$开头,双引号将取代它的内容,而单引号中的一个则不会。这是壳牌的特点。因此,如果您想动态生成sed命令,只需使用单引号。

变量某些以$in双引号开头的单词将被替换为其内容,而单引号中的一个则不会。这是壳牌的特点。因此,如果您想动态生成sed命令,只需使用单引号。

使用变量时使用双引号而不是单引号通常是解决方案使用变量时使用双引号而不是单引号通常是解决方案非常感谢Anubhav-BaraniThank非常感谢Anubhav-BaraniThank非常感谢Ed-Barani欢迎。仅供参考除了复制结束条件的明显问题外,您接受的sed解决方案将以奇怪的方式失败,因为您的shell变量的内容多种多样。非常感谢Ed-BaraniYou,不客气。仅供参考除了复制结束条件的明显问题外,您接受的sed解决方案将以奇怪的方式失败,因为您的shell变量的内容多种多样。当然,我会遵循它。谢谢-BaraniSure Nano我会关注的。谢谢-巴拉尼