简体   繁体   中英

bash — merging and manipulation 2 files

I have 2 files of which I currently manipulate each one in awk:

======================= File 1: ===================

 0x0002 RUNNING  EXISTS foo 253 65535
 0x0003 RUNNING  EXISTS foo 252 5
 0x0004 RUNNING  EXISTS foo 251 3

I'm interested in the first field and the last 2.

Field 1: vdisk(in hex). Last two fields are the possible Cdisks for each vdisk. At least 1 must exist. the values are decimal. If the number "65535" appears, it means that the 2nd cdisk is non-existent.

I use this awk to display a user friendly table:

 awk 'BEGIN {print "vdisk cdisk  Mr_cdisk"} 
 {
      if ( $3 ~ /EXISTS|THIS_AGENT_ONLINE/ ) {
           sub("65535", "N/A")
           printf "%-11s %-6s %s\n",$1,$(NF-1),$(NF)
      }
  }' ${FILE}

Will produce this table:

vdisk  cdisk  Mr_cdisk
0x0002 253    N/A
0x0003 252    5
0x0004 1      3

======================= File 2: ===================

0x0000 Cmp cli Foo 0 SOME 0 0x0 0x0 0x0
0x0001 Cmp own Foo 1 NONE 0 0x0 0x0 0x0
0x0002 Cmp cli Foo 0 SOME 0 0x0 0x1 0x0
0x0003 Cmp own Foo 0 NONE 0 0x0 0x0 0x1
0x0004 Cmp cli Foo 0 SOME 0 0x0 0x0 0x0
0x0005 Cmp own Foo 1 NONE 0 0x1 0x0 0x0

I'm interested in the "Cmp own" lines, in which the first field is the Cdisk (in hex). The 5th field from the end (just before the SOME/NONE text), is the instance number. It's either 0 or 1. I use this awk to display a user friendly table:

awk 'BEGIN {print "cdisk(hex)  RACE_Instance"}
                    /Cmp own/ {
                         printf "%-11s %-10s\n",$1,$(NF-5)
                    }' ${FILE};

This will produce the following table:

cdisk(hex)  Instance
0x0001      1
0x0003      0
0x0005      1

++++++++++++++++++++++++++++++++++++++

What would I like to display a merged table. Preferably, directly from the original files. It should spread the first data into 2 lines (if there's more than 1 cdisk). This will be the base for the merge. Then print the Instance number, if exist per this cdisk.

vdisk(hex)  cdisk(hex)  Instance
0x0002      0x00fd      N/A
0x0003      0x00fc      N/A
0x0003      0x0005      1
0x0004      0x0001      0
0x0004      0x0003      1

I would definitely prefer a solution with awk. :)

Thanks!

EDIT : added some more info and correction to one data table.

EDIT2 : Simplified input

I couldn't figure out what the mapping is from your 2 input files to your output but this should point you in the right direction:

$ cat tst.awk
NR==FNR {
    v2c[$1] = sprintf("0x%04x",$5)
    v2m[$1] = ( $6==65535 ? "N/A" : sprintf("0x%04x",$6) )
    next
}

$1 in v2c {
    print $1, v2c[$1], $5
    print $1, v2m[$1], $5
}
$
$ awk -f tst.awk file1 file2
0x0002 0x00fd 0
0x0002 N/A 0
0x0003 0x00fc 0
0x0003 0x0005 0
0x0004 0x00fb 0
0x0004 0x0003 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM