2

好的,我有一些文字:

=== Blah 1 ===
::Junk I wish: 2 Ignore <br/>
::More Junk: 1.2-2.7 <br/>
::ABC: [http://www.google.com (STUFF/I/Want)]<br/>
::More2: Ignore<br/>
::More Stuf 2 Ignore: N/A<br/>

=== Blah 2 ===
::Junk I wish: More 2 Ignore <br/>
::More Junk: 1.2-2.7 <br/>
::ABC: [http://www.google.com (Other/STUFF/I/Want)]<br/>
::More2: More Ignore<br/>
::More Stuf 2 Ignore: More N/A<br/>

我想输出:

Blah 1, (STUFF/I/Want)
Blah 2, (Other/STUFF/I/Want)

我已经想出了如何抓住我想要的部分线条:

gawk  '/===/ {print } /ABC/ {print $3}' file_name

这将输出以下内容:

=== Blah 1 ===
(STUFF/I/Want)]<br/>
=== Blah 2 ===
(Other/STUFF/I/Want)]<br/>

我不明白的是如何剥离我不想要的其他字符,并将其放在一行中。

4

4 回答 4

4

使用printf而不是print省略换行符,仅打印第一个块中的第二个和第三个字段,并用于sub丢弃第二个块中不需要的东西:

awk '/===/{printf "%s %s, ",$2,$3}/ABC/{sub(/].*/,"");print $3}' file
Blah 1, (STUFF/I/Want)
Blah 2, (Other/STUFF/I/Want)

如果标题是可变长度:

awk '/===/{gsub(/ ?=+ ?/,"");printf "%s, ",$0}/ABC/{sub(/].*/,"");print $3}' file
Blah 1, (STUFF/I/Want)
Blah 2, (Other/STUFF/I/Want)
于 2013-03-10T18:54:26.217 回答
3

单程。

内容script.awk

BEGIN {
    ## Characters to separate output fields
    OFS = ", "
}

## When line begins with several equal signs, remove them, both leading
## and trailing, and save the title.
$1 ~ /^=+$/ {
    gsub( /\s*=\s*/, "", $0 )
    title = $0
    next
}

## For the second field, split line with both pair of parentheses and 
## print second field.
$1 ~ /ABC/ {

    ## For GNU-Awk
    #split( $0, abc_line, /(\()|(\))/, seps )
    #printf "%s%s%s%s%s\n", title, OFS, seps[1], abc_line[2], seps[2]

    ## For Awk
    split( $0, abc_line, /(\()|(\))/ )
    printf "%s%s(%s)\n", title, OFS, abc_line[2]

}

像这样运行它:

awk -f script.awk infile

它产生:

Blah 1, (STUFF/I/Want)
Blah 2, (Other/STUFF/I/Want)
于 2013-03-10T19:00:55.573 回答
1
gawk '/===/{header=gensub(" *=== *","","g",$0)} /ABC/{abc=gensub("]<br/>","","g",$3); print header", "abc}' file_name

这可能对你有用。它将剥离的信息保存到变量中,然后打印它们。

于 2013-03-10T18:54:44.227 回答
0

有时在 awk 中,如果您寻找非正统的记录分隔符,解决方案会变得非常简单:

awk -v RS=' *=== *|[()]' '
  NR%4==2 {printf "%s, ", $0}
  NR%4==0 {print "(" $0 ")"}
'

在这里,记录分隔符===可选地用空格或左括号或右括号括起来。

于 2013-03-11T00:26:58.050 回答