使用 awk 打印一列的一部分以及其他列

使用 awk 打印一列的一部分以及其他列

再会。

仅使用 Linux 操作系统工具,如何将以下文本转换为:

cat latest.csv
apsga,alp1acscf0001vm001oam001p1n004v001,192.7.129.7,UVP
apsga,alp1acscf0001vm001oam001p1n004v002,192.7.129.8,UVP
apsga,alp1acscf0001vm002oam001p1n004v001,192.7.129.9,UVP
apsga,alp1acscf0001vm003oam001p1n004v001,192.7.129.10,UVP
apsga,alp1acscf0001vm004cif001p1n004v001,192.7.129.11,UVP
apsga,alp1acscf0001vm004cif001p1n004v002,192.7.129.12,UVP
apsga,alp1acscf0001vm005cif001p1n004v001,192.7.129.13,UVP

我设法得到以下结果:

cat latest.csv`|awk -F',' '{print $2, $3}'|awk '{ $1 = substr($1,1,21); print $1","$2}'
alp1acscf0001vm001oam,192.7.129.7
alp1acscf0001vm001oam,192.7.129.8
alp1acscf0001vm002oam,192.7.129.9
alp1acscf0001vm003oam,192.7.129.10
alp1acscf0001vm004cif,192.7.129.11
alp1acscf0001vm004cif,192.7.129.12
alp1acscf0001vm005cif,192.7.129.13

我希望最终结果如下所示:

ALP1A/alp1acscf0001v,UUID,192.7.129.7,SSH2,alp1acscf0001vm001oam
ALP1A/alp1acscf0001v,UUID,192.7.129.8,SSH2,alp1acscf0001vm001oam
ALP1A/alp1acscf0001v,UUID,192.7.129.9,SSH2,alp1acscf0001vm002oam
ALP1A/alp1acscf0001v,UUID,192.7.129.10,SSH2,alp1acscf0001vm003oam
ALP1A/alp1acscf0001v,UUID,192.7.129.11,SSH2,alp1acscf0001vm004cif
ALP1A/alp1acscf0001v,UUID,192.7.129.12,SSH2,alp1acscf0001vm004cif
ALP1A/alp1acscf0001v,UUID,192.7.129.13,SSH2,alp1acscf0001vm005cif

提前致谢,

比约恩

答案1

尝试这个:

awk -F',' '{ print "ALP1A/" substr($2,1,14) ",UUID," $3 ",SSH2," substr($2,1,21) }' file

答案2

使用 GNU awk 执行 patsplit():

$ awk '
    BEGIN { FS=OFS="," }
    patsplit($2,a,/[[:alpha:]]+[0-9]+[[:alpha:]]/) {
        sub(/([0-9]+[[:alpha:]]+){3}[0-9]+$/,"",$2)
        print toupper(a[1]) "/" a[1] a[2], "UUID", $3, "SSH2", $2
    }
' latest.csv
ALP1A/alp1acscf0001v,UUID,192.7.129.7,SSH2,alp1acscf0001vm001oam
ALP1A/alp1acscf0001v,UUID,192.7.129.8,SSH2,alp1acscf0001vm001oam
ALP1A/alp1acscf0001v,UUID,192.7.129.9,SSH2,alp1acscf0001vm002oam
ALP1A/alp1acscf0001v,UUID,192.7.129.10,SSH2,alp1acscf0001vm003oam
ALP1A/alp1acscf0001v,UUID,192.7.129.11,SSH2,alp1acscf0001vm004cif
ALP1A/alp1acscf0001v,UUID,192.7.129.12,SSH2,alp1acscf0001vm004cif
ALP1A/alp1acscf0001v,UUID,192.7.129.13,SSH2,alp1acscf0001vm005cif

或者使用 GNU awk 将第三个参数设置为 match():

$ awk '
    BEGIN { FS=OFS="," }
    match($2,/(([[:alpha:]]+[0-9]+[[:alpha:]])[[:alpha:]]+[0-9]+[[:alpha:]])[[:alpha:]]+[0-9]+[[:alpha:]]+/,a) {
        print toupper(a[2]) "/" a[1], "UUID", $3, "SSH2", a[0]
    }
' latest.csv
ALP1A/alp1acscf0001v,UUID,192.7.129.7,SSH2,alp1acscf0001vm001oam
ALP1A/alp1acscf0001v,UUID,192.7.129.8,SSH2,alp1acscf0001vm001oam
ALP1A/alp1acscf0001v,UUID,192.7.129.9,SSH2,alp1acscf0001vm002oam
ALP1A/alp1acscf0001v,UUID,192.7.129.10,SSH2,alp1acscf0001vm003oam
ALP1A/alp1acscf0001v,UUID,192.7.129.11,SSH2,alp1acscf0001vm004cif
ALP1A/alp1acscf0001v,UUID,192.7.129.12,SSH2,alp1acscf0001vm004cif
ALP1A/alp1acscf0001v,UUID,192.7.129.13,SSH2,alp1acscf0001vm005cif

答案3

命令:

awk -F "," '{print toupper(substr($2,1,5))"/"substr($2,1,14)",UUID,"$(NF-1)",SSH2,"substr($2,1,20)}' filename

输出

ALP1A/alp1acscf0001v,UUID,192.7.129.7,SSH2,alp1acscf0001vm001oa
ALP1A/alp1acscf0001v,UUID,192.7.129.8,SSH2,alp1acscf0001vm001oa
ALP1A/alp1acscf0001v,UUID,192.7.129.9,SSH2,alp1acscf0001vm002oa
ALP1A/alp1acscf0001v,UUID,192.7.129.10,SSH2,alp1acscf0001vm003oa
ALP1A/alp1acscf0001v,UUID,192.7.129.11,SSH2,alp1acscf0001vm004ci
ALP1A/alp1acscf0001v,UUID,192.7.129.12,SSH2,alp1acscf0001vm004ci
ALP1A/alp1acscf0001v,UUID,192.7.129.13,SSH2,alp1acscf0001vm005ci

答案4

要求GNU sed对于跑步:

## delimiter, non-delimiter, run of 7 non-delimiters
d=, D="[^$d]" D7="(${D}{7})"
sed -Ee "
  s|^$D+,(($D7{2})$D{7})$D*,($D+),.*|ALP1A/\2,UUID,\4,SSH2,\1|
" latest.csv

相关内容