在一个文件中与bash的新的一行替换两行 [英] Replace two lines in a file with one new line with bash

查看:452
本文介绍了在一个文件中与bash的新的一行替换两行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个新的生产线来替换文件中有两行:

I am trying to replace two lines in a file with one new line:

foo1.txt
  aaa   aaa
  bbb   bbb
  ccc   ccc
  ddd   ddd
  bbb   bbb
  ddd   ddd

后替换文件应该是这样的。

after the replace the file should look like this

foo1.txt
  aaa   aaa
  eee   eee
  ddd   ddd
  bbb   bbb
  ddd   ddd

是否与中美战略经济对话的方式或其他一些命令使这个替换文件夹中的所有文件

Is there a way with sed or some other command make this replace in all files of a folder

我一直在尝试使用sed,但没有成功: SED的/ BBB \\ tbbb \\ NCCC \\ TCCC / EEE \\ TEEE / g的'富* .TXT

I have been trying with sed but without any success: sed 's/bbb\tbbb\nccc\tccc/eee\teee/g' foo*.txt

推荐答案

即使这个问题应该已经回答的在这个线程,我没使一行一命令解决方案的工作。

Even if this question should be already answered in this thread, I didn't manage to make the "one-line-one-command" solution work.

此命令:

perl -pe 's/START.*STOP/replace_string/g' file_to_change

似乎没有为我工作,不会执行多线代替。我不得不在这两个不同的Perl脚本分裂,就像这样:

seems not to work for me and doesn't perform a multi-line replace. I had to split it in two different perl scripts, like this:

perl -pe 's/bbb\tbbb\n.*/placeholderstring/g' foo1.txt | perl -pe 's/placeholderstring  ccc\tccc/eee\teee/g'

试试,看看有什么最适合你。

Try to see what works best for you.

编辑:

随着新的示范文本,唯一可行的解​​决方案是一个接威廉Pursell

With the new sample text, the only solution that works is the one by William Pursell

sed '/bbb\tbbb/{ N; s/.*ccc\tccc/  eee\teee/; }' foo1.txt

这篇关于在一个文件中与bash的新的一行替换两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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