我使用以下方法从多个文件中删除相同的文本
sed -n -i '' '/<?xml version="1.0" encoding="UTF-8"?>/q;p' myfile.emlx
目前为止有效。
我知道我可以对当前目录中的所有文件运行它。我用这个测试了
sed -i '' 's/^/\t/' *.txt
并且按预期工作,但是当我尝试时
sed -n -i '' '/<?xml version="1.0" encoding="UTF-8"?>/q;p' *.emlx
尽管第一个文件已正确修改并保存,但所有其他文件中的所有文本都被删除。 我尝试了 -i 和 -n 等的各种组合,但无法解决我的问题。
最佳答案
您的方法的问题在于 sed
跨文件边界携带模式。您将希望恢复为每个文件运行一个 sed
实例。
for file in *.emlx; do
sed -n -i '' '/<?xml version="1.0" encoding="UTF-8"?>/q;p' "$file"
done
快速演示:
bash$ printf '%s\n' >foo "one" "two" "three"
bash$ printf '%s\n' >bar "four" "five" "six"
bash$ tail *
===> foo <===
one
two
three
===> bar <===
four
five
six
bash$ sed '/two/,/five/d' foo bar
one
six
关于sed - 使用 sed 删除目录中多个文件的某些文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67266997/