bash - 如何从多个文件中删除特定字符串之前的所有行

标签 bash shell sed

我有n个文件,比如:

文件1:

1aaa
2eee

Test        XXX
Hanna
Lars 

文件2:

1fff
2ddd
3zzz

Test        XXX
Mike
Charly

我想从所有 n 个文件中删除“Test XXX”之前的所有行。 要删除的行数因文件而异。

我的想法:

for file in 1 :n
do
pos=grep -n "Test XXX" file$file
sed -i "1:$pos-1 d" file$file >new$file
done

最佳答案

这应该适合你:

sed -i '1,/Test XXX/d' file1
sed -i '1,/Test XXX/d' file2

或者只是

sed -i '1,/Test XXX/d' file*

关于bash - 如何从多个文件中删除特定字符串之前的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7416921/

相关文章:

bash - sed 是否有仅用于检查文件中是否存在字符串的选项?

linux - 如何从Linux中另一个文件的数据编辑一个文件中带有扩展名的行

无法使用 fork 和 pipe 在 C 中执行控制台命令

mysql - 导入具有 `max_allowed_packet` 限制问题且没有管理员访问权限的数据库

python - 如何告诉 `complete` 回退到默认值?

shell - xargs' -x 选项的示例

shell - 为什么 `help format-list` 与 `format-list | help` 不一样?

bash - 如果列为空,awk 打印一些东西

linux - 无法在ubuntu中执行bash命令: permissions denied

linux - 从部分名称在文本文件中的文件夹中选择 wav 文件