Reputation: 9
We received 18,000 CSV files, each with 2 lines of text.
These two lines were supposed to be a single line, separated by a comma.
FK1QG5QL
b8:9d:7d
should appear
FK1QG5QL,b8:9d:7d
I'm trying to remove the return from the first line and replace it with a comma.
Upvotes: 0
Views: 136
Reputation: 19982
Do you have the command paste
?
for f in *; do
mv "$f" "$f.bak" && paste -sd "," "$f.bak" > "$f"
done
# test first before uncommenting next line
# rm *.bak
Without paste you can try other solutions:
head -1 input.csv | tr '\n' ',' ; tail -1 input.csv
cat input.csv | tr "\\n" "," | sed 's/,$/\n/'
printf -v oneline "%s," $(cat input.csv); echo "${oneline%,}"
sed 'N;s/\n/,/' input.csv
set -- $(cat input.csv); IFS=,; echo "$*"
Upvotes: 0
Reputation: 730
Will this one work?
for f in *.csv; do awk 'BEGIN {l=""} !/^ *$/ {if (l!="") {l=l","}; l=l$0} END {print l}' "$f"; done
You can redirect the output of the above command to a file:
for f in *.csv; do awk 'BEGIN {l=""} !/^ *$/ {if (l!="") {l=l","}; l=l$0} END {print l}' "$f"; done > combined
If you want to change every single file you can use the next version. It will create another file called *.copy:
for f in *.csv; do awk 'BEGIN {l=""} !/^ *$/ {if (l!="") {l=l","}; l=l$0} END {print l}' "$f" > "$f".copy; done
Upvotes: 1