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

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

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

问题

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

  1. 这是第一行
  2. 这是第二行
  3. 这是第三行
  4. 这是第四行
  5. 这是第五行
  6. 这是第六行

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

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

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

希望这对你有所帮助。

英文:

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

  1. this line 1
  2. this line 2
  3. this line 3
  4. this line 4
  5. this line 5
  6. 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:

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

答案1

得分: 1

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

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

给定 ex 命令的参数:

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

输入:list.txt

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

输出结果:list.txt

  1. line 3
  2. line 4
  3. line 5
  4. line 6
英文:

Below the command which displays the requested transformations.

  1. $ 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. +'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)
  1. +'g/^break$/d'
  • For all lines (%) subsitute (s) matching (/) something (this) (/) [with nothing] (/) also if multiple times one a line (g)
  1. +'%s/this//g'
  • Combined with the ex commands to write and quit:
  1. +'wq'

INPUT: list.txt

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

RESULTING OUTPUT: list.txt

  1. line 3
  2. line 4
  3. line 5
  4. 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:

确定