sed - 使用 sed 删除目录中多个文件的某些文本

标签 sed

我使用以下方法从多个文件中删除相同的文本

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/

相关文章:

linux - 如何在日志中未输入确切时间戳时过滤日期范围内的值

regex - 替换大文件中带引号的字符串中的换行符

linux - sed 命令查找特定单词

bash - 如何使用 awk 或 sed 递归查找/替换字符串?

macos - 在 bash 脚本中使用 sed 替换多行

linux - 如何在 sed 文件中测试 linux 变量?

linux - 如果字符也在下一个单词(sed)中,如何从单词中删除字符?

linux - 我在 sed 中不断收到意外标记 `/bin/busybox.exe' 错误。我该如何解决这个问题?

linux - 在两个模式之间添加字符串 - sed

shell - POSIX:abcdef 到 ab bc cd de ef