我可以搜索/在多个.txt文件从终端迅速取代? [英] Can I search/replace in multiple .txt files quickly from Terminal?

查看:99
本文介绍了我可以搜索/在多个.txt文件从终端迅速取代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含纯文本约100 .txt文件。不知何故,一些数据已损坏,需要找到/取代。

I have about 100 .txt files that contain plain text. Somehow, some of the data has been corrupted and needs to be found/replaced.

我需要搜索的字符 - 和用一个长划替换:' - '

I need to search for the characters'--' and replace it with a long dash: '—'.

有没有一种方法来在终端的命令迅速做到这一点?

Is there a way to do this quickly with a command in terminal?

在我的目录中的.txt文件的名称按顺序编号:的1.txt,2.txt等

The names of the .txt files in my directory are numbered sequentially: 1.txt, 2.txt, etc.

谢谢!

推荐答案

GNU SED

GNU sed:

sed -i 's/--/—/g' *.txt

OSX BSD SED

OSX BSD sed:

您需要指定备份文件的扩展名。要创建具有扩展的备份文件: .txt.bak

You need to specify a backup file extension. To create a backup file with the extension: .txt.bak:

sed -i '.bak' 's/--/—/g' *.txt

要完全取代文件,指定一个空的扩展:

To completely replace the files, specify an empty extension:

sed -i '' 's/--/—/g' *.txt

这篇关于我可以搜索/在多个.txt文件从终端迅速取代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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