Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Awk transpose data with multi field separators

Tags:

awk

I'm trying to do the following with just one awk command. I have a text file with the following structure

File fields description (not in file)

First fields are separated with ":", last field (host list) separated by spaces
Uniq id:Name:Uniq identifier:host_list (may be empty to many and can be repeated)

File content
sv_0:blabla:205700DD4C506261796ED3:Host_10 Host_1 Host_16
sv_111:abcd:205700DD4C50629585735C:Host_10
sv_3:xpto1:2057008E714F629B3BCDCF:Host_11 Host_10
sv_46:something:205700DD4C50629E5AB93A:
Need to convert this input to this output
sv_0 blabla 205700DD4C506261796ED3 Host_10
sv_0 blabla 205700DD4C506261796ED3 Host_1
sv_0 blabla 205700DD4C506261796ED3 Host_16
sv_111 abcd 205700DD4C50629585735C Host_10
sv_3 xpto1 2057008E714F629B3BCDCF Host_11
sv_3 xpto1 2057008E714F629B3BCDCF Host_10
sv_46 something 205700DD4C50629E5AB93A

Using the next command works, but I don't like to use pipe after pipe

awk -F: '{print $1" "$2" "$3" "$4 }' file.txt | awk '{nfield=4; while (NF >= nfield) {print $1" "$2" "$3" "$nfield; nfield++}}'

like image 518
tgreis Avatar asked Jun 14 '26 09:06

tgreis


1 Answers

With single gawk command:

awk -F'[: ]' '{ for (i=4;i<=NF;i++) print $1,$2,$3,$i }' file.txt

sv_0 blabla 205700DD4C506261796ED3 Host_10
sv_0 blabla 205700DD4C506261796ED3 Host_1
sv_0 blabla 205700DD4C506261796ED3 Host_16
sv_111 abcd 205700DD4C50629585735C Host_10
sv_3 xpto1 2057008E714F629B3BCDCF Host_11
sv_3 xpto1 2057008E714F629B3BCDCF Host_10
sv_46 something 205700DD4C50629E5AB93A
like image 189
RomanPerekhrest Avatar answered Jun 17 '26 00:06

RomanPerekhrest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!