Need to put semicolon

Hi guys,

I want to write script so that i can put semicolon after every numeric

e.g
input would be like that
50060E80058F49A4

Output should be

50:06:0E:80:05:8F:49:A4

Please help

Thanks & Regards
Nirjhar

#! /bin/bash
while read x
do
    y=''
    while [ ${#x} -gt 0 ]
    do
        y="${y}${x:0:2}:"; x=${x#??}
    done
    echo ${y%?}
done < inputfile

or

sed 's/\(..\)/\1:/g; s/:$//' inputfile
1 Like

Thanks for the reply it is properly working with the While loop...
but i want to do it with the sed and it is not giveing the right output...

I have little bit idea of sed,it would be great if u can explain the little......so i can modify the same acording to my need..

Thanks
Nirjhar

or try like this

# sed 's/../&:/g;s/.$//'

Thanks for the reply again but i am getting the minor mistake....

5:00:60:E8:00:58:F4:90: it is coming like that......:frowning:

what is your code and inputfile?

The input file is

50060E80058F4900
50060E80058F4920
50060E80058F4901
50060E80058F4921
50060E80058F4904
50060E80058F4924
50060E80058F4905
50060E80058F4925
50060E80058F4984
50060E80058F49A4
50060E80058F4985
50060E80058F49A5
50060E80058F4910
50060E80058F4930
50060E80058F4911
50060E80058F4931
50060E80058F4914
50060E80058F4934
50060E80058F4915
50060E80058F4935
50060E80058F4994
50060E80058F49B4
50060E80058F4995
50060E80058F49B5

This works:

$ sed 's/\(..\)/\1:/g; s/:$//' inputfile
50:06:0E:80:05:8F:49:00
50:06:0E:80:05:8F:49:20
50:06:0E:80:05:8F:49:01
50:06:0E:80:05:8F:49:21
50:06:0E:80:05:8F:49:04
50:06:0E:80:05:8F:49:24
50:06:0E:80:05:8F:49:05
50:06:0E:80:05:8F:49:25
50:06:0E:80:05:8F:49:84
50:06:0E:80:05:8F:49:A4
50:06:0E:80:05:8F:49:85
50:06:0E:80:05:8F:49:A5
50:06:0E:80:05:8F:49:10
50:06:0E:80:05:8F:49:30
50:06:0E:80:05:8F:49:11
50:06:0E:80:05:8F:49:31
50:06:0E:80:05:8F:49:14
50:06:0E:80:05:8F:49:34
50:06:0E:80:05:8F:49:15
50:06:0E:80:05:8F:49:35
50:06:0E:80:05:8F:49:94
50:06:0E:80:05:8F:49:B4
50:06:0E:80:05:8F:49:95
50:06:0E:80:05:8F:49:B5

what is your desired output?
is that or ?

# sed 's/../&:/g;s/.$//' file
50:06:0E:80:05:8F:49:00
50:06:0E:80:05:8F:49:20
50:06:0E:80:05:8F:49:01
50:06:0E:80:05:8F:49:21
50:06:0E:80:05:8F:49:04
50:06:0E:80:05:8F:49:24
50:06:0E:80:05:8F:49:05
50:06:0E:80:05:8F:49:25
50:06:0E:80:05:8F:49:84
50:06:0E:80:05:8F:49:A4
50:06:0E:80:05:8F:49:85
50:06:0E:80:05:8F:49:A5
50:06:0E:80:05:8F:49:10
50:06:0E:80:05:8F:49:30
50:06:0E:80:05:8F:49:11
50:06:0E:80:05:8F:49:31
50:06:0E:80:05:8F:49:14
50:06:0E:80:05:8F:49:34
50:06:0E:80:05:8F:49:15
50:06:0E:80:05:8F:49:35
50:06:0E:80:05:8F:49:94
50:06:0E:80:05:8F:49:B4
50:06:0E:80:05:8F:49:95
50:06:0E:80:05:8F:49:B5
1 Like

Hi ygemici,

Thanks for the response, I got the desired o/p.

I have also try to under stand ur code.....
sed 's/\(..\)/\1:/g; s/:$//'

i got all the things but can you tell me what is the use \1 here.
i did n't get this concept, i have tried it without \1 but than its not taking the input file.

Hope you will answer.

Regards
Nirjhar

\1 is back reference to the sub-pattern in first parenthesis of the pattern. In this case, \(..\) is the sub pattern in first parenthesis. This is referred by \1.

1 Like

Thanks for the reply. I got the idea here we use /1 to store the pattern inside it. and we can store up to 9 patterns.