输入文件:
123 exx abcdef 890 hello-hi-welcome and name in-India 1 3.45 1.3538 8.773
456 hfjgt 928 aetr-new-abc-India 1 9.7392 18.1903 8.752
输出:
123,exx abcdef,890,hello-hi-welcome and name in-India,1,3.45,1.3538,8.773
456,hfjgt,928,aetr-new-abc-India,1,9.7392,18.1903,8.752
我们如何为此编写 shell 脚本呢?
答案1
我认为更改数字之前或之后的空格就足够了:
$ sed -r 's/([[:digit:]]) /\1,/g; s/ ([[:digit:]])/,\1/g' file
123,exx abcdef,890,hello-hi-welcome and name in-India,1,3.45,1.3538,8.773
456,hfjgt,928,aetr-new-abc-India,1,9.7392,18.1903,8.752
答案2
我想您想将每行中的数字和非数字项分开。
GNUawk解决方案:
awk -v FPAT='[0-9]+|[0-9]+\\.[0-9]+|[^0-9]{2,}' '{
for(i=1;i<=NF;i++) {
gsub(/^ *| *$/,"",$i); printf "%s%s",$i,(i==NF? ORS:OFS)
}
}' OFS=',' file
输出:
123,exx abcdef,890,hello-hi-welcome and name in-India,1,3.45,1.3538,8.773
456,hfjgt,928,aetr-new-abc-India,1,9.7392,18.1903,8.752