Quick and dirty trick with your current command:
(cat file.csv | tr -d ' \t\n\r' | tr '|' '\n' |sed "s/.$//" > file2.csv && mv file2.csv file.csv)
Explanations:
Adding && mv file2.csv file csv to your command will trigger the move operation if and only if the first command finished successfully.
This being said, your current command should be simplified!!! Example: avoid using cat and pipe when you can redirect stdin
(tr -d ' \t\n\r' < file.csv | tr '|' '\n' |sed "s/.$//" > file2.csv && mv file2.csv file.csv)
After the edit let me introduce you a inline command that will modify the file directly without creating any intermediate file, for this purpose I use sed
Input:
$ more file.csv 330000, 200000, , , xbdcb, rrrrrr, N, N, 2018-06-14,N,|, 330000, 200000, , , xbdcb, rrrrrr, N, N, 2018-06-14,N,|, 330000, 200000, , , xbdcb, rrrrrr, N, N, 2018-06-14,N,|, 330000, 200000, , , xbdcb, rrrrrr, N, N, 2018-06-14,N,|,
Command:
$ sed -n -i.bak 'h;n;H;n;x;s/[\n ]//g;s/,|,$//;p' file.csv
Edited file:
$ more file.csv 330000,200000,,,xbdcb,rrrrrr,N,N,2018-06-14,N 330000,200000,,,xbdcb,rrrrrr,N,N,2018-06-14,N 330000,200000,,,xbdcb,rrrrrr,N,N,2018-06-14,N 330000,200000,,,xbdcb,rrrrrr,N,N,2018-06-14,N
Explanations:
-n option is used to unable the default printing of sed -i.bak is to modify the file in-line and to take a backup .bak file if you are sure about what you do change this to -i, sed will directly modify the file without taking any backup h;n; -> h put the content of current line into hold buffer and go to next line n H;n; -> H append the current line to the hold buffer and go to next line x; exchange the pattern buffer and the hold buffer to perform the modification and the printing it s/[\n ]//g remove all spaces and EOL from the pattern buffer s/,|,$// remove the ,|, at the end of the line p print the pattern buffer
Last but not least, in case you have more empty lines that what you have showed in your example, please use:
$ sed -n -i.bak '/^ *$/!{h;n;H;x;s/[\n ]//g;s/,|,$//;p}' file.csv