awk相当于搜索模式

时间:2017-05-01 15:33:48

标签: awk

我有以下文件:

Schedule Name:       Today

  Schedule Type:       Standard
  Active:              yes
  Effective date:      01/24/2014 11:17:05
  Client Encrypt:      no
  LC/CY/Custmr:  EU         NY  Cindy
                 BU         CA  Victor
                 GU         MI  Bob
  Include:
Schedule Name:       Tomorrow

  Schedule Type:       Standard
  Active:              yes
  Effective date:      01/26/2014 11:17:05
  Client Encrypt:      no
  LC/CY/Custmr:  MU         LA  Martha
                 EU         CA  Sam
  Include:
Schedule Name:       Yesterday

  Schedule Type:       Standard
  Active:              no
  Effective date:      01/21/2014 11:17:05
  Client Encrypt:      no
  LC/CY/Custmr:  NV         IL  Joe

  Include:

期望输出

Cindy    Today
Victor   Today
Bob      Today
Martha   Tomorrow
Sam      Tomorrow

现在我想获得Schedule Name,即今天,明天和Customer name,如果Activeyes,那么这是第4个字段。所以输出应该是:

cat billing | 
    awk '/Schedule Name/ || /Active:/ || /LC/,/^$/' | 
    grep -v '^$'

在Include之前的LC后面是一个空白行,所以我试着把所有数据都拿到我,直到你找到一个空行然后grep -v空白行,如果我尝试没有调度Schedule name和Active,它可以正常工作,但是没有&# 39;与这两个模式搜索一起工作。

我使用的代码非常慢。

for pol in `cat /tmp/Active_Policies`
do
        count=`sudo /usr/openv/admin/pollist $pol -U | awk '/LC\/CY\/Custmr:/,/Include:/' | grep -v "Include:" | wc -l`
        if [ $count -gt 0 ]
        then
                first=`sudo /usr/openv/admin/pollist $pol -U | awk '/LC\/CY\/Custmr:/,/Include:/' | grep -v "Include:" | awk '{print $4}' | head -1`
                echo "$first    $pol" >> /tmp/Policies_$(date +%m-%d-%Y)
                counter=1
                for client in `sudo /usr/openv/admin/pollist $pol -U | awk '/LC\/CY\/Custmr:/,/Include:/' | grep -v "Include:" | awk '{print $3}' | sed '1d;$d'`
                do
                        ((counter = counter + 1))
                        if [ $counter -le $count ]
                        then
                                echo "$client   $pol" >> /tmp/Policies_$(date +%m-%d-%Y)
                        fi
                done
        fi
done

1 个答案:

答案 0 :(得分:1)

这是解决问题的正确方法:

$ cat tst.awk
BEGIN { OFS="\t" }
/^[^[:space:]]/ { prt() }
NF {
    if ( /:/ ) {
        name = $0
        sub(/:.*/,"",name)
        gsub(/^[[:space:]]+|[[:space:]]+$/,"",name)
    }
    value = $0
    sub(/^[^:]+:/,"",value)
    gsub(/^[[:space:]]+|[[:space:]]+$/,"",value)

    n2v[name,++numVals[name]] = value
}
END { prt() }

function prt() {
    custFldName = "LC/CY/Custmr"
    if ( n2v["Active",1] == "yes" ) {
        sched = n2v["Schedule Name",1]
        for (valNr=1; valNr<=numVals[custFldName]; valNr++) {
            cust = n2v[custFldName,valNr]
            sub(/.*[[:space:]]/,"",cust)
            print cust, sched
        }
    }
    delete n2v
    delete numVals
}

$ awk -f tst.awk file
Cindy   Today
Victor  Today
Bob     Today
Martha  Tomorrow
Sam     Tomorrow

关键是创建一个数组(上面为n2v[]),将名称映射到值,其中“name”是每行第一个:之前的文本,相关的“值”是任意值在:之后。然后,每次点击n2v行或输入文件的末尾时,只需从Schedule数组打印您关心的值。