Remove seconds from time (awk, sed)
I have a file like this:
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
So the time format is not consistent. Some lines have a time like hh: mm: ss, and some have a hh: mm time format. I would like to remove seconds and get the file like this:
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
What I have tried so far is
#!/bin/bash
sed 's@,\(..\):\(..\):\(..\) @,\1:\2 @' < time_fault > ./time_corrected
and
#!/usr/bin/awk -f
BEGIN { RS="," ; FS=":"; ORS=","}
{ getline str
gsub(/*..:..:..*/, $1":"$2 str) > time_corrected }
but both didn't work.
+3
telemachos
source
to share
6 answers
C sed
requires only one capture group:
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
Maybe awk
better .. apply only the replacement in the third field, if necessary, remove even more extra spaces from the fourth:
$ 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
Chris seymour
source
to share
You almost got it.
sed 's@\(..\):\(..\):\(..\)@\1:\2@'
gives
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
Lev Levitsky
source
to share
check this if it's ok for you:
based on your example input, this should work:
awk -F, 'split($0,a,":")>2{gsub(/:[0-9][0-9],/,",")}1' file
Test
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
Kent
source
to share
I changed your command a bit 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
+2
dogbane
source
to share
One of the methods:
awk -F, '{$3=substr($3,0,16);}1' OFS=, file
+2
Guru
source
to share
awk -F, '{OFS=","}length($3)>16{$3=substr($3,0,16)}1' your_file
checked below:
> 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
+1
Vijay
source
to share