我想知道如何解析如下所示的解析:
Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text
And many other lines with text that I do not need
* * * * * * *
Autolisp - Dialect of LISP used by the Autocad CAD package, Autodesk,
Sausalito, CA.
CPL -
1. Combined Programming Language. U Cambridge and U London. A very
complex language, syntactically based on ALGOL-60, with a pure functional
subset.
Modula-3* - Incoprporation of Modula-2* ideas into Modula-3. "Modula-3*:这样我就可以从awk语句中得到以下出口:
Autolisp
CPL
Modula-3*我尝试了以下句子,因为我想要过滤的文件是巨大的。它是到目前为止所有现有编程语言的列表,但基本上所有的行都遵循与上面相同的模式。
我迄今所用的句子:
BEGIN{$0 !~ /^ / && NF == 2 && $2 == "-"} { print $1 }
BEGIN{RS=""; ORS="\n\n"; FS=OFS="\n"} /^FLIP -/{print $1,$3}
BEGIN{RS=""; FS=OFS="\n"} {print $1 NF-1}
BEGIN{NF == 2 && $2 == "-" } { print $1 }
BEGIN { RS = "" } { print $1 } 到目前为止,对我有用的句子是:
BEGIN { RS = "\n\n"; FS = " - " }
{ print $1 }
awk -F " - " "/ - /{ print $1 }" file.txt但它仍然打印或跳过我需要/不需要的线条。
感谢您的帮助和回应!几天来,我一直心灰意冷,因为我是AWK编程的新手。
发布于 2013-08-15 05:56:45
默认的FS应该很好,为了避免任何重复的行,您可以将输出输送到sort -u。
$ gawk '$2 == "-" { print $1 }' file | sort -u
Autolisp
CPL
Modula-3*它可能不会过滤掉您想要的所有内容,但是您可以一直添加规则,直到过滤坏数据为止。
或者,可以通过使用关联数组来避免使用sort:
$ gawk '$2=="-" { arr[$1] } END { for (key in arr) print key}' file
Autolisp
CPL
Modula-3*发布于 2013-08-15 04:38:43
如果不需要使用awk,那么可能首先使用grep来选择正确形式的行,然后使用sed来修剪尾,如下所示:
grep -e '^.* -' | sed -e 's/\(^.*\) -.*$/\1\n/; p;'编辑:在玩了几次awk之后,你的问题之一似乎是,你并不总是有“语言名-东西”,而是“语言名称-\n东西”,就像示例文本中的CPL一样,因此,FS=“不会在类似的事情上分开。”
另外,可以尝试的一件事是:
BEGIN { r = "^.* -"; }
{
if (match($0, r)) {
printf("%s\n", substr($0, 1, RSTART + RLENGTH - 3));
}
}实际上,我对awk不太了解,但这是我对复制grep和sed在上面所做的最好的猜测。至少在你给出的样本文本上,它确实有效。
https://stackoverflow.com/questions/18246370
复制相似问题