0

I am using Solaris SunOS 5.10. I have a directory which I have populated with a number of text files. Something like this:

CMGW9.log
CMGW11.log
CMGW2.log
.
.
CMGWx.log

I am filtering only the interesting part from a file by using:

cat CMGW9.log | nawk '/productNumber|productRevision/'

The output I get is:

productNumber                        GMPV4_CNP_R6.3.2.0D
productRevision                      R6A

Now the requirement is a csv file like following:

|MGW_Name|   Product Number   |Product Revision|
------------------------------------------------

 CMGW9   GMPV4_CNP_R6.3.2.0D|  R6A           
 CMGW11   xxxxxxxxxxxxxxxxxxx  R6A
 CMGW2    xxxxxxxxxxxxxxxxxxx  R6A
 .   .  .   .   .   .    .   .  .
 CMGWx    xxxxxxxxxxxxxxxxxxx  xx

Please help. Thanks.

1
  • That output is not a CSV file format. Are you sure you want CSV? Commented Jul 31, 2014 at 12:55

2 Answers 2

1

Here you have a part of a possible solution;

$ cat getloginfo.sh 
#!/bin/bash
for file in CMGW*.log
do
  filename=${file%.log}
  pn=`awk '$0 ~ /productNumber/  {print $2}' $file`
  pr=`awk '$0 ~ /productRevision/  {print $2}' $file`
  echo -e "$filename\t$pn\t$pr" >> CMGWresume.log
done
$ ./getloginfo.sh 
$ cat CMGWresume.log 
CMGW11  GMPV5.3.2.0D    R6AB
CMGW9   GMPV4_CNP_R6.3.2.0D R6A
Sign up to request clarification or add additional context in comments.

Comments

1

Try this one. It produces a CSV file.

awk -v OFS=, 'FNR == 1 { key = FILENAME; sub(/[.][^.]*$/, "", key); keys[++k] = key }
    /productNumber|productRevision/ { a[key, $1] = $2 }
    END {
        for (i = 1; i <= k; ++i) {
            key = keys[i]
            print key, a[key, "productNumber"], a[key, "productRevision"]
        }
    }' *.log

And this may produce the table you presented:

awk -v OFS="\t" 'FNR == 1 { key = FILENAME; sub(/[.][^.]*$/, "", key); keys[++k] = key }
    /productNumber|productRevision/ { a[key, $1] = $2 }
    END {
        print "|MGW_Name|", "Product Number", "|Product Revision|"
        print "------------------------------------------------"
        print
        for (i = 1; i <= k; ++i) {
            key = keys[i]
            print key, a[key, "productNumber"], a[key, "productRevision"]
        }
    }' *.log

You can use printf instead of print to change the formatting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.