如果我有一个文本文件,其记录以//分隔,标题包含单行或多行内容,例如
SOMETHING (single line content)
ATHING Lorem (single line content)
THETHING (single line content)
THING (multi-line content)
ANOTHERTHING (single line content)
//
SOMETHING (single line content)
ATHING Lorem (single line content)
THETHING (single line content)
THING (multi-line content)
ANOTHERTHING (single line content)
我想要打印: 1)匹配“ATHING”的行和2)匹配以THING开头直到下一个标题的多行行,以便我最终得到这个输出:
ATHING content, THING content (multi-line concatenated to single line)
ATHING content, THING content (multi-line concatenated to single line)
答案 0 :(得分:2)
awk 解决方案:
示例testfile
内容:
SOMETHING (single line content)
ATHING Lorem (single line content)
THETHING (single line content)
THING (multi-line content)
some tetx
sdsdf text
ANOTHERTHING (single line content)
//
SOMETHING (single line content)
ATHING Lorem (single line content)
THETHING (single line content)
THING (multi-line content)
text
text
ANOTHERTHING (single line content)
工作:
awk -v th="^THING" '/^ATHING/{ printf "%s,",$0 }
$0~th{ f=1 }
f{ if ($0~/^[A-Z]/ && $0!~th){ f=0; print "" } else printf " %s",$0; }' testfile
输出:
ATHING Lorem (single line content) , THING (multi-line content) some tetx sdsdf text
ATHING Lorem (single line content) , THING (multi-line content) text text
答案 1 :(得分:0)
BEGIN { OFS = ", " }
/^\/\// && line { print line;
line = "";
getline;
next }
NR > 1 && line { line = line OFS $0 }
NR > 1 && !line { line = $0 }
END { print line }
此awk
脚本将在line
中构建每个输出行,并在适当时输出。
BEGIN
块设置用于连接线的分隔符。//
分隔符并且在line
中组装了一条线时,第二个块执行。它打印该行并重置该变量。它还跳过下一行输入(SOMETHING
输入行),然后从脚本开始后继续下一个输入行。NR > 1
,我们会跳过最初的SOMETHING
行。如果line包含某些内容,则会将当前行附加到其中,否则我们只需将line
设置为当前输入行。对于给定的数据,这会产生:
$ awk -f script.awk file.in
ATHING Lorem (single line content), THETHING (single line content), THING (multi-line content), ANOTHERTHING (single line content)
ATHING Lorem (single line content), THETHING (single line content), THING (multi-line content), ANOTHERTHING (single line content)