Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash在两个字符串之间打印文件内容_Bash_Shell_Awk_Grep_Cat - Fatal编程技术网

Bash在两个字符串之间打印文件内容

Bash在两个字符串之间打印文件内容,bash,shell,awk,grep,cat,Bash,Shell,Awk,Grep,Cat,我想打印start和start后面的第一个end之间的内容(start始终是唯一的)。我还想在打印文本的行之间进行打印,在本例中,在第4行和第7行之间 我尝试了grep和cat,但做不了多少 我试过: a b s start text more text end even more text end 将shell变量传递到awk,然后按范围打印文本,然后重试,在startawk的variable中提到您的shell变量,然后我们就可以了。(同时将$0~start更改为$0~“^”start“$

我想打印
start
start
后面的第一个
end
之间的内容(
start
始终是唯一的)。我还想在打印文本的行之间进行打印,在本例中,在第4行和第7行之间

我尝试了
grep
cat
,但做不了多少

我试过:

a
b
s
start
text
more text
end
even more text
end

将shell变量传递到
awk
,然后按范围打印文本,然后重试,在
start
awk的
variable中提到您的shell变量,然后我们就可以了。(同时将
$0~start
更改为
$0~“^”start“$”
,以防您要查找行中的起始值的精确匹配。)

OP样本的样本输出:

简单解释:按范围打印行
开始
直到
结束
在此语句之间检查条件如果行有结束字符串,则从输入_文件中出来,我们不需要读取完整的输入_文件,因为OP只需要打印第一组行。

示例数据:

start
text
more text
end
The content is between lines 4 and 7
一个
awk
解决方案,使用一个范围(类似于RavinderSingh13的帖子),在结尾打印OP的文本消息:

$ cat -n strings.dat
 1  a
 2  b
 3  s
 4  start
 5  text
 6  more text
 7  end of more text
 8  end
 9  even more text
10  end
注意
$0==ss
/^end$/
测试假定数据文件中没有前导/尾随空格,否则这些测试将失败,并且没有范围匹配

使用
startstring=“start”
这将生成:

startstring="start"                            # define start of search block

awk -v ss="${startstring}" '                   # pass start of search block in as awk variable "ss"

# search for a range of lines between "ss" and "end":

$0==ss,/^end$/ { if ($0==ss && x==0 ) x=FNR    # if this is the first line of the range make note of the line number
                 print                         # print the current line of the range
                 if ($0=="end")                # if this is the last line of the range then print our textual message re: start/finish line numbers
                    printf "\nThe content is between lines %d and %d.\n",x,FNR
               }
' strings.dat
start
text
more text
end of more text
end

The content is between lines 4 and 8.
more text
end of more text
end

The content is between lines 6 and 8.
even more text
end

The content is between lines 9 and 10.
使用
startstring=“more text”
这将生成:

startstring="start"                            # define start of search block

awk -v ss="${startstring}" '                   # pass start of search block in as awk variable "ss"

# search for a range of lines between "ss" and "end":

$0==ss,/^end$/ { if ($0==ss && x==0 ) x=FNR    # if this is the first line of the range make note of the line number
                 print                         # print the current line of the range
                 if ($0=="end")                # if this is the last line of the range then print our textual message re: start/finish line numbers
                    printf "\nThe content is between lines %d and %d.\n",x,FNR
               }
' strings.dat
start
text
more text
end of more text
end

The content is between lines 4 and 8.
more text
end of more text
end

The content is between lines 6 and 8.
even more text
end

The content is between lines 9 and 10.
使用
startstring=“甚至更多文本”
将生成:

startstring="start"                            # define start of search block

awk -v ss="${startstring}" '                   # pass start of search block in as awk variable "ss"

# search for a range of lines between "ss" and "end":

$0==ss,/^end$/ { if ($0==ss && x==0 ) x=FNR    # if this is the first line of the range make note of the line number
                 print                         # print the current line of the range
                 if ($0=="end")                # if this is the last line of the range then print our textual message re: start/finish line numbers
                    printf "\nThe content is between lines %d and %d.\n",x,FNR
               }
' strings.dat
start
text
more text
end of more text
end

The content is between lines 4 and 8.
more text
end of more text
end

The content is between lines 6 and 8.
even more text
end

The content is between lines 9 and 10.
通过
startstring=“water”
这将生成:

startstring="start"                            # define start of search block

awk -v ss="${startstring}" '                   # pass start of search block in as awk variable "ss"

# search for a range of lines between "ss" and "end":

$0==ss,/^end$/ { if ($0==ss && x==0 ) x=FNR    # if this is the first line of the range make note of the line number
                 print                         # print the current line of the range
                 if ($0=="end")                # if this is the last line of the range then print our textual message re: start/finish line numbers
                    printf "\nThe content is between lines %d and %d.\n",x,FNR
               }
' strings.dat
start
text
more text
end of more text
end

The content is between lines 4 and 8.
more text
end of more text
end

The content is between lines 6 and 8.
even more text
end

The content is between lines 9 and 10.

注意:如果OP使用
startstring=“end”
结果与预期不符;虽然可以添加更多代码来解决此方案,但我暂时跳过此方案。

请在您的问题中添加您的努力,这是非常鼓励的,谢谢。您想在输出中打印行和行号吗?请使用代码格式块中的预期输出更新问题;如果你打算使用出口,你也可以在发出出口之前打印摘要,这样就不需要使用结束块了;谢谢,这对我不起作用。我应该补充一点,文件中可能有空白,文件是.cpp,如果这很重要,您必须详细说明
对我不起作用
;文件名/扩展名不重要,重要的是文件的内容,我们需要对所述文件内容进行准确描述(即,用更多细节更新问题)。我认为它会一直读到文件的末尾,而不会停在“结束”处。它从正确的位置开始。@GeoCap,如果您的行与
end
完全相同(此处不考虑行尾、行首或任何其他单词的空格),则它将打印第一组匹配值。假设开始变量是
bla
,那么它应该打印从
bla
end
(第一次出现)的值,然后从文件中出来,如果有任何查询或它在哪里不起作用,请务必让我知道。对于我来说,带有变量的示例打印从变量开始的文件和整个文件(不停在“end”)。没有变量的示例在“end”处停止,测试了同一个文件。哦,是的,我有空行,抱歉,现在它可以工作了,谢谢!是的,我感谢您的帮助,因为我的问题解决了一半,我仍然希望打印提取内容的行数。