gpt4 book ai didi

linux - 使用 shell 脚本从先前创建的文件中删除单词

转载 作者:太空狗 更新时间:2023-10-29 12:03:09 26 4
gpt4 key购买 nike

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

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

hello
hi
how
what
where

Now with the help of shell script i do want to remove the word 'how' from ABC

我正在尝试这个

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

关于linux - 使用 shell 脚本从先前创建的文件中删除单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27382396/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com