awk만 사용하여 다음 텍스트를 어떻게 변환할 수 있습니까?
$ cat latest.csv
psga,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
나는 다음과 같은 결과를 얻었습니다.
$ awk -F',' '{ print substr($2, 1, 14), (substr($2, 1, 18)),$3 }' latest.csv
alp1acscf0001v alp1acscf0001vm001 192.7.129.7
alp1acscf0001v alp1acscf0001vm001 192.7.129.8
alp1acscf0001v alp1acscf0001vm002 192.7.129.9
alp1acscf0001v alp1acscf0001vm003 192.7.129.10
alp1acscf0001v alp1acscf0001vm004 192.7.129.11
alp1acscf0001v alp1acscf0001vm004 192.7.129.12
alp1acscf0001v alp1acscf0001vm005 192.7.129.13
하지만 난 첫 번째를 원해substr($2, 1, 14)
줄에 한 번만나머지는 한 줄에 다음과 같습니다.
alp1acscf0001v alp1acscf0001vm001 192.7.129.7 alp1acscf0001vm001 192.7.129.8 alp1acscf0001vm002 192.7.129.9 alp1acscf0001vm003 192.7.129.10 alp1acscf0001vm004 192.7.129.11 alp1acscf0001vm004 192.7.129.12 alp1acscf0001vm005 192.7.129.13
답변1
awk -F, '
{ printf "%s %s %s", (NR==1 ? substr($2, 1, 14) : ""), substr($2, 1, 18), $3 }
END{ print "" }
' latest.csv
이는 첫 번째 입력 레코드의 첫 번째 하위 문자열만 인쇄합니다. $3
모든 레코드에 대해 또 다른 하위 문자열 플러스를 인쇄합니다. END
블록 내에서 개행 문자를 인쇄합니다.
답변2
주문하다:
awk -F "," 'BEGIN{print "alp1acscf0001v"}ORS=" "{print substr($2,1,18),$3}' filename |perl -pne "s/\n/ /g"
산출
alp1acscf0001v alp1acscf0001vm001 192.7.129.7 alp1acscf0001vm001 192.7.129.8 alp1acscf0001vm002 192.7.129.9 alp1acscf0001vm003 192.7.129.10 alp1acscf0001vm004 192.7.129.11 alp1acscf0001vm004 192.7.129.12 alp1acscf0001vm005 192.7.129.13