Insert delimiter between digits in linux

2019-09-25 06:46发布

问题:

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.

回答1:

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


回答2:

$ awk '{gsub(/./,"&/"); sub(/.{7}$/,"")} 1' 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

$ sed 's#.#&/#g; s#.\{7\}$##' 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


标签: linux shell awk