【发布时间】:2022-10-25 14:36:38
【问题描述】:
我需要帮助grep 一个包含大量数据的文件。我有一个带有以下行的文件:
random line with hashcode 1
This file is use for some analysis
Analysis code is <01234>
This is line after analysis
This is second test line
This file is use for some analysis
Analysis code is <01234>
This is line after analysis
Some data to be here as well
This file is use for some analysis
Analysis code is <01267>
This is line after analysis
我只想打印那些具有字符串“分析代码”且值为“01234”的行,并打印它之前和之后的所有行。 我试图半途而废,但需要完整的逻辑。
egrep -i "Analysis code" c.txt |
grep -i 01234 |
awk -F "<" '{print $2}' |
awk -F ">" '{print $1}' |
uniq > am.txt
while read line ; do
echo $line
awk "/$line/,/$^/" c.txt
done <am.txt
在此之后,我只从具有分析代码的行开始获得输出。
我想打印匹配字符串之前的所有行,直到空白行出现在顶部:
random line with hashcode 1
This file is use for some analysis
Analysis code is <01234>
This is line after analysis
This is second test line
This file is use for some analysis
Analysis code is <01234>
This is line after analysis
【问题讨论】:
-
怎么样:
awk -v RS="" -v ORS="\n\n" -v IGNORECASE=1 '/analysis code/ && /01234/' am.txt。 -
谢谢tshiono。它奏效了,这就是我正在寻找的。你拯救了我的一天。