英文:
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 到行号 2(包括 2),删除行:
+'1,2d'
- 删除特定行。
对于从文件开头到文件末尾的所有行,匹配以文本 "break" 开始并紧跟行尾的行,执行删除操作:
+'g/^break$/d'
- 对于所有行,替换匹配的 "this" 为 ""(删除),如果一行上多次出现(全局替换):
+'%s/this//g'
- 与 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论