'Remove new line characters from csv file and save results to the same filename

I have files which have spaces in their filenames (ex filename Listener Care - RXM Call Center Satisfaction-2022-03-01.csv) These files also have new line characters in the data they contain. I have several such files in the input directory. For each file, I want to remove new line characters and save the result to the same file. example - Listener Care - RXM Call Center Satisfaction-2022-03-01.csv --> remove new line characters --> save result to Listener Care - RXM Call Center Satisfaction-2022-03-01.csv

I want to do this process for each file in the input directory and then move these processed files to Archive directory.

I tried to use
cat 'Listener Care - RXM Call Center Satisfaction-2022-03-01.csv'| gawk -v RS='"' 'NR % 2 == 0 { gsub(/\n/, "") } { printf("%s%s", $0, RT) }' >/home/kold/Qual/modified/'Listener Care - RXM Call Center Satisfaction-2022-03-01.csv';

This works for single file.

Please guide me how to make the solution work as per my requirements stated above.mI am using ksh. Thank you.



Solution 1:[1]

The outputfile is already created before reading the input, so you need a temporary file. Something like

mydir='/home/kold/Qual/modified/'

    csv="${mydir}/Listener Care - RXM Call Center Satisfaction-2022-03-01.csv"
    tmpfile="${mydir}/${csv}.tmp"
    
    gawk -v RS='"' '
      NR % 2 == 0 { gsub(/\n/, "") }
      {
        printf("%s%s", $0, RT)
      }' "${csv}" > "${tmpfile} && mv "${tmpfile}" "${csv}"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1