用shell处理文件的时候我们常常需要去掉或者加上换行符,name问题就来了怎么才能快速的替换呢?
我们有这样一个文件
[[email protected] working]# cat 1 GD200A16C013493,GD200A16C013494,GD200A16C013495,GD200A16C013497
我们需要把逗号去掉,换成换行符可以这样做
[[email protected] working]# cat 1|sed ‘s/,/\n/g‘ GD200A16C013493 GD200A16C013494 GD200A16C013495 GD200A16C013497
但是我们反过来了呢?就是把下面的换行替换为用逗号分开,sed反过来用就不行了,
[[email protected] working]# cat 1|sed ‘s/,/\n/g‘|sed ‘s/\n/,/g‘ GD200A16C013493 GD200A16C013494 GD200A16C013495 GD200A16C013497
我们可用awk来做用也行,如下:
cat 1|sed ‘s/,/\n/g‘|awk ‘{{printf"%s,",$0}}‘ GD200A16C013493,GD200A16C013494,GD200A16C013495,GD200A16C013497
这里也可以用tr来处理,但是te来处理的话还需要手动删除最后一个逗号如下:
[[email protected] working]# cat 1|sed ‘s/,/\n/g‘|tr -s ‘\n‘ ‘,‘ GD200A16C013493,GD200A16C013494,GD200A16C013495,GD200A16C013497,[[email protected] working]# [[email protected] working]# cat 1|sed ‘s/,/\n/g‘|tr -t ‘\n‘ ‘,‘ GD200A16C013493,GD200A16C013494,GD200A16C013495,GD200A16C013497,[[email protected] working]# [[email protected] working]# cat 1|sed ‘s/,/\n/g‘|tr ‘\n‘ ‘,‘ GD200A16C013493,GD200A16C013494,GD200A16C013495,GD200A16C013497,[[email protected] working]#
时间: 2024-12-12 04:03:18