Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.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
Bash 删除两行(含两行)之间的数据_Bash_Shell_Unix - Fatal编程技术网

Bash 删除两行(含两行)之间的数据

Bash 删除两行(含两行)之间的数据,bash,shell,unix,Bash,Shell,Unix,搜索和删除两行文本(包括第一行但不包括第二行)之间的数据的最佳方式是什么 字符串1:部分-支付500-要删除 要删除的数据,随机文本行 字符串2:部分-支付400-住宿 这是大约3000页的word文档,但我也有一个文本版本。我从哪里开始为这样的任务编写bash脚本 文件内容的示例: text SECTION - PAY 500 (to be deleted) text (to be deleted) SECTION - Pay 400 text SEC

搜索和删除两行文本(包括第一行但不包括第二行)之间的数据的最佳方式是什么

字符串1:
部分-支付500
-要删除

要删除的数据,随机文本行

字符串2:
部分-支付400
-住宿

这是大约3000页的word文档,但我也有一个文本版本。我从哪里开始为这样的任务编写bash脚本

文件内容的示例:

text 
SECTION - PAY 500    (to be deleted)
text                 (to be deleted)
SECTION - Pay 400
text 
SECTION - PAY 500    (to be deleted)
text                 (to be deleted)
SECTION - Pay 400
text 
删除后,这应该是结果

text 
SECTION - Pay 400
text
SECTION - Pay 400
text

使用标准的
sed
解决方案:

sed "/$START/,/$END/ { /$END/"'!'" d; }"
这意味着,对于从
/$START/
开始到
/$END/
结束的范围,将执行
{/$END/!d;}
操作,这将对所有非
/$END/
的行执行
d
(删除)


“!”
很奇怪,但这是逃离
的唯一方法bash扩展中的符号

我认为您可以很快地逐行解析文件。你对阿契夫说的话似乎并不复杂到难以理解

copy=true
while read line; do
    if [ $copy ]; then
        if [[ "$line" == "SECTION - PAY 500"* ]]; then copy=; continue; fi
        echo "$line" >> outputfile
    else
        if [[ "$line" == "SECTION - Pay 400"* ]]; then copy=true; fi
    fi
done < inputfile
copy=true
读行时;做
如果[$copy];然后
如果[[“$line”==“部分-支付500”*];然后复制=;继续;fi
echo“$line”>>输出文件
其他的
如果[[“$line”==”部分-支付400”*];那么copy=true;fi
fi
完成<输入文件
通过这样做,我们现在甚至有了一个像图灵机器这样的东西

另一种(不那么奇怪;)标准sed解决方案:
sed”/$END/p;/$START/,/$END/d;“

旁注:某些
sed
版本还支持文件的就地编辑(如果需要)

还有一个成熟的bash脚本:

#/bin/bash
如果[“x$1”=“x-r”]
然后
正则表达式=1
转移
其他的
正则表达式=0
fi
如果[$#-lt 2]
然后
echo“用法:del.sh[-r]开始-结束”
出口1
fi
start=“$1”
完=“$2”
函数匹配
{
[[(regex-eq 1&&“$1”=~$2)| |(regex-eq 0&&“$1”==“$2”)]]
}
del=0
读行时
做
#必须打印结束标记
如果匹配“$line”$end”
然后
del=0
fi
#开始标记,必须删除
如果匹配“$line”$start”
然后
del=1
fi
如果[$del-等式0]
然后
回音“$line”
fi
完成

简单的解决方案:试试这种方法

Inputfile.txt

text 
SECTION - PAY 500    
text                 
SECTION - Pay 400
text 
SECTION - PAY 500   
text                 
SECTION - Pay 400
text
text 
SECTION - Pay 400
text 
SECTION - Pay 400
text 
代码

awk '/500/{print;getline;next}1' Inputfile.txt | sed '/500/d'
输出

text 
SECTION - PAY 500    
text                 
SECTION - Pay 400
text 
SECTION - PAY 500   
text                 
SECTION - Pay 400
text
text 
SECTION - Pay 400
text 
SECTION - Pay 400
text 

假设您希望从3000页的文档中删除多个块,您能再给我们举几个例子吗。您希望删除多少个块?章节标记中的文本是否存在歧义,即
章节-PAY 5000
?祝你好运。
sed
将是我的目标。