~ / archivo / stackoverflow / 50934421
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 OfflineI tried the following command,
awk '{print $1, $2, $3, $4, $5, $6, $7, $8, $9, $10}' lvs.txtBut 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 OfflineMy 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 | | | | OfflinePlease help me through this. Any other possible ways is also welcome.Thanks in advance.
-
Solution, use
awkwith--separatorand format the output withawk -F(Field Separator) andprintf: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 lvsI found it has several options to print the output, one of them is the--separatorparameter 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
awkI'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.