搜索文件中的字符串,然后通过Shell脚本从该文件中删除它 [英] Search a string in a file and delete it from this file by Shell Script

查看:41
本文介绍了搜索文件中的字符串,然后通过Shell脚本从该文件中删除它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从文件中删除包含特定字符串的行.不使用 awk 怎么办?我尝试使用 sed ,但无法实现.

I want to delete a line containing a specific string from the file. How can I do this without using awk? I tried to use sed but I could not achieve it.

推荐答案

这应该做到:

sed -e s/deletethis//g -i *
sed -e "s/deletethis//g" -i.backup *
sed -e "s/deletethis//g" -i .backup *

它将在所有文件( * )中将所有出现的"deletethis"替换为"(不作任何替换),并在适当位置进行编辑.

it will replace all occurrences of "deletethis" with "" (nothing) in all files (*), editing them in place.

在第二种形式中,可以更安全地编辑该模式,并通过在文件后缀".backup"后对它们进行备份.

In the second form the pattern can be edited a little safer, and it makes backups of any modified files, by suffixing them with ".backup".

第三种形式是 sed 的某些版本喜欢的方式.(例如Mac OS X)

The third form is the way some versions of sed like it. (e.g. Mac OS X)

man sed 了解更多信息.

这篇关于搜索文件中的字符串,然后通过Shell脚本从该文件中删除它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆