How to get the empty values in data using awk command

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.

1 answer

  • answered 2018-06-19 18:12 Miguel Ortiz

    Cheap solution, using printf - format and print data, ignoring empty columns and replacing the header of "Origin" with "Log" using sed:

    <lvm_command> | sed 's/Origin /Log/g' | awk '{printf "%-5s| %-10s| %10s| %10s| %10s| %10s|  \n", $1,$2,$3,$4,$5,$6}'
    

    Output example:

    LV   | VG        |       Attr|      LSize|       Pool|        Log|  
    root | centos    | -wi-ao----|     45.62g|       root|     Online|  
    swap | centos    | -wi-ao----|      3.62g|       root|    Offline| 
    

    Explanation of formatting: man 3 printf

    Not elegant but works.

    You could also avoid using sed and filter the columns of the lvs output:

    lvs -o <name_of_column, name_of_column>
    

    Possible values:

    Column names include: chunk_size, convert_lv, copy_percent, data_lv, devices, discards, lv_attr, lv_host, lv_kernel_major,  lv_ker‐
                  nel_minor,  lv_kernel_read_ahead,  lv_major,  lv_minor,  lv_name,  lv_path,  lv_profile,  lv_read_ahead, lv_size, lv_tags, lv_time,
                  lv_uuid, metadata_lv, mirror_log, modules, move_pv, origin, origin_size, pool_lv,  raid_max_recovery_rate,  raid_min_recovery_rate,
                  raid_mismatch_count,  raid_sync_action,  raid_write_behind,  region_size, segtype, seg_count, seg_pe_ranges, seg_size, seg_size_pe,
                  seg_start, seg_start_pe, seg_tags, snap_percent, stripes, stripe_size, sync_percent, thin_count, transaction_id, zero.