我想达到的效果是这样的
a,b,c
→效果
a
b
c
假设`sample.txt`文本的内容如下:
a,b,c,d,e
用awk,使用如下的命令
cat sample.txt |awk 'BEGIN{RS=","}{print $1}'
或
awk 'BEGIN{RS=","}{print $1}' sample.txt
其中BEGIN{RS=","},指定了分隔的符号是逗号。RS是换行,实际上是以换行替换逗号。
执行结果:
a
b
c
d
e
不使用使用{print $1},{print $0}也是可以的。只是注意输出在屏幕上会出现一个换行。如下,
# BEGIN{RS=","}指定换行替换的符号;
# {print NR, $1,RS}依次打印输出:行号,第一列/字段内容,RS换行的指定符号
~/test$ awk 'BEGIN{RS=","}{print NR, $1,RS}' sample.txt
1 a ,
2 b ,
3 c ,
4 d ,
5 e ,
# print $1与 $0 屏幕输出效果对比看看
~/test$ awk 'BEGIN{RS=","}{print NR, $0,RS}' sample.txt
1 a ,
2 b ,
3 c ,
4 d ,
5 e
,
# NR是输出行号。$0与$1内容相同,只是屏幕输出效果略不同
~/test$ awk 'BEGIN{RS=","}{print NR, $0}' sample.txt
1 a
2 b
3 c
4 d
5 e
~/test$ awk 'BEGIN{RS=","}{print NR, $1}' sample.txt
1 a
2 b
3 c
4 d
5 e
~/test$
以上。