1

For an input file named Lab1:

034023  052030
034023  022130
044023  012030
034223  022030
034123  152030
024023  152030

AWK command awk 'gsub(/[0-9][0-9]/,"&:",$1) gsub(/[0-9][0-9]/,"&:",$2)' Lab1

results in:

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:

How can I prevent the trailing colons?

desired result

    03:40:23 05:20:30
    03:40:23 02:21:30
  • 1
    sed -r 's/([0-9]{2})([0-9]{2})\>/:\1:\2/g' – Costas Jan 11 '16 at 12:11
  • I am not sure I understand what you mean. Could you include the desired result? – joepd Jan 11 '16 at 12:12
  • I want to use awk @Costas – asadz Jan 11 '16 at 12:17
  • @joepd see update – asadz Jan 11 '16 at 12:17
  • Even shorter sed -r 's/[0-9]{2}\B/&:/g' – Costas Jan 11 '16 at 12:39
4
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'

produces:

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

Other scripts are
1.

#!/usr/bin/awk -f
gsub(/[0-9]{4}\>/,":&") &&
gsub(/:[0-9][0-9]/,"&:")

2.

#!/usr/bin/awk -f
gsub(/[0-9]{2}\B/,"&:")

3.

#!/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))
}
  • You are awesome.+1 – rɑːdʒɑ Jan 12 '16 at 3:32
  • +1 excellent work but I'm confused on 4 in code substr($0,5,6) it brings results as 23 05 why? – asadz Jan 12 '16 at 6:24
  • @asadz I do not understand the question. What do you mean "why"? We need not change the end of 1st word so as beginning of second ones so it's output unchanged. – Costas Jan 12 '16 at 11:47
  • @Costas sorry for confusion I want to know why substr($0,5,6) when run separately matches 23 05 it should only match 23.Can you please explain? – asadz Jan 12 '16 at 15:08
  • 1
    @asadz there are 2 spaces between 23 and 15 (for 024023 152030 so it starts from 5th symbol 2 and than 3 space space 1 5 == totally 6 symbols) – Costas Jan 12 '16 at 21:26
1

Best way I got for now but this is not only way

[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]#
  • +1 s/://1 over here 1 matches the first occurrence? – asadz Jan 11 '16 at 16:06
  • @asadz Yes I am applying it only on 1st occurrence and 4th occurrence which is eventually 3rd occurrence after 1st occurrence changes with sed. – rɑːdʒɑ Jan 12 '16 at 3:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.