2013-02-04 81 views
3

我有一個這樣的文件:從時間刪除秒(AWK,SED)

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 '[email protected],\(..\):\(..\):\(..\) @,\1:\2 @' <time_fault> ./time_corrected 

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

但都沒有奏效。

回答

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 
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 
+0

這個作品!但我不明白如何......首先你將當前記錄$ 0分割爲一個帶分隔符「:」的數組a。對於字段Nr。大於2時,用逗號替換兩位數字和逗號?而最終的結果是什麼? – telemachos

+1

如果nr> 2,表示該行有「秒」部分,gsub用於刪除該部分。 1到底意味着,實際上是「真實的」,打印當前行,當然已經被gsub取代了。 – Kent

4

你幾乎得到了它。

sed '[email protected]\(..\):\(..\):\(..\)@\1:\[email protected]' 

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 
+0

正常工作,謝謝! – telemachos

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 
+0

工作正常,謝謝! – telemachos

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 
2

方式一:

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