在文件中查找字符串并在bash中删除它旁边的所有文本

在文件中查找字符串并在bash中删除它旁边的所有文本,bash,Bash,我在一个文件中有以下文本,我想查找单词fox,并想从单词fox中删除文本,直到文本结束。(包括fox) 我知道/d开关,但它只删除该行 sed -i '/fox/d' file 所要求的结果将是: The quick brown 试试这个sed命令 sed '/fox/q' file | sed 's/fox//' 示例: $ cat c The quick brown fox blah fox jumps over the lazy dog $ sed '/fox/q' c

我在一个文件中有以下文本,我想查找单词
fox
,并想从单词
fox
中删除文本,直到文本结束。(包括
fox

我知道
/d
开关,但它只删除该行

sed -i '/fox/d' file
所要求的结果将是:

The quick 
brown

试试这个
sed
命令

sed '/fox/q' file | sed 's/fox//'
示例:

$ cat c
The quick 
brown  fox blah fox 
jumps over 
the lazy dog

$ sed '/fox/q' c | sed 's/fox.*//'
The quick 
brown  

试试这个
sed
命令

sed '/fox/q' file | sed 's/fox//'
示例:

$ cat c
The quick 
brown  fox blah fox 
jumps over 
the lazy dog

$ sed '/fox/q' c | sed 's/fox.*//'
The quick 
brown  
您可以使用此sed:

sed '/fox/{s/fox//;q;}' file
The quick
brown
您可以使用此sed:

sed '/fox/{s/fox//;q;}' file
The quick
brown

谢谢,也是下一个文本,我更新了我的问题。谢谢,也是下一个文本,我更新了我的问题。你最好在第二个
sed
上使用
sed的/fox.*/'
,删除找到
fox
的行的其余部分。不客气!另外请注意,
/g
不是必需的,因为它只会发生一次。您最好在第二个
sed
上使用
sed的/fox.*/'
,以删除找到
fox
的行的其余部分。不客气!还要注意,
/g
不是必需的,因为它只会发生一次。