Linux 使用shell脚本从以前创建的文件中删除单词

Linux 使用shell脚本从以前创建的文件中删除单词,linux,bash,shell,Linux,Bash,Shell,假设我们确实有一个以前创建的文件ABC cat ABC //Checking contents of **ABC** hello hi how what where 现在,在shell脚本的帮助下,我确实希望从ABCABC cat ABC //Checking contents of **ABC** hello hi how what where 我正在尝试这个 echo Enter file name read a if[ $a -f ] then _____

假设我们确实有一个以前创建的文件ABC

cat ABC      //Checking contents of **ABC**

hello
hi 
how
what
where
现在,在shell脚本的帮助下,我确实希望从ABCABC

cat ABC      //Checking contents of **ABC**

hello
hi 
how
what
where
我正在尝试这个

echo Enter file name 
read a
if[ $a -f ]
then
_____ |grep how ABC
fi
是否有任何命令可在____;处使用


也欢迎使用所有其他解决方案。

您可以使用
sed

read -p "Enter file name: " file
[[ -f "$file" ]] && sed -i.bak '/how/d' ABC
/how/d
将删除文件
ABC
中模式为
how
的行(如果找到),并将更改保存回
ABC
。它还会创建一个名为ABC.bak的原始文件备份,以防出现问题

如果您只想替换一个单词(而不删除包含该单词的整行),请使用:

sed -i.bak 's/how//' ABC

您正在删除包含单词
how
的整行,您应该使用替换,例如
sed's/how//g'ABC
,谢谢anubhava,但是是否可以使用grep和我上面提到的其他命令删除单词“how”。为什么要使用命令(
grep
,它只是搜索文本)要删除一些文本,而不是另一个(
sed
),它既可以搜索文本,也可以从文件中删除文本?@MarcoS只是好奇是否可能。。。因此,您应该更改您的问题,删除“所有其他解决方案也受欢迎”这句话,并添加“该命令应使用
grep
”。但我想它的兴趣应该很小……:-)