안녕히 가세요.
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