Its much more simple, easier and faster with sed
and xargs
. Here sed
uses in-place editing thus avoiding additional shell tools.
$ ls file{1..5}.txt|xargs -I% sed -i 's/$/;%/' %
And here is the output.
$ cat file{1..5}.txt
ColVal_1;ColVal_2;ColVal_3;file1.txt
ColVal_4;ColVal_5;ColVal_6;file1.txt
ColVal_1;ColVal_2;ColVal_3;file2.txt
ColVal_4;ColVal_5;ColVal_6;file2.txt
ColVal_1;ColVal_2;ColVal_3;file3.txt
ColVal_4;ColVal_5;ColVal_6;file3.txt
ColVal_1;ColVal_2;ColVal_3;file4.txt
ColVal_4;ColVal_5;ColVal_6;file4.txt
ColVal_1;ColVal_2;ColVal_3;file5.txt
ColVal_4;ColVal_5;ColVal_6;file5.txt
... | jq -r '.rows[] | .[] | [.name, .["cpu.total"], .["memory.total"]] | map(tostring) | join(",")'
This:
- Expands the array in
.rows
into the output stream (.rows.[]
).
- Pipes that stream into the next step (
|
).
- Expands the object it's given into the (in this case) single value it contained (
.[]
).
- Creates an array with the results of
.name
, .["cpu.total"]
, and .["memory.total"]
each evaluated on that object (.[ .name, ... ]
).
- Converts all the values of that array into strings (
map(tostring)
).
- Joins the elements of each array with a comma (
join(",")
).
jq -r
outputs raw data, rather than quoting and escaping it. The output is then:
foo-01,12,16267368kB
foo-02,12,16264296kB
as you wanted. Depending on your CSV parser & the real data, you might need extra quoting around the strings, which you can add in, or use @csv
in place of the last two steps.
... | jq -r '.rows[] | .[] | [.name, .["cpu.total"], .["memory.total"]] | @csv'
We could skip the map
by converting only the one value inside, which takes some extra brackets:
... | jq -r '.rows[]|.[]|[.name, (.["cpu.total"] | tostring), .["memory.total"]] | join(",")'
And probably the ugliest alternative:
... | jq -r '.rows[]|to_entries|.[]|.key + "," + (.value["cpu.total"] | tostring) + "," + .value["memory.total"]'
In this case, we don't rely on the .name
field, and build up the whole string manually. If you need a highly customised format, this is the most flexible option.
Best Answer
That's pretty easy:
1d
will delete caption line.s
command will modify the string like in your examplee
in the end ofs
command will execute the string. this is GNU extension, so if you don't have GNU sed, you can usexargs
insteade
: