sumit gupta sumit gupta - 6 months ago 13
Bash Question

Insert delimiter between digits in linux

I have few random numbers which I retrieve from a database. Eg.

12203770
6458251
6458250
10336719
10366878
10366877
10366874
81048


Now, I have to place the delimiter "/" in between them. I am using this command:

[root@abc01 ~]# awk 'BEGIN{FS="";OFS="/"} {print $1,$2,$3,$4,$5}' /tmp/abc.txt


This works fine if numbers are 8 digits but my
requirement
is :
The command/script should leave last 3 digit from right and for rest, it prints digit with delimiter.

Desired output required:

1/2/2/0/3
6/4/5/8
6/4/5/8
1/0/3/3/6
1/0/3/6/6
1/0/3/6/6
1/0/3/6/6
8/1


Kindly help.

Answer
awk 'BEGIN{FS="";OFS="/"}{NF-=3;print}' file
1/2/2/0/3
6/4/5/8
6/4/5/8
1/0/3/3/6
1/0/3/6/6
1/0/3/6/6
1/0/3/6/6
8/1
Comments