LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 09-11-2023, 05:08 AM   #1
sag2662
Member
 
Registered: Sep 2022
Posts: 74

Rep: Reputation: 0
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

Last edited by sag2662; 09-11-2023 at 05:51 AM.
 
Old 09-11-2023, 05:15 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,962

Rep: Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332
/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
 
1 members found this post helpful.
Old 09-11-2023, 05:17 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,142

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
nm - too slow typing ...
 
Old 09-11-2023, 05:47 AM   #4
sag2662
Member
 
Registered: Sep 2022
Posts: 74

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pan64 View Post
/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
 
Old 09-11-2023, 06:04 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,962

Rep: Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332
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
 
1 members found this post helpful.
Old 05-01-2024, 06:56 AM   #6
murugesandins
Member
 
Registered: Apr 2024
Location: Bangalore Karnataka India
Distribution: CYGWIN_NT
Posts: 75

Rep: Reputation: 1
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

Last edited by murugesandins; 05-01-2024 at 06:57 AM. Reason: copied and pasted using Textpad.exe
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
For multiple csv files how do I add the value of one particular entry in any given csv to that csv's name? sean mckinney Programming 8 01-22-2021 09:46 AM
How to print lines in csv file if 1 csv column field = "text". There are 10 column (;) in csv file nexuslinux Linux - Newbie 9 04-22-2016 11:35 PM
[SOLVED] A challenging script - Replace field of CSV file based on another CSV file arbex5 Programming 11 06-12-2013 06:56 AM
[SOLVED] How to script csv editing? Remove rows from csv file that do not contain certain text ingram87 Linux - Software 9 08-03-2012 12:45 PM
Comparing two csv files and write different record in third CSV file irfanb146 Linux - Newbie 3 06-30-2008 09:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 03:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration