for name in anyname*.csv; do
cat header.csv "$name" >"${name%.csv}c.csv"
done
Here, anyname*.csv
is some shell pattern, possibly including a directory path that matches the names of the CSV files to which you want to prepend the contents of header.csv
. Ideally, this pattern should not match the name header.csv
. If it is, consider moving it to some other place, renaming it, or inserting an explicit test to skip it:
for name in anyname*.csv; do
[ "${name##*/}" = header.csv ] && continue
cat header.csv "$name" >"${name%.csv}c.csv"
done
or, in the bash
or zsh
shell,
for name in anyname*.csv; do
[ "$name" -ef header.csv ] && continue
cat header.csv "$name" >"${name%.csv}c.csv"
done
(Or don't bother with it and delete the headerc.csv
file that may be generated.)
The loop iterates over the matched names and creates the new files by concatenating the header with the CSV records, creating a file whose name will be the same as the original file's but with the .csv
file name suffix replaced by c.csv
.
The standard expansion ${var%pattern}
expands to $var
, but with the shortest suffix string matching pattern
removed. The expansion ${var##pattern}
is also standard and removes the longest matching prefix string from $var
. One example above uses this to remove any directory path from $name
.