I have text file contain strings like this
abc,def,ghi,jkl,mno,pqr,stu,wxyz
I want to make it like this
1.abc
2.def
3.ghi
4.jkl
5.mno
6.pqr
7.stu
8.wxyz
how can i do that using sed?
I have text file contain strings like this
abc,def,ghi,jkl,mno,pqr,stu,wxyz
I want to make it like this
1.abc
2.def
3.ghi
4.jkl
5.mno
6.pqr
7.stu
8.wxyz
how can i do that using sed?
You can do this
echo abc,def,ghi,jkl,mno,pqr,stu,wxyz | sed 's/,/\n/g' | nl -s "."
1.abc
2.def
3.ghi
4.jkl
5.mno
6.pqr
7.stu
8.wxyz
You can use GNU awk
:
awk -v RS=',|\n' '{printf "%s.%s\n",NR,$0}' <<< "abc,def,ghi,jkl,mno,pqr,stu,wxyz"
A couple of Perl approaches:
$ perl -F, -lne 'print ++$k.".$_" for @F' file
1.abc
2.def
3.ghi
4.jkl
5.mno
6.pqr
7.stu
8.wxyz
$ perl -pne 'chomp;s/([^,]+),*/++$k.".$1\n"/ge' file
1.abc
2.def
3.ghi
4.jkl
5.mno
6.pqr
7.stu
8.wxyz
And awk
:
$ awk -F, '{for(i=1;i<=NF;i++){print i"."$i}}' file
1.abc
2.def
3.ghi
4.jkl
5.mno
6.pqr
7.stu
8.wxyz
$ <file sed 's/,/\n/g' | sed '=' | paste -d . - -
1.abc
2.def
3.ghi
4.jkl
5.mno
6.pqr
7.stu
8.wxyz