我需要在纯文本文件中找到最后一个字符串(没有分隔符或列)并将其行号和整行保存在变量中以供以后在我的脚本中使用
然后我需要检查在刚找到的行之后是否出现第二个字符串。
我不确定如何做到这一点,我在bash上擦洗。我不确定如何在变量中保存awk的结果,而且我不确定我需要找到最后一次出现的字符串的逻辑。任何建议/指导都会很棒
答案 0 :(得分:1)
# Remember last line on which we saw "string_to_match", and the line itself
/string_to_match/ { last1 = NR; line=$0 }
# Remember last line on which we saw "second_string"
/second_string/ { last2 = NR }
# At the end of the file, if last2 was after last1, print it.
END { if (last2 > last1) print last2 }
基本上只是依次处理每一行,每当你发现第一个字符串更新last1
和line
变量时。
同样,每次看到第一个字符串都会更新last2
变量。
当您到达文件末尾时last1
将是您看到第一个字符串的最后一行。此时,您可以看到在该点之后是否看到第二个字符串。您还可以使用last1
和line
进行任何处理。