Merge multiple rows into single row then append to csv output file
I have multiple comma-separated files that I need first to merge the rows into a single row then I can append that row to append to one output.csv file.
file-1.txt:
CV-1999-0001,
CV-1999-0002,
CV-1999-0003,
CV-1999-0004,
CV-1999-0005,
...
file-2.txt:
CV-2000-0006,
CV-2000-0007,
CV-2000-0008,
....
output.csv:
IDs
CV-1999-0001,CV-1999-0002,CV-1999-0003,CV-1999-0004,CV-1999-0005
CV-2000-0006,CV-2000-0007,CV-2000-0008,CV-2000-0009,CV-2000-0010
I have tried these but none seem to achieve what I need. I still end up with multiple rows
echo $(cat /Users/...../"$ROWS".txt) | sed 's/ //g' > "$ROWS_new".txt
echo $(cat paste -s -d "" files-1.txt) | sed 's/ //g' >> output.csv
Do you try:
for ROW in ${ROWS}; do
tr -d "n" < "/Users/...../${ROW}.txt"
| sed -e 's/,$//'
done >> "output.txt"
With GNU awk for multi-char RS and reading each file into memory:
$ awk -v RS='^$' -F',n' -v OFS=',' '{$1=$1}1' file-1.txt file-2.txt
CV-1999-0001,CV-1999-0002,CV-1999-0003,CV-1999-0004,CV-1999-0005,
CV-2000-0006,CV-2000-0007,CV-2000-0008,
or with GNU awk for multi-char RS and ENDFILE reading 1 line at a time:
$ awk -v RS=',n' -v ORS=',' '1; ENDFILE{printf "n"}' file-1.txt file-2.txt
CV-1999-0001,CV-1999-0002,CV-1999-0003,CV-1999-0004,CV-1999-0005,
CV-2000-0006,CV-2000-0007,CV-2000-0008,
or with any awk reading 1 line at a time:
$ awk '
FNR==1 { printf "%s%s", prev, ors; ors=ORS }
FNR>1 { printf "%s", prev }
{ prev=$0 }
END { print prev }
' file-1.txt file-2.txt
CV-1999-0001,CV-1999-0002,CV-1999-0003,CV-1999-0004,CV-1999-0005,
CV-2000-0006,CV-2000-0007,CV-2000-0008,
If the last line of your input files end in ,
it’d be easy to remove it but since the sample input in your question ends with ...
we don’t know if your input files really end with ,
s or not so I left that as an exercise.
With a sed
that offers the -z
(--null-data; separate lines by NUL characters
) option, and thus reads the entire input stream, try
sed -z 's/n//g; s/r/n/' file1 <(echo $'r') file2
CV-1999-0001,CV-1999-0002,CV-1999-0003,CV-1999-0004,CV-1999-0005,
CV-2000-0006,CV-2000-0007,CV-2000-0008,
Without that option, try
sed -f chabo.sed file1 <(echo $'r') file2
CV-1999-0001,CV-1999-0002,CV-1999-0003,CV-1999-0004,CV-1999-0005,
CV-2000-0006,CV-2000-0007,CV-2000-0008,
with chabo.sed being
:L
N
$!bL
s/n//g
s/^M/
/
and ^M
the <cr>
(0x0D
) character.
What’s wrong with this solution?
╰─ → $ paste -sd " " file1.txt file2.txt
CV-1999-0001, CV-1999-0002, CV-1999-0003, CV-1999-0004, CV-1999-0005,
CV-2000-0006, CV-2000-0007, CV-2000-0008,