I have a csv file. Its line numbers are max 25.
Headings are time(UTC)(1st field), latitude(2nd field), longitude(3rd field), depth (4th field), mag(5th field), place (14th field) and so forth
Sample data
2019-12-10T21:58:28.816Z 35.488 26.4157 57.32 5.4 35km NNE of Palaikastron, Greece
2019-12-11T11:54:27.670Z 18.6158 -67.2838 85 2.85 23km NW of San Antonio, Puerto Rico
First, I want to inset heading, including built-in variable fieldwith. Second, I want to convert time(utc) to utc+03:00 and divided into date and time of headings and to change its date format. Third, I want to extract matches between 'of' word and comma before country name for 14th field.
Desired output as headings:
Date Time Latitude Longitude Depth Mag Place
Desired output:
11.12.2019 00:58:28 35.488 26.4157 57.32 5.4 Palaikastron
11.12.2019 14:54:27 18.6158 -67.2838 85 2.85 San Antonio
time,latitude,longitude,depth,mag,magType,nst,gap,dmin,rms,net,id,updated,place,type,horizontalError,depthError,magError,magNst,status,locationSource,magSource
2019-12-06T13:04:46.931Z,-15.2838,-175.1193,10,6,mww,,50,3.512,0.81,us,us60006n19,2019-12-07T13:11:48.228Z,"164km WNW of Hihifo, Tonga",earthquake,8.4,1.9,0.08,15,reviewed,us,us
2019-12-04T20:10:03.614Z,-19.0515,169.5628,266,6,mww,,21,2.812,0.82,us,us60006m2j,2019-12-05T23:44:01.300Z,"63km NNE of Isangel, Vanuatu",earthquake,7.6,1.9,0.037,71,reviewed,us,us
2019-12-03T08:46:36.374Z,-18.5597,-70.6504,32.44,6,mww,,112,0.31,1.4,us,us70006fh7,2019-12-05T08:07:29.617Z,"37km WSW of Arica, Chile",earthquake,6.2,7.8,0.069,20,reviewed,us,us
2019-12-02T05:01:54.693Z,51.3218,-178.2425,27.33,6,mww,,104,0.862,0.97,us,us70006f6d,2019-12-07T02:09:55.119Z,"60km E of Amatignak Island, Alaska",earthquake,6.7,4.2,0.066,22,reviewed,us,us
2019-11-27T07:23:42.552Z,35.7272,23.2673,71.76,6,mww,,23,1.394,1.16,us,us70006dlt,2019-12-03T23:18:27.456Z,"41km NW of Platanos, Greece",earthquake,5.8,5.4,0.046,46,reviewed,us,us
2019-11-26T02:54:12.594Z,41.5112,19.5151,20,6.4,mww,,17,0.937,0.58,us,us70006d0m,2019-12-09T15:46:11.689Z,"16km WSW of Mamurras, Albania",earthquake,3.5,1.8,0.037,72,reviewed,us,us
2019-11-24T00:54:01.052Z,51.3809,-175.5108,20,6.3,mww,,22,0.658,0.95,us,us70006cb6,2019-12-10T01:04:03.731Z,"96km SE of Adak, Alaska",earthquake,3.9,1.8,0.05,38,reviewed,us,us
2019-11-23T12:11:16.261Z,1.6286,132.7854,10,6.1,mww,,38,4.549,1.1,us,us70006c6w,2019-11-25T21:00:33.040Z,"Papua region, Indonesia",earthquake,7.8,1.8,0.061,26,reviewed,us,us
2019-11-20T23:50:43.955Z,19.4533,101.3558,10,6.2,mww,,15,2.366,0.62,us,us70006ara,2019-12-04T05:52:37.313Z,"32km ESE of Chaloem Phra Kiat, Thailand",earthquake,6.4,1.7,0.049,40,reviewed,us,us
If I can succeed one thing, I don't do others. It is challenge for me. Guide me, please. One the one hand, have difficulty to understand to use awk
for it. On the other hand, awk
time functions are very useful. I am so confused now. Whatever I've tried, I don't make good at.
First, you'll need to pre-process the CSV input to handle the embedded comma better. Then break down the AWK into functional chunks.
This will replace the
".*,.*"
with.*@@.*
, which will make it easier for AWK.To change just the date to a new timezone, replace the first line:
The AWK script would look like:
Make sure both are executable and run
preprocess.sed sample.csv | change_date.sh | reformat.awk
Or on one line:
Although learning awk is an admirable goal, it has no built-in mechanism for parsing true CSV files (in particular, fields that may contain escaped or quoted delimiters) - and the time functions are GNU-specific and not portable.
For these reasons you may want to consider using Perl (with its Text::CSV module), Python - or my current favorite for this kind of thing, Miller. As well as providing true CSV parsing, these also provide a proper
strptime
function whereas even with GNU awk'smktime
you need to manually parse and assemble thedatespec
argument.In Miller for example, you could do the following:
If you want whitespace separated output columns, change
--csv
to--icsv --opprint
("pretty printed" tabular output - with headers) or--icsv --onidx
(simple space-separated output).Ex.
Miller is available from the Ubuntu
universe
repository.