删除方括号之间包含特定文本的所有行

时间:2020-09-28 07:13:11

标签: sed grep

我正在处理一些数据。 我想删除[]之间包含特定文本的每一行 我的文件如下:

cat file1 

today_is_a_new_day#1 12123 12356 [today is a new day]
today_is_a_new_day#2 14531 12456 [today is a new day]
today_is_a_new_day#3 12345 12456 [today is a new day]
tommorow_will_be_a_new_day#1 2342 34124 [tommorow will be a new day]
tommorow_will_be_a_new_day#2 2342 34144 [tommorow will be a new day]
today_is_a_new_day#4 12343 12452 [today is a new day]
today_is_a_new_day#5 12348 12451 [today is a new day]
tommorow_will_be_a_new_day#3 2342 34144 [today is a new day]

我想获得

tommorow_will_be_a_new_day#1 2342 34124 [tommorow will be a new day]
tommorow_will_be_a_new_day#2 2342 34144 [tommorow will be a new day]

我尝试了以下操作:

egrep  -ve "\[today" -e is -e a -e new -e "day]" file1 

echo [today is a new day] >file2
cat file1 |grep -vwf- file2 

我发现工作的唯一方法是:

sed 's/today_is_a_new_day/temp/'g file1| grep -vi today |sed 's/temp/today_is_a_new_day/'g >desired_output.txt

有什么方法可以使其更简单?

4 个答案:

答案 0 :(得分:2)

如果对awk感兴趣;这将为您工作。

awk '!/\[today is a new day\]/' inputfile

答案 1 :(得分:1)

对我来说,看起来像是您想要的:

grep -Fv '[today is a new day]' file

答案 2 :(得分:1)

Sed变体

$ sed '/\[tommorow will be a new day\]/d' file 
today_is_a_new_day#1 12123 12356 [today is a new day]
today_is_a_new_day#2 14531 12456 [today is a new day]
today_is_a_new_day#3 12345 12456 [today is a new day]
today_is_a_new_day#4 12343 12452 [today is a new day]
today_is_a_new_day#5 12348 12451 [today is a new day]
tommorow_will_be_a_new_day#3 2342 34144 [today is a new day]

答案 3 :(得分:1)

sed方法:删除包含sed '/^.*\[today .*day\].*$/d' file1 > file2的所有行。 P / s:为什么不只使用cat和grep呢?