给定以下文本:
2014-01-21 15:59:18.916,2014-01-21 15:59:19.232
2014-01-21 16:02:25.917,2014-01-21 16:02:28.457
我正在寻找一个将其更改为:
的尴尬语句2014-01-21 15:59:18.916,916
2014-01-21 16:02:25.917,917
with bash:
while IFS=, read -r f1 f2; do
echo "$f1,${f1##*.}"
done << END
2014-01-21 15:59:18.916,2014-01-21 15:59:19.232
2014-01-21 16:02:25.917,2014-01-21 16:02:28.457
END
2014-01-21 15:59:18.916,916
2014-01-21 16:02:25.917,917
如果您只是在逗号之后删除第二个值并添加第一个字段的毫秒,那么这应该足够:
$ awk -F"[.,]" '{print $1"."$2","$2}' file
2014-01-21 15:59:18.916,916
2014-01-21 16:02:25.917,917
它的作用是用-F"[.,]"
定义两个可能的字段分离器,然后基于该分隔打印第一,第二和第二个字段。
sed 's/.([0-9]*),.*/.1,1/' file