删除特定行到特定行的ex命令行在list.txt的bash脚本中。

huangapple go评论66阅读模式
英文:

ex command line for delete frome specific line to specific line in list.txt bash script

问题

我有一个名为list.txt的文件,其中有几行内容,如下:

这是第一行
这是第二行
这是第三行
这是第四行
这是第五行
这是第六行

我想要使用ex命令从第一行删除到第二行,而不使用vim编辑器,如果可能的话,我想要使用ex命令。

我只知道如何删除列表中特定的单词,比如从列表中删除"break"这个词:

$ ex +g/^"break"$/d -cwq list.txt

希望这对你有所帮助。

英文:

I have a list.txt file with a couple of lines like:

this line 1
this line 2
this line 3
this line 4
this line 5
this line 6

I want to delete from line 1 to line 2 with the ex command without using vim editor, I want use the ex command if that is possible

I just know how to delete specific word like break from the list:

$ ex +g/^"break"$/d -cwq list.txt

答案1

得分: 1

以下是显示所请求的转换的命令:

$  ex list.txt +'1,2d' +'g/^break$/d' +'%s/this//g' +'wq'

给定 ex 命令的参数:

  1. 从行号 1 到行号 2(包括 2),删除行:
+'1,2d'
  1. 删除特定行。
    对于从文件开头到文件末尾的所有行,匹配以文本 "break" 开始并紧跟行尾的行,执行删除操作:
+'g/^break$/d'
  1. 对于所有行,替换匹配的 "this" 为 ""(删除),如果一行上多次出现(全局替换):
+'%s/this//g'
  1. 与 ex 命令结合使用以写入并退出:
+'wq'

输入:list.txt

this line 1
this line 2
this line 3
this line 4
break
this line 5
this line 6

输出结果:list.txt

 line 3
 line 4
 line 5
 line 6
英文:

Below the command which displays the requested transformations.

$  ex list.txt +'1,2d' +'g/^break$/d' +'%s/this//g' +'wq'

Arguments of the given ex command:

>From line number (1) until (,) linenumber (2) inclusive, delete (d) line:

+'1,2d'
  • Delete a specific line.
    For all line from beginning-to-the-end-of-file (g) match on starting line (^) with text (break) followed directly with end of line ($), do delete (d)
+'g/^break$/d'
  • For all lines (%) subsitute (s) matching (/) something (this) (/) [with nothing] (/) also if multiple times one a line (g)
+'%s/this//g'
  • Combined with the ex commands to write and quit:
 +'wq'

INPUT: list.txt

this line 1
this line 2
this line 3
this line 4
break
this line 5
this line 6

RESULTING OUTPUT: list.txt

 line 3
 line 4
 line 5
 line 6

Good luck

huangapple
  • 本文由 发表于 2023年2月27日 04:50:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定