Insert delimiter between digits in linux

2019-09-25 06:37发布

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.

标签: linux shell awk
2条回答
叛逆
2楼-- · 2019-09-25 07:13
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
查看更多
家丑人穷心不美
3楼-- · 2019-09-25 07:23
$ 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
查看更多
登录 后发表回答