Sid
Sid

Reputation: 171

awk equivalent for search of a pattern

I have the following file:

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:

Desired Output

Cindy    Today
Victor   Today
Bob      Today
Martha   Tomorrow
Sam      Tomorrow

Now I want to get Schedule Name i.e Today, Tomorrow along with Customer name which is the 4th field if Active is yes. So the output should be:

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

A blank line is after LC before Include, So I am trying get me all data till you find a blank line and then grep -v blank line, It works fine if I try without awking Schedule name and Active, but doesn't work along with these 2 patter searches.

I am using below code which is pretty slow.

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

Upvotes: 0

Views: 113

Answers (1)

Ed Morton
Ed Morton

Reputation: 203607

Here's the right way to approach your problem:

$ 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

The key is to create an array (n2v[] above) that maps names to values where a "name" is the text before the first : on each line and the associated "value" is whatever comes after that :. Then just print the values you care about from the n2v array every time you hit a Schedule line or the end of the input file.

Upvotes: 1

Related Questions