我有一个示例数据集,其中一个部分具有以“某事”开头的字段。我想在匹配特定字符串“1234”时获取每个“something”部分中的所有行。
我想我可以搜索“1234”并打印之前和之后的所有行,直到匹配“某事”。
期望的输出:
something like this one
1234
abcd
something like this one
zyxw
1234
示例数据集:
otherthings
otherthings
otherthings
something like this one
1234
abcd
something not like this one
xxxx
yyyy
something not like this one
xxxx
yyyy
something like this one
1234
abcd
otherthings
otherthings
otherthings
答案1
使用“awk”:
#!/bin/sh
awk '
function print_section() {
# Only print section if "1234" was encountered
if (valid == 1) print section;
}
{
if (/something/) {
# Start new section
section = $0;
}
else if (/^\s*$/) {
# Empty line -> output previous section
if (section ne "") {
print_section();
section = "";
valid = 0;
}
}
else if (section ne "") {
# Add line to section if one has been started
section = section "\n" $0;
if (/1234/) valid = 1;
}
}
END {
# End of file, print current section if it exists
print_section();
}
' file