如果该部分具有搜索关键字 [BASH],如何打印文件中的特定部分
How to print specific section from a file if that section has the search keyword [BASH]
下面是名为 "books.info":
的文件的片段
TITLE and AUTHOR ETEXT NO.
Aspects of plant life; with special reference to the British flora, 56900
by Robert Lloyd Praeger
The Vicar of Morwenstow, by Sabine Baring-Gould 56899
[Subtitle: Being a Life of Robert Stephen Hawker, M.A.]
Raamatun tutkisteluja IV, mennessä Charles T. Russell 56898
[Subtitle: Harmagedonin taistelu]
[Language: Finnish]
Tom Thatcher's Fortune, by Horatio Alger, Jr. 56896
A Yankee Flier in the Far East, by Al Avery 56895
and George Rutherford Montgomery
[Illustrator: Paul Laune]
Nancy Brandon's Mystery, by Lillian Garis 56894
The Junior Classics, Volume 3: Tales from Greece and Rome, by Various 56887
~ ~ ~ ~ Posting Dates for the below eBooks: 1 Mar 2018 to 31 Mar 2018 ~ ~ ~ ~
TITLE and AUTHOR ETEXT NO.
The American Missionary, Volume 41, No. 1, January, 1887, by Various 56886
Morganin miljoonat, mennessä Sven Elvestad 56885
[Author a.k.a. Stein Riverton]
[Subtitle: Salapoliisiromaani]
[Language: Finnish]
"Trip to the Sunny South" in March, 1885, by L. S. D 56884
Balaam and His Master, by Joel Chandler Harris 56883
[Subtitle: and Other Sketches and Stories]
如果我按作者姓名搜索一本书,我会尝试获取该书的完整信息。
示例 1:
搜索关键字:Al Avery
那么return应该是:
A Yankee Flier in the Far East, by Al Avery 56895
and George Rutherford Montgomery
[Illustrator: Paul Laune]
示例 2:
搜索关键字:罗伯特·劳埃德·普拉格
那么return应该是:
Aspects of plant life; with special reference to the British flora, 56900
by Robert Lloyd Praeger
我试过这个:
#!/bin/bash
if [ -f books.info ]
then
echo Please enter search keyword:
read keyword
command=$(grep -i "$keyword" books.info)
echo $command
else
echo books.info file is missing
fi
但它并没有完全按照我想要的方式工作。谁能帮我解决这个问题?
编辑
注意到日期线后更新了文件结构。
试过:
awk -v RS='\n\n' "/${keyword}/" infile
和:
awk -v RS= -v keyword="$keyword" '$0 ~ keyword' file.txt
正如@andlrc 和@α˓sнιη 在下面给出的答案所建议的那样,但它会打印出许多其他不相关的内容。我最好的猜测是这条线打破了它:
~ ~ ~ ~ Posting Dates for the below eBooks: 1 Mar 2018 to 31 Mar 2018 ~ ~ ~ ~
因为这一行的顶部有 2 个 space 和下面的一个 space,我认为是因为前两个 space 它不能正常工作。
您可以使用如下 grep
命令:-
grep search_string -A n -B m file.extension
这里 -A
代表搜索行之后, -B
代表搜索行之前 所以你可以试试:-
command=$(grep -i -A 2 "$keyword" books.info)
我会使用 awk
,只需定义一个空行作为 Record Separator 并搜索包含的字符串在任何区块中:
awk -v RS='\n\n' '/Al Avery/' infile
或将 RS 设置为空字符串
awk '/Al Avery/' RS= infile
一旦你需要获取每个用户输入的块,那么你可以这样做:
awk -v RS='\n\n' "/${keyword}/" infile
注意使用双引号而不是单引号,这允许 shell 首先扩展变量然后在 awk.
中使用它
与 关键字 一起工作,如果它包含反斜杠 \
或斜杠 /
和任何,你可以使用 awk 如下(对于Shell (Bash, ksh, ksh93, mksh, zsh) which supports Pattern Substitution Expansion
):
PATTERN="${keyword//\/\\}" awk '[=13=] ~ ENVIRON["PATTERN"]' RS= infile
您可以利用 awk 和记录分隔符变量:
awk -v RS= -v keyword="$keyword" '[=10=] ~ keyword' file.txt
您可以通过 -v
选项为 awk 设置变量; -v name=val
。在您的情况下,我们需要将变量 "keyword" 设置为同名 bash 变量的值:
-v keyword="$keyword"
awk 脚本是:
[=12=] ~ keyword
这会将整个内容块与变量 "keyword" 中提供的正则表达式进行匹配。
If RS is null, then records are separated by sequences consisting of a plus one or more blank lines
来源:$ man awk | awk '/RS is null/' RS=
下面是名为 "books.info":
的文件的片段TITLE and AUTHOR ETEXT NO.
Aspects of plant life; with special reference to the British flora, 56900
by Robert Lloyd Praeger
The Vicar of Morwenstow, by Sabine Baring-Gould 56899
[Subtitle: Being a Life of Robert Stephen Hawker, M.A.]
Raamatun tutkisteluja IV, mennessä Charles T. Russell 56898
[Subtitle: Harmagedonin taistelu]
[Language: Finnish]
Tom Thatcher's Fortune, by Horatio Alger, Jr. 56896
A Yankee Flier in the Far East, by Al Avery 56895
and George Rutherford Montgomery
[Illustrator: Paul Laune]
Nancy Brandon's Mystery, by Lillian Garis 56894
The Junior Classics, Volume 3: Tales from Greece and Rome, by Various 56887
~ ~ ~ ~ Posting Dates for the below eBooks: 1 Mar 2018 to 31 Mar 2018 ~ ~ ~ ~
TITLE and AUTHOR ETEXT NO.
The American Missionary, Volume 41, No. 1, January, 1887, by Various 56886
Morganin miljoonat, mennessä Sven Elvestad 56885
[Author a.k.a. Stein Riverton]
[Subtitle: Salapoliisiromaani]
[Language: Finnish]
"Trip to the Sunny South" in March, 1885, by L. S. D 56884
Balaam and His Master, by Joel Chandler Harris 56883
[Subtitle: and Other Sketches and Stories]
如果我按作者姓名搜索一本书,我会尝试获取该书的完整信息。
示例 1:
搜索关键字:Al Avery
那么return应该是:
A Yankee Flier in the Far East, by Al Avery 56895
and George Rutherford Montgomery
[Illustrator: Paul Laune]
示例 2:
搜索关键字:罗伯特·劳埃德·普拉格
那么return应该是:
Aspects of plant life; with special reference to the British flora, 56900
by Robert Lloyd Praeger
我试过这个:
#!/bin/bash
if [ -f books.info ]
then
echo Please enter search keyword:
read keyword
command=$(grep -i "$keyword" books.info)
echo $command
else
echo books.info file is missing
fi
但它并没有完全按照我想要的方式工作。谁能帮我解决这个问题?
编辑
注意到日期线后更新了文件结构。 试过: awk -v RS='\n\n' "/${keyword}/" infile 和: awk -v RS= -v keyword="$keyword" '$0 ~ keyword' file.txt 正如@andlrc 和@α˓sнιη 在下面给出的答案所建议的那样,但它会打印出许多其他不相关的内容。我最好的猜测是这条线打破了它:
~ ~ ~ ~ Posting Dates for the below eBooks: 1 Mar 2018 to 31 Mar 2018 ~ ~ ~ ~
因为这一行的顶部有 2 个 space 和下面的一个 space,我认为是因为前两个 space 它不能正常工作。
您可以使用如下 grep
命令:-
grep search_string -A n -B m file.extension
这里 -A
代表搜索行之后, -B
代表搜索行之前 所以你可以试试:-
command=$(grep -i -A 2 "$keyword" books.info)
我会使用 awk
,只需定义一个空行作为 Record Separator 并搜索包含的字符串在任何区块中:
awk -v RS='\n\n' '/Al Avery/' infile
或将 RS 设置为空字符串
awk '/Al Avery/' RS= infile
一旦你需要获取每个用户输入的块,那么你可以这样做:
awk -v RS='\n\n' "/${keyword}/" infile
注意使用双引号而不是单引号,这允许 shell 首先扩展变量然后在 awk.
中使用它 与 关键字 一起工作,如果它包含反斜杠 \
或斜杠 /
和任何,你可以使用 awk 如下(对于Shell (Bash, ksh, ksh93, mksh, zsh) which supports Pattern Substitution Expansion
):
PATTERN="${keyword//\/\\}" awk '[=13=] ~ ENVIRON["PATTERN"]' RS= infile
您可以利用 awk 和记录分隔符变量:
awk -v RS= -v keyword="$keyword" '[=10=] ~ keyword' file.txt
您可以通过 -v
选项为 awk 设置变量; -v name=val
。在您的情况下,我们需要将变量 "keyword" 设置为同名 bash 变量的值:
-v keyword="$keyword"
awk 脚本是:
[=12=] ~ keyword
这会将整个内容块与变量 "keyword" 中提供的正则表达式进行匹配。
If RS is null, then records are separated by sequences consisting of a plus one or more blank lines
来源:$ man awk | awk '/RS is null/' RS=