Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用sed删除所有块中的特定行值_Sed - Fatal编程技术网

使用sed删除所有块中的特定行值

使用sed删除所有块中的特定行值,sed,Sed,仅删除每个块中的第四和第五个值(第1块中的第四和第五个值,第2块中的第四和第五个值,第3块中的第四和第五个值) 尝试了这个sed-E-E:a-E的/(.*)(.*)/\1\4\5/;ta'并且不工作 {content-start} first1 second1 third1 fourth1 fifth1 sixth1 {content-end} {content-start} first2 second2 third2 fourth2 fifth2 sixth2 {content-end} {c

仅删除每个块中的第四和第五个值(第1块中的第四和第五个值,第2块中的第四和第五个值,第3块中的第四和第五个值)

尝试了这个
sed-E-E:a-E的/(.*)(.*)/\1\4\5/;ta'
并且不工作

{content-start}
first1
second1
third1
fourth1
fifth1
sixth1
{content-end}
{content-start}
first2
second2
third2
fourth2
fifth2
sixth2
{content-end}
{content-start}
first3
second3
third3
fourth3
fifth3
sixth3
{content-end}

说真的,不要试图在单独的行中使用sed,而不仅仅是s/old/new。对于其他任何事情,awk解决方案总是比用于相同任务的sed解决方案更清晰、更简单、更高效、更易于增强/维护和更可移植的组合

这只是建立在:


下面的
awk
在这里也会有所帮助

awk '
/^{content-start}/{
    flag=1;
    print;
    next}
flag{
    count++}
flag && ((count==4) || (count==5)){
    next}
flag;
/^{content-end}/{
    flag=count=""}
'  Input_file

为精神锻炼而进行的Sed

sed -E 'N;N;N;N;N;N;N;s/(([^\n]*\n){4})([^\n]*\n){2}(.*)/\1\4/' infile
这可能适用于您(GNU-sed):


通过设置选项
-n
关闭自动打印。关注
开始
结束
分隔符之间的行。如果当前行是分隔符,则将计数器重置为空。打印除第五行和第六行之外的所有行,不要忘记累加计数器。打印所有其他行。

20个问题,只接受4个答案?停止使用“sed”要求-对于任何涉及多行块的工作来说,它是完全错误的工具,这就是为什么你必须为要求中的每一个微小变化不断提出新问题的原因。你应该使用awk——回头看看你已经有的awk答案,把它们用在那些任务上,然后做一些明显的、琐碎的调整,把它们用在这个和未来的任务上。人们在sed中使用的不仅仅是s、g和p(带-n),而仅仅是为了进行脑力锻炼——在20世纪80年代中期发明awk时,每一个sed结构都被淘汰了;6~8d'文件
awk '
/^{content-start}/{
    flag=1;
    print;
    next}
flag{
    count++}
flag && ((count==4) || (count==5)){
    next}
flag;
/^{content-end}/{
    flag=count=""}
'  Input_file
sed -E 'N;N;N;N;N;N;N;s/(([^\n]*\n){4})([^\n]*\n){2}(.*)/\1\4/' infile
sed -n '/start/,/end/{/start/{x;s/.*//;x};x;/^x\{4,5\}$/!{x;p;x};s/^/x/;x;b};p' file