gpt4 book ai didi

bash - 在 bash 中包含以空格开头的换行符?

转载 作者:行者123 更新时间:2023-11-29 09:31:10 24 4
gpt4 key购买 nike

所以,文本文件是这样的:

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]

Raamatun tutkisteluja III, mennessä Charles T. Russell 56897
[Subtitle: Tulkoon valtakuntasi]
[Language: Finnish]

文件名为 GUTINDEX.ALL。

我想在文件中搜索字符串。当它找到时,它应该打印整个记录。

所以我这样做了:

#!/bin/sh
read -p 'string to search ' searchstring
grep --color "$searchstring" GUTINDEX.ALL

当我搜索字符串“flora”时它打印

Aspects of plant life; with special reference to the British flora,    56900

但如果新行以空格开头,我也想打印它。像这样:

Aspects of plant life; with special reference to the British flora,   56900

by Robert Lloyd Praeger

最佳答案

首先阅读整个记录然后进行搜索。需要在文件末尾换行:

sed -n ':l N;/^$/!tl;/'"$searchstring"'/p' input

关于bash - 在 bash 中包含以空格开头的换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50082841/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com