Another POSIX one:
awk -F , 'NF == 11' <file
If the line has 10 commas, then there will be 11 fields in this line. So we simply make awk
use ,
as the field delimiter. If the number of fields is 11, the condition NF == 11
is true, awk
then performs the default action print $0
.
Regular expressions and sed
aren't a great match for this kind of task, but it's fairly simple using jq
, a JSON processing tool:
$ jq -r '.list[]|[.dt, .main[]] | @csv' < data.json
1519333200,271.62,271.62,272.921,1028.3,1037.2,1028.3,88,-1.3
1519344000,271.22,271.22,272.193,1028.11,1037.04,1028.11,100,-0.98
jq
takes in a JSON data stream and filters it to extract the data you want, analogously to sed
but for structured data rather than text. This command takes each element of the "list" array (.list[]
) and plucks out the "dt" field (.dt
) of the object inside along with everything in the "main" object too (.main[]
), putting the whole lot into an array and converting that to a CSV row (@csv
).
If you're interested in particular fields, or you want to reorder them, you can list them out too:
$ jq -r '.list[]|[.dt, .main.temp_min, .main.humidity] | @csv' < data.json
1519333200,271.62,88
1519344000,271.22,100
Just list out each one you care about and ignore the rest.
If you want to get into other parts of the data, you can put that in there too: .clouds
will look into the clouds
field, or you can read the wind speed with .wind.speed
- always a dot before the name, like you were writing JavaScript. The jq
manual is quite comprehensive for whatever custom behaviour you need.
Best Answer
Try this
This will produce output like this:
Explanation:
You need to enclose each of your column using quotes
""
.First
s/|/","/g
replaces all|
with","
comma enclosed in quotes Output :var1","var2","var3 Potter, Harry","2","3
Second
s/^/"/
replaces the beginning of line with quotes where^
means beginning of lineThird
s/$/"/
replaces the end of line with quotes where$
means end of lineYou can also escape the commas in field using backslash before each comma something like this
You can read more about csv formatting here: https://www.csvreader.com/csv_format.php