Removing \n from a text file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Removing \n from a text file
# 1  
Old 05-24-2015
Blade Removing \n from a text file

Hi All,

I have a question regarding text substitution.
I have a file that contains a lot of text.
Some of the text is separated with a \n like:

Code:
TEST\nTEST2\nTEST3
BLA\nBLA2\nBLA3

So there are both actual newlines and 'used to be newlines' in the text.

using tr
Code:
tr "\n" ","

or using sed
Code:
sed 's/\n/,/'

I get the same result: They both replace all the actual newlines in the text file with a comma but NONE of the \n in the file get replaced...

I would like to have the reverse. I would like to keep the newlines and replace any \n in the file with a comma.

Does anyone have an idea?

Last edited by Scott; 05-24-2015 at 11:26 AM.. Reason: Please use code tags
# 2  
Old 05-24-2015
Using RE's search for the backlash character then the following n character
Code:
sed 's/[\][n]/,/g' filename

# 3  
Old 05-24-2015
Quote:
Originally Posted by JaapSchuurman
I would like to have the reverse. I would like to keep the newlines and replace any \n in the file with a comma.
xbin told you already (the more important) half of the truth, here is the rest of it:

"\n" has a special meaning to sed, in fact it means "newline character". Whenever you have to deal with a character with a special meaning to sed you can strip it of its special meaning by prepending a "\" to it: "\*" will not mean "zero or more of the previous character" but "literal asterisk". The same is true for "\\", which will mean a literal backslash. This mechanism is called "escaping". Hence:

Code:
sed 's/\\n/,/' /path/to/input

should do the trick.

I hope this helps.

bakunin
# 4  
Old 05-26-2015
Hi Bakunin and xbin,

Thanks for the reply.

Code:
sed 's/\\n/,/'

does the job indeed :-)

I noticed it only removes one \n per line.
Code:
echo "TEST\nTEST2\nTEST3" | sed 's/\\n/,/'
TEST,TEST2\nTEST3

Can I adapt this to remove all of them?

Last edited by JaapSchuurman; 05-26-2015 at 06:47 AM..
# 5  
Old 05-26-2015
info sed:
Quote:
The `s' command can be followed by zero or more of the following
FLAGS:

`g'
Apply the replacement to _all_ matches to the REGEXP, not just the
first.
# 6  
Old 05-26-2015
Ahh :-)

Thanks all !!! it works now.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Removing PATTERN from txt without removing lines and general text formatting

Hi Everybody! First post! Totally noobie. I'm using the terminal to read a poorly formatted book. The text file contains, in the middle of paragraphs, hyphenation to split words that are supposed to be on multiple pages. It looks ve -- ry much like this. I was hoping to use grep -v " -- "... (5 Replies)
Discussion started by: AxeHandle
5 Replies

2. Shell Programming and Scripting

Removing rows and chars from text file

Dear community, maybe I'm asking the moon :rolleyes:, but I'm scratching my head to find a solution for it. :wall: I have a file called query.out (coming from Oracle query), the file is like this: ADDR TOTAL -------------------- ---------- TGROUPAGGR... (16 Replies)
Discussion started by: Lord Spectre
16 Replies

3. Shell Programming and Scripting

Removing zero values from text file

Hi all, I wrote the following code to remove the value which are 0 in the input file (a columns if numbers). awk 'BEGIN { for (i=1; i<=NF; i++) if ($i) printf("%13.6e\n",$i) }' $1 >> $2 The script works if the zeros are written as 0.0000 but not as 0.000000e+00 In... (10 Replies)
Discussion started by: f_o_555
10 Replies

4. Shell Programming and Scripting

Removing files with same text but different file names

Hi All, I have some 50,000 HTML files in a directory. The problem is; some HTML files are duplicate versions that is wget crawled them two times and gave them file names by appending 1, 2, 3 etc after each crawl. For example, if the file index.html has been crawled several times, it has been... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

5. UNIX for Dummies Questions & Answers

Removing a string of text from a file - help please

Hey Folks, I have a file that contains data that I am working with, sometimes this file has a very long string of text that messes with an awk command in a script i am trying to build. I would like to cut this string of text out of a file and then redirect everything except that string to a new... (5 Replies)
Discussion started by: deepslp
5 Replies

6. Shell Programming and Scripting

removing carriage returns in text file

Hi I have a text file that looks like this: A B C D E F G H I I want it to be reformatted to A;B;C; D;E;F; G;H;I; (4 Replies)
Discussion started by: coolnfunky
4 Replies

7. Shell Programming and Scripting

Removing text from a line in a file

Hi All, I would like to know how to remove text from a line in a file. eg to The 4 sets of numbers are not static ie they change on each line in each different file so if anyone can help that would be great. Jeremy (10 Replies)
Discussion started by: outthere_3
10 Replies

8. Shell Programming and Scripting

Removing a particular line from a text file

Hi, I have a file called inp.txt the contents of the file are as follows MANI123|23|41 MANI123|96|23 I want to reove the first line of this file. How can I do it. Thanks in advance (5 Replies)
Discussion started by: sendhilmani123
5 Replies

9. Shell Programming and Scripting

Removing lines in a text file.

Here is my problem I'm hoping you guru's can help me figure out. I have a text file that contains comma delimited columns. What I'm looking to do is see if the 24th column on each row in the file contains a value (not null), and then write/append that line to a different file. I've been... (4 Replies)
Discussion started by: WABonnett
4 Replies

10. UNIX for Dummies Questions & Answers

removing commas from text file

Dear all I have a file which looks like this xxxxxxxxxxxxxx,xxx,xxxxxxxxxx xxxxxxxxxxxxxx,xxx,xxxxxxxxxx etc basically 14 characters then a comma, three characters, then a comma then 10 characters. We are uploading this file to our mainframe and they want the commas removed, so it... (6 Replies)
Discussion started by: hcclnoodles
6 Replies
Login or Register to Ask a Question