awk copy info after space to new column

Viewed 71

I have data with an hour value and would like to copy just the hourly value from the date column to a new column. Given:

01/02/2021 07:00,54596894,0,SIEA
01/02/2021 09:00,55038801,0,SIEA
01/02/2021 06:00,55038845,0,SIEA
01/02/2021 02:00,78434342,0.004,SIEA
01/02/2021 15:00,78434429,0,SIEA
01/02/2021 17:00,78434838,0.005,SIEA
01/02/2021 05:00,78508079,2.715,SIEA
01/02/2021 19:00,79083881,0,SIEA
01/02/2021 12:00,82928135,0,SIEA
01/02/2021 01:00,83889954,0,SIEA

I would like the result to be:

01/02/2021 07:00,54596894,0,SIEA,07
01/02/2021 09:00,55038801,0,SIEA,09
01/02/2021 06:00,55038845,0,SIEA,06
01/02/2021 02:00,78434342,0.004,SIEA,02
01/02/2021 15:00,78434429,0,SIEA,15
01/02/2021 17:00,78434838,0.005,SIEA,17
01/02/2021 05:00,78508079,2.715,SIEA,05
01/02/2021 19:00,79083881,0,SIEA,19
01/02/2021 12:00,82928135,0,SIEA,12
01/02/2021 01:00,83889954,0,SIEA,01

I've been working with awk but have only been able to get it to give me the first part of the column. I need to get what's after the space and only two characters after the space. Copy end of string and paste to new column

awk '{n=split($1,a," "); print $0","a[n]}'

produces

01/02/2021 07:00,54596894,0,SIEA,01/02/2021
01/02/2021 09:00,55038801,0,SIEA,01/02/2021
...

I'm ok if I get all the characters after the space (though I'd prefer only to get the two immediate characters) but I can't seem to figure out how to get it to select the other side of the split.

6 Answers

No need for split. Set the field separator as : or (space) and the output field separator as ,. Then print the whole record and the 2nd field.

awk -F '[: ]' -v OFS=, '{print $0,$2}'
$ awk -F, '{split($1,a,"[ :]"); print $0 FS a[2]}' file

01/02/2021 07:00,54596894,0,SIEA,07
01/02/2021 09:00,55038801,0,SIEA,09
01/02/2021 06:00,55038845,0,SIEA,06
01/02/2021 02:00,78434342,0.004,SIEA,02
01/02/2021 15:00,78434429,0,SIEA,15
01/02/2021 17:00,78434838,0.005,SIEA,17
01/02/2021 05:00,78508079,2.715,SIEA,05
01/02/2021 19:00,79083881,0,SIEA,19
01/02/2021 12:00,82928135,0,SIEA,12
01/02/2021 01:00,83889954,0,SIEA,01

split based on two delimiters, the middle value is the hour digits.

Could you please try following. Written and tested with shown samples in GNU awk.

awk '{print $0","substr($2,1,2)}' Input_file

All awk solution are pretty good. Here is a simple sed that also does the job:

sed -E 's/.* ([0-9]{2}):.*/&,\1/' file

01/02/2021 07:00,54596894,0,SIEA,07
01/02/2021 09:00,55038801,0,SIEA,09
01/02/2021 06:00,55038845,0,SIEA,06
01/02/2021 02:00,78434342,0.004,SIEA,02
01/02/2021 15:00,78434429,0,SIEA,15
01/02/2021 17:00,78434838,0.005,SIEA,17
01/02/2021 05:00,78508079,2.715,SIEA,05
01/02/2021 19:00,79083881,0,SIEA,19
01/02/2021 12:00,82928135,0,SIEA,12
01/02/2021 01:00,83889954,0,SIEA,01

Sed alternative:

sed -E 's/(^.*[[:space:]])([[:digit:]]{2})(:.*$)/\1\2\3,\2/' file

Enable regular expression interpretation with -r -E, split the lines into 3 section specified between parenthesis and then print the first, second and third sections, followed by a comma and the third section again.

here is another (more expensive) solution

$ paste -d, file <(cut -d: -f1 file | cut -d' ' -f2)
Related