读书人

shell有关问题

发布时间: 2012-02-16 21:30:36 作者: rapoo

shell问题
源文件为:
1,ac1995dsxx, 23
2,ab1999, 32
3,ad1971, 23
4,ae1971xx, 31

x表示空格



结果1:1,ac1995ds, 23
2,ab1999, 32
3,ad1971, 23
4,ae1971, 31
就是第2列去除空格;


结果2:
1,1995, 23
2,1999, 32
3,1971, 23
4,1971, 31

就是第2列截取字符串;


问题:求实现这2个结果的shell

[解决办法]
awk -F, 'BEGIN{OFS=","} {gsub(" ", "", $2);print}' yourfile
[解决办法]
awk -F, 'BEGIN{OFS=","} {$2=substr($2,3,4);print}' yourfile
[解决办法]

Assembly code
sed 's/ \+,/,/' urfile
[解决办法]
Assembly code
sed 's/\(.*,\)..\(....\).*\(,.*\)/\1\2\3/' urfile 

读书人网 >UNIXLINUX

热点推荐