0

I have a set of csv files, one of them has a timestamp, all the other ones just have data, but they all have the same number of rows.

I would like to append the timestamp to all csv files. This is currently working but instead of writing new files with out_ in front of the name, I would simply want to overwrite the original files. If I only use $file then I only get the timestamp and not the data from the files.

#!/bin/bash
for file in *.csv;
do
    awk -F, -v var="$file" '{getline f1 < var ;print $2, f1}' OFS=, timestamp.csv >  out_$file
done
emge
  • 67
  • 5
  • If you're using GNU awk you can use its [`inplace`](https://www.gnu.org/software/gawk/manual/html_node/Extension-Sample-Inplace.html) extension. Otherwise you need to write to a new file and then rename or copy. – Barmar Jan 18 '21 at 23:52
  • I think `paste` would be easier to use here. To make `paste` overwrite the original, use `sponge` from the package *moreutils*: `paste -d, "$file" timestamp.csv | sponge "$file"`. – Socowi Jan 18 '21 at 23:56

0 Answers0