Saltar al contenido

~ / archivo / stackoverflow / 50934421

How to get the empty values in data using awk command

StackOverflow · awk, sed · ayudé
  1. vivin pregunta +2 2018-06-19 17:58

    I had linux lvs command sample result set. I am trying to re-arrange the fields using AWK command.Using that I am not able to skip the empty vales in data.

    LV    VG      Attr        LSize   Pool  Origin  Data%  Meta%  Move  Log
    root  centos  -wi-ao----  45.62g        root                        Online
    swap  centos  -wi-ao----                root                        Offline
    

    I tried the following command,

    awk '{print $1, $2, $3, $4, $5, $6, $7, $8, $9, $10}' lvs.txt
    

    But Output is

    LV VG Attr LSize Pool Origin Data% Meta% Move Log
    root centos -wi-ao---- 45.62g root Online
    swap centos -wi-ao---- root   Offline
    

    My expected result must be,

    LV   | VG     | Attr       | LSize  | Pool | Origin | Data% | Meta% | Move | Log
    root | centos | -wi-ao---- | 45.62g |      | root   |       |       |      | Online
    swap | centos | -wi-ao---- |        |      | root   |       |       |      | Offline
    

    Please help me through this. Any other possible ways is also welcome.Thanks in advance.

  2. Miguel Ortiz yo respuesta 2018-06-19 18:12

    Solution, use awk with --separator and format the output with awk -F (Field Separator) and printf:

    lvs --separator ',' | awk -F ',' '{printf "%-15s| %-10s| %10s| %10s| %10s| %10s| %10s| %10s| %10s|%10s|  \n", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10}'
    

    Output:

      LV           | VG        |       Attr|      LSize|       Pool|     Origin|      Data%|      Meta%|       Move|       Log|  
      logicalTest1 | testgroup | -wi-a-----|      1.00g|           |           |           |           |           |          |  
      logicalTest2 | testgroup | -wi-a-----|      1.00g|           |           |           |           |           |          |  
    

    Explanation

    Installed lvm and created a couple of lvs to test.

    The lvs command produces an output without field separators, just a bunch of spaces. After redirecting the output to a file I've proceeded to display the spaces:

     root@florida:~# cat test2 | tr " " "*"
    **Host*******Attr*******KMaj*LSize*OSize*Origin
    **florida****-wi-a-----**254*1.00g*************
    **florida****-wi-a-----**254*1.00g*************
    

    Because spaces aren't good delimiters and after reading man lvs I found it has several options to print the output, one of them is the --separator parameter which allows you to use a personalized separator for each column, I've used a "," (comma) which is common for CSV.

    Then, looking for options to delimit awk I've found an option to use a field separator on it, I've glued that with the print formatting explained here.

    I just had to search and read a little but all the answers for this problem were on internet.

    • vivin 2018-06-20 10:37 Thanks Miguel Ortiz. But also I need the fields which holds the empty values and sometimes some fields may have values in one row and not in another rows.

← todos los hilos de StackOverflow