LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   replace \n with space in csv file (https://www.linuxquestions.org/questions/linux-server-73/replace-%5Cn-with-space-in-csv-file-4175728846/)

sag2662 09-11-2023 05:08 AM

replace \n with space in csv file
 
Hi all, I have csv file with the some of the rows with "\n" values, and I wanted to replace "\n" with just empty space " ". How can i achieve it. I tried the below but doesnot work. can anyone help?

Code:

sed 's/\/n/ /g' "$input_file" > "$output_file"
my csv file looks like this

Code:

Name,Age,Location
Alice\nbhat,25,New York
Bob\n,30,Los Angeles
Charlie\nTC,22,Chicago


pan64 09-11-2023 05:15 AM

/n and \n are two different things. Which one do you want to replace?
You can use a different separator:
Code:

sed 's!/n! !g' infile > outfile
# or
sed 's!\\n! !g' infile > outfile


syg00 09-11-2023 05:17 AM

nm - too slow typing ... :doh:

sag2662 09-11-2023 05:47 AM

Quote:

Originally Posted by pan64 (Post 6452958)
/n and \n are two different things. Which one do you want to replace?
You can use a different separator:
Code:

sed 's!/n! !g' infile > outfile
# or
sed 's!\\n! !g' infile > outfile


\n sorry

pan64 09-11-2023 06:04 AM

does it work for you? In that case you might want to mark the thread as solved.
And also if you want to say thanks just click on yes

murugesandins 05-01-2024 06:56 AM

Sample scrip using bash.exe at windows
 
Code:

#!/bin/bash
input_file="input_file.csv"
if [[ ! -f $input_file ]]
then
        echo Last line not ending with new line
        echo -ne "Name,Age,Location
Alice\nbhat,25,New York
Bob\n,30,Los Angeles
Charlie\nTC,22,Chicago" > $input_file
fi
LAST_MODIFIED_DDD_DD_MMM_YYYY_HH_MM_SS_PM_LOC=$(/usr/bin/date --date="$(/usr/bin/stat -c "%y" $input_file)" "+%a_%d_%b_%Y_%I_%M_%S_%p_%Z")
if [[ ! -f "$input_file".$LAST_MODIFIED_DDD_DD_MMM_YYYY_HH_MM_SS_PM_LOC ]]
then
        echo "cp -i $input_file "$input_file".$LAST_MODIFIED_DDD_DD_MMM_YYYY_HH_MM_SS_PM_LOC"
        /usr/bin/cp -i $input_file "$input_file".$LAST_MODIFIED_DDD_DD_MMM_YYYY_HH_MM_SS_PM_LOC
fi
output_file=output_file.csv
if [[ ! -f $output_file ]]
then
        #:n => n is the label name
        #we can use a-z for label names
        #N        take current line append this line to next line using then PATTERN(regex) N
        #$!bn        => $ representing last line. bn => go back to label again. $! excluding the last line go back to label n
        #Replace every new line with space on PATTERN
        /usr/bin/sed ':n;N;$!bn;s/\n/ /g' $input_file > $output_file
        LINE_COUNT=$(/usr/bin/wc -l $output_file | /usr/bin/awk '{ print $1}')
else
        LINE_COUNT=$(/usr/bin/wc -l $output_file | /usr/bin/awk '{ print $1}')
        if [[ 0 -ne $LINE_COUNT ]]
        then
                backup_file=backup_file.csv
                if [[ ! -f $backup_file ]]
                then
                        /usr/bin/sed ':n;N;$!bn;s/\n/ /g' $output_file > $backup_file
                        output_file=$backup_file
                else
                        echo "update this script to rename backup file name $backup_file inside this script to other non-existing file name"
                        read
                fi
        fi
fi
LINE_COUNT=$(/usr/bin/wc -l $output_file | /usr/bin/awk '{ print $1}')
echo "Number of lines at $output_file: $LINE_COUNT"
echo "====== cat $input_file last line not ending with new line ======"
/usr/bin/cat $input_file
#Since previous cat do not end with new line I am using echo
echo
echo "====== cat $output_file ======"
/usr/bin/cat $output_file



All times are GMT -5. The time now is 03:34 AM.