1

I have the following file: searching for "LC"(2nd search pattern) gives more that one line output, i need to process it to provide one per line repeating 1st search pattern adjacent to it.

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:/ || /Loc/,/^$/' | 
    grep -v '^$'

A blank line is after Loc 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/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
4
  • The post doesn't show the output I am looking for, Let me know in case of any doubt.
    – Sid
    Feb 17, 2017 at 13:13
  • We can achieve it from different method, but to reduce the complexity of the entire code i would want single awk to perform this task.
    – Sid
    Feb 17, 2017 at 13:14
  • 2
    1) what have you tried? read stackoverflow.com/help/how-to-ask; 2) what does ssh have to do with this? Feb 17, 2017 at 13:24
  • Yes, please edit your post, show us what you have so far and explain which part of this is giving you trouble.
    – terdon
    Feb 17, 2017 at 13:35

2 Answers 2

2

Try this

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

Or 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 version 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
20
  • Thanks, However it doesn't give me any output.
    – Sid
    Feb 20, 2017 at 6:24
  • @Sid I have tested it on example date and it produce output as required. Try to remove trailing spaces by sed -i '/\s\+$/s///' file before
    – Costas
    Feb 20, 2017 at 6:38
  • Yes it did, actually too much data was taking a lot of time, do you think there could be any other way than awk? Also I have a small issue, as i mentioned above In few schedule entries there are more than one entry for Loc/City/Customer, So i wanted Schedule name to be repeated for each Customer as mentioned above. In addition to this I have another issue, I get these entries by egrep "Schedule Name|Active|Loc", and I just realized that it sweeps any entry more than 1 for Loc which is a problem.
    – Sid
    Feb 20, 2017 at 14:28
  • @Sid Do not use egrep at all. The awk script is enough alone. For your data sed script may be quicker a little but not too much.
    – Costas
    Feb 20, 2017 at 20:25
  • thanks for the reply, let me get the data from awk, I will have to match Schedule Name, Active and most importantly Loc and for Loc I will have to get the data till next line as single Loc will have multiple entries, so let me try
    – Sid
    Feb 21, 2017 at 5:55
-1
awk '/Schedule/{A="";S=$NF;next}/Active/{A=$NF;next}/Customer/&&A=="yes"{print $NF,S}' billing

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .