删除时间秒(awk,sed)

时间:2013-02-04 11:00:34

标签: bash unix sed awk gnu

我有一个这样的文件:

XX1, 1.1,24.08.1994 13:00:00, 111,112,113
XX2, 1.2,24.08.1994 13:30:00, 121,122,123
XX3, NaN,22.08.1995 15:00,    131,132,133

所以时间格式不一致。有些行的时间类似于hh:mm:ss,有些行的时间格式为hh:mm。我想删除秒并得到这样的文件:

XX1, 1.1,24.08.1994 13:00, 111,112,113
XX2, 1.2,24.08.1994 13:30, 121,122,123
XX3, NaN,22.08.1995 15:00, 131,132,133

我到目前为止尝试的是

#!/bin/bash
sed 's@,\(..\):\(..\):\(..\) @,\1:\2 @' < time_fault > ./time_corrected

#!/usr/bin/awk -f
BEGIN { RS="," ; FS=":"; ORS=","}
{ getline str
gsub(/*..:..:..*/,  $1":"$2 str) > time_corrected }

但两者都不起作用。

7 个答案:

答案 0 :(得分:4)

你几乎得到了它。

sed 's@\(..\):\(..\):\(..\)@\1:\2@'

给出

XX1, 1.1,24.08.1994 13:00, 111,112,113
XX2, 1.2,24.08.1994 13:30, 121,122,123
XX3, NaN,22.08.1995 15:00,    131,132,133

答案 1 :(得分:2)

如果可以的话,请检查一下:

根据您的示例输入,这应该有效:

awk -F, 'split($0,a,":")>2{gsub(/:[0-9][0-9],/,",")}1' file

测试

kent$  echo "XX1, 1.1,24.08.1994 13:00:00, 111,112,113
XX2, 1.2,24.08.1994 13:30:00, 121,122,123
XX3, NaN,22.08.1995 15:00,    131,132,133"|awk -F, 'split($0,a,":")>2{gsub(/:[0-9][0-9],/,",")}1'
XX1, 1.1,24.08.1994 13:00, 111,112,113
XX2, 1.2,24.08.1994 13:30, 121,122,123
XX3, NaN,22.08.1995 15:00,    131,132,133

答案 2 :(得分:2)

使用sed只需要一个捕获组:

sed -re 's/([0-9]{2}:[0-9]{2}):[0-9]{2},/\1,/' -e 's/, +/, /g' file
XX1, 1.1,24.08.1994 13:00, 111,112,113
XX2, 1.2,24.08.1994 13:30, 121,122,123
XX3, NaN,22.08.1995 15:00, 131,132,133

也许awk更好..如果需要,只在第三个字段上应用替换,否则从第四个字段中删除多余的空格:

$ awk '{if ($3~/([0-9]{2}:){2}/) sub(/:[0-9]{2},/,",",$3);else sub(/ */,"",$4)}1'
XX1, 1.1,24.08.1994 13:00, 111,112,113
XX2, 1.2,24.08.1994 13:30, 121,122,123
XX3, NaN,22.08.1995 15:00, 131,132,133

答案 3 :(得分:2)

我稍微调整了你的sed命令:

$ sed 's/ \(..:..\)[^,]*/ \1/g' file
XX1, 1.1,24.08.1994 13:00, 111,112,113
XX2, 1.2,24.08.1994 13:30, 121,122,123
XX3, NaN,22.08.1995 15:00,    131,132,133

答案 4 :(得分:2)

一种方式:

 awk -F, '{$3=substr($3,0,16);}1' OFS=, file

答案 5 :(得分:1)

awk -F, '{OFS=","}length($3)>16{$3=substr($3,0,16)}1' your_file

测试如下:

> cat temp
XX1, 1.1,24.08.1994 13:00:00, 111,112,113
XX2, 1.2,24.08.1994 13:30:00, 121,122,123
XX3, NaN,22.08.1995 15:00,    131,132,133

> awk -F, '{OFS=","}length($3)>17{$3=substr($3,0,16)}1' temp
XX1, 1.1,24.08.1994 13:00, 111,112,113
XX2, 1.2,24.08.1994 13:30, 121,122,123
XX3, NaN,22.08.1995 15:00,    131,132,133

答案 6 :(得分:0)

回答非常具体的标题:“从时间中删除秒数(awk,sed)”

删除 :.. 模式的第二次出现对我有用:

是: 13:06:20

sed 's/:../ /2'

现在: 13:06