Lab1이라는 입력 파일의 경우:
034023 052030
034023 022130
044023 012030
034223 022030
034123 152030
024023 152030
AWK 명령
awk 'gsub(/[0-9][0-9]/,"&:",$1) gsub(/[0-9][0-9]/,"&:",$2)' Lab1
결과 :
03:40:23: 05:20:30:
03:40:23: 02:21:30:
04:40:23: 01:20:30:
03:42:23: 02:20:30:
03:41:23: 15:20:30:
02:40:23: 15:20:30:
후행 콜론을 방지하는 방법은 무엇입니까?
원하는 결과
03:40:23 05:20:30
03:40:23 02:21:30
답변1
awk '
{
for(i=1;i<=NF;i++){
sub(/[0-9]{4}$/,":&",$i)
sub(/:[0-9]{2}/,"&:",$i)
}
}
1
' <<<\
'034023 052030
034023 022130
044023 012030
034223 022030
034123 152030
024023 152030'
생산하다:
03:40:23 05:20:30
03:40:23 02:21:30
04:40:23 01:20:30
03:42:23 02:20:30
03:41:23 15:20:30
02:40:23 15:20:30
1은 다른 스크립트용입니다
.
#!/usr/bin/awk -f
gsub(/[0-9]{4}\>/,":&") &&
gsub(/:[0-9][0-9]/,"&:")
2.
#!/usr/bin/awk -f
gsub(/[0-9]{2}\B/,"&:")
삼.
#!/usr/bin/awk -f
BEGIN{
FS=OFS=""
}
/[0-9]{6} [0-9]{6}/{
$3=":"$3
$4=$4":"
$11=":"$11
$12=$12":"
print
}
4.
#!/usr/bin/awk -f
/[0-9]{6} [0-9]{6}/{
printf("%02d:%d:%s:%d:%d\n",
substr($0,0,2),
substr($0,3,2),
substr($0,5,6),
substr($0,11,2),
substr($0,13,2))
}
답변2
지금까지 내가 가진 가장 좋은 방법이지만 이것이 유일한 방법은 아닙니다.
[root@virt03 test]# awk 'gsub(/[0-9][0-9]/,":&",$1) gsub(/[0-9][0-9]/,":&",$2)' lab1 | sed 's/://1' | sed 's/://3'
03:40:23 05:20:30
03:40:23 02:21:30
04:40:23 01:20:30
03:42:23 02:20:30
03:41:23 15:20:30
02:40:23 15:20:30
[root@virt03 test]# cat lab1
034023 052030
034023 022130
044023 012030
034223 022030
034123 152030
024023 152030
[root@virt03 test]#