adding text and inserting a literal slash between two columns

50 Views Asked by At

I am capturing the LVs and VGs.

$ lvs 
  LV              VG              Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root            rhel            -wi-ao---- <12.81g                                                    
  swap            rhel            -wi-ao----   1.60g                                                    
  lv_hana_backups vg_hana_backups -wi-ao---- <10.00g                                                    
  lv_hana_data    vg_hana_data    -wi-ao---- <10.00g                                                    
  lv_hana_log     vg_hana_log     -wi-ao---- <10.00g                                                    
  lv_hana_shared  vg_hana_shared  -wi-ao---- <10.00g

Piping lvs output into awk piping into tail to only return the columns and lines I need.

lvs | awk '{print $2$1}' | tail -n +4

Which outputs...

  vg_hana_datalv_hana_data
  vg_hana_loglv_hana_log
  vg_hana_sharedlv_hana_shared

I want to insert the text /dev/ at the beginning of column2 for each line and insert a literal slash '/' between the column2 and column1, so it reads like:


/dev/vg_hana_backups/lv_hana_backups
/dev/vg_hana_data/lv_hana_data
/dev/vg_hana_log/lv_hana_log
/dev/vg_hana_shared/lv_hana_shared

How would I do that?

3

There are 3 best solutions below

0
markp-fuso On

Using the following to simulate OP's lvs output:

$ cat lvs.out
  LV              VG              Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root            rhel            -wi-ao---- <12.81g                                    
  swap            rhel            -wi-ao----   1.60g                                    
  lv_hana_backups vg_hana_backups -wi-ao---- <10.00g                                    
  lv_hana_data    vg_hana_data    -wi-ao---- <10.00g                                    
  lv_hana_log     vg_hana_log     -wi-ao---- <10.00g                                    
  lv_hana_shared  vg_hana_shared  -wi-ao---- <10.00g

One awk idea to replace OP's current awk ... | tail ...:

cat lvs.out | awk 'NR>=4 { print "/dev/" $2 "/" $1 }'

NOTE: OP will replace cat lvs.out | with lvs |

This generates:

/dev/vg_hana_backups/lv_hana_backups
/dev/vg_hana_data/lv_hana_data
/dev/vg_hana_log/lv_hana_log
/dev/vg_hana_shared/lv_hana_shared
1
dmayilyan On

I came up with this solution:

cat foo | awk '{print "/dev/"$2"/"$1}' | tail -n +4

outputs

/dev/vg_hana_backups/lv_hana_backups
/dev/vg_hana_data/lv_hana_data
/dev/vg_hana_log/lv_hana_log
/dev/vg_hana_shared/lv_hana_shared
0
Daweo On

How would I do that?

I suggest taking look at printf as you want just text as-is inside string simple %s will suffice for your needs, let command output be

  LV              VG              Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root            rhel            -wi-ao---- <12.81g                                                    
  swap            rhel            -wi-ao----   1.60g                                                    
  lv_hana_backups vg_hana_backups -wi-ao---- <10.00g                                                    
  lv_hana_data    vg_hana_data    -wi-ao---- <10.00g                                                    
  lv_hana_log     vg_hana_log     -wi-ao---- <10.00g                                                    
  lv_hana_shared  vg_hana_shared  -wi-ao---- <10.00g

then

command | awk 'NR>3{printf("/dev/%s/%s\n",$2,$1)}'

output is

/dev/vg_hana_backups/lv_hana_backups
/dev/vg_hana_data/lv_hana_data
/dev/vg_hana_log/lv_hana_log
/dev/vg_hana_shared/lv_hana_shared

Explanation: for lines after 3rd line use formatting print with 2 placeholders (%s) to output desired text. Keep in mind to use \n as printf does not add it automatically as opposed to print.

(tested in GNU Awk 5.1.0)