简体   繁体   中英

editing csv file

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.

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

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 "$*"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM