我有一个由几行文本组成的文件:
The first line
The second line
The third line
The fourth line
Run Code Online (Sandbox Code Playgroud)
我有一个字符串是其中一行: The second line
我要删除字符串,并在文件中后,所有行,因此它会删除The third line,并The fourth line在除了字符串.该文件将成为:
The first line
Run Code Online (Sandbox Code Playgroud)
我在谷歌搜索了一个解决方案,似乎我应该使用sed.就像是:
sed 'linenum,$d' file
Run Code Online (Sandbox Code Playgroud)
但是如何找到字符串的行号?或者,我该怎么做呢?
我想忽略在bash中匹配之前出现的所有行(也忽略匹配的行.输入的示例可能是
R1-01.sql
R1-02.sql
R1-03.sql
R1-04.sql
R2-01.sql
R2-02.sql
R2-03.sql
Run Code Online (Sandbox Code Playgroud)
如果我匹配R2-01.sql这个已经排序的输入,我想得到
R2-02.sql
R2-03.sql
Run Code Online (Sandbox Code Playgroud) 我发现我可以用来grep -A10 "search string" file.txt在文件中找到搜索字符串后显示 10 行或grep -B10 "search string" file.txt之前显示 10 行。如何设置它以显示比赛前后的所有行?
例如,我有这个文件:
This is some text.
This is some more text.
This is some text.
This is some more text.
This is some text.
This is some yet more text.
This is some text.
---
This is some more text.
This is some text.
This is some more text.
Run Code Online (Sandbox Code Playgroud)
我需要在第一次搜索中打印在“---”之前找到的所有内容,无论有多少行。
我需要在第二次搜索中,打印在“---”之后找到的所有内容,无论之后有多少行。