awk 或 sed 根据搜索模式每行显示一个输出

awk 或 sed 根据搜索模式每行显示一个输出

我有以下文件:搜索“LC”(第二个搜索模式)给出了不止一行输出,我需要对其进行处理以提供每行一个重复与其相邻的第一个搜索模式。

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 Nameie 今天,明天以及Customer name第四个字段 if Activeis yes。所以输出应该是:

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

在包含之前,Loc 之后有一个空行,所以我试图获取所有数据,直到找到一个空行,然后 grep -v 空行,如果我尝试不使用 awking Schedule name 和 Active,它可以正常工作,但不能一起工作通过这 2 个模式搜索。

我正在使用下面的代码,速度非常慢。

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

答案1

尝试这个

awk '
    BEGIN{OFS = "\t"}
    /Schedule Name:/{s = $NF}
    /Active:/{a = $2}
    /:|^$/&&!/LC\//{next}
    a == "yes"{print $NF, s}
    ' file

或者sed

sed '
    /Schedule Name:/! d
    s/.*:\s\+//
    :1
    N
    /Active:/! b1
    /yes/! d
    :2
    $! N
    /Include:/d
    /LC\//!{/:\|^$/b2;}
    s/\s*\n.*\s\(\S\+\)\s*/\n\1/
    s/\(.*\)\n\(.*\)/\2\t\1/p
    s/.*\t//
    t2
    ' file

sed版本2:

sed -n '
    /Schedule Name:/! d
    s/.*:\s\+//
    h
    :1
    n
    /Active:\s*no/d
    /LC\//!b1
    :2
    s/.*\s\(\S*\)\s*/\1/
    G
    s/\n/\t/p
    n
    /^\s*$\|Include:/! b2
    ' file

答案2

awk '/Schedule/{A="";S=$NF;next}/Active/{A=$NF;next}/Customer/&&A=="yes"{print $NF,S}' billing

相关内容