我想用此结构优化文件的处理:
2014-01-21 14:26:05.900,2014-01-21 14:26:05.740, 0.000, 192.168.40.2, 192.168.40.26,6 , 8000, 33311, 172000, 2000,.A..S., 0
2014-01-21 14:29:23.900,2014-01-21 14:29:23.340, 0.000, 192.168.40.26, 192.168.40.2,6 , 33317, 8000, 3052000, 2000,.A...., 0
2014-01-21 14:30:25.900,2014-01-21 14:30:25.330, 0.000, 192.168.40.26, 192.168.40.2,17 , 36193, 514, 558000, 2000,......, 0
2014-01-21 14:31:04.901,2014-01-21 14:31:04.222, 0.000, 192.168.40.242, 192.168.40.2,17 , 57516, 514, 422000, 2000,......, 0
2014-01-21 14:31:13.900,2014-01-21 14:31:13.143, 0.000, 192.168.40.16, 192.168.40.2,17 , 53313, 514, 540000, 2000,......, 0
到具有此结构的文件:
2014-01-21 14:26:05.900,900,0.000,192.168.40.2,192.168.40.26,6,8000,33311,172000,2000,.A..S.,0
2014-01-21 14:29:23.900,900,0.000,192.168.40.26,192.168.40.2,6,33317,8000,3052000,2000,.A....,0
2014-01-21 14:30:25.900,900,0.000,192.168.40.26,192.168.40.2,17,36193,514,558000,2000,......,0
2014-01-21 14:31:04.901,901,0.000,192.168.40.242,192.168.40.2,17,57516,514,422000,2000,......,0
2014-01-21 14:31:13.900,900,0.000,192.168.40.16,192.168.40.2,17,53313,514,540000,2000,......,0
命令要优化:
sed -e 's/,s+/,/g' -i /tmp/to_filter
sed -e 's/s+,/,/g' -i /tmp/to_filter
while IFS=, read -r f1 f2 f3 f4 f5 f6 f7 f8 f9 f10; do
echo "$f1,${f1##*.},$f3,$f4,$f5,$f6,$f7,$f8,$f9,$f10"
done < /tmp/to_filter
awk 'BEGIN{FS=OFS=","} {t=$2=$1; sub(/.*./,"",$2); gsub(/ /,""); $1=t} 1' file
2014-01-21 14:26:05.900,900,0.000,192.168.40.2,192.168.40.26,6,8000,33311,172000,2000,.A..S.,0
2014-01-21 14:29:23.900,900,0.000,192.168.40.26,192.168.40.2,6,33317,8000,3052000,2000,.A....,0
2014-01-21 14:30:25.900,900,0.000,192.168.40.26,192.168.40.2,17,36193,514,558000,2000,......,0
2014-01-21 14:31:04.901,901,0.000,192.168.40.242,192.168.40.2,17,57516,514,422000,2000,......,0
2014-01-21 14:31:13.900,900,0.000,192.168.40.16,192.168.40.2,17,53313,514,540000,2000,......,0
这可能对您有用(gnu sed):
sed -r 's/^([^,.]*.([^,]*)),[^,]*/1,2/;s/s*,s*/,/g' file
编辑:
sed -r 's/.([^,]*),[^,]*/.1,1/;s/s*,s*/,/g' file
我会使用perl单线。它在每个字段上映射以删除前导和尾随空间,然后将所有字符从第二个字段中删除直到上次.
,然后与逗号连接的所有字段打印:
perl -F, -ane '
@F = map { s/As+//; s/s+Z//; $_ } @F;
$F[1] =~ s/A.*.//;
printf qq|%sn|, join q|,|, @F
' infile
它产生:
2014-01-21 14:26:05.900,900,0.000,192.168.40.2,192.168.40.26,6,8000,33311,172000,2000,.A..S.,0
2014-01-21 14:29:23.900,900,0.000,192.168.40.26,192.168.40.2,6,33317,8000,3052000,2000,.A....,0
2014-01-21 14:30:25.900,900,0.000,192.168.40.26,192.168.40.2,17,36193,514,558000,2000,......,0
2014-01-21 14:31:04.901,901,0.000,192.168.40.242,192.168.40.2,17,57516,514,422000,2000,......,0
2014-01-21 14:31:13.900,900,0.000,192.168.40.16,192.168.40.2,17,53313,514,540000,2000,......,0
使用awk
awk '{t=$1;gsub(/ /,"");split($1,a,".");$1=t;$2=a[2]}1' FS=, OFS=, file