Friday, December 11, 2009

Inserting character at some location using sed

Inserting Colon ":" after every 2nd digit

Before insertion: 0022938bdea4
After insertion: 00:22:93:8b:de:a4

Method 1: This will also insert Colon in the end
sed 's/\(..\)/\1:/g' <<<0022938bdea4

00:22:93:8b:de:a4:


Method 2: This will work fine without doing the ending colon.
sed -e 's/\(..\)/\1:/g' -e 's/.$//' <<<0022938bdea4
00:22:93:8b:de:a4

Method 3: Same as Method 2
a=$(sed 's/\(..\)/\1:/g' <<<0022938bdea4); echo "${a:0:${#a}-1}"
00:22:93:8b:de:a4

Doing it on a file containing the data

while read file; do sed -e 's/\(..\)/\1:/g' -e 's/.$//' <<<"$file";done < mac_addresses.txt

No comments:

Post a Comment