Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
#!/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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.