LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-29-2010, 10:17 AM   #1
horu
LQ Newbie
 
Registered: Sep 2010
Posts: 2

Rep: Reputation: Disabled
Help with sed


Hi,

I'm hoping someone can help with the correct syntax for sed. I retrieve a log file via a cgi script and the format of the output file is a single line of text several thousand characters long. It has lots of "\n" in the text, but they seem to be strings and not new line characters.

An example of the file contents:

# cat example.txt
one\nninety\nnine\n

I'd like to remove each \n and insert a new line so the output looks like:

one
ninety
nine

I've tried with sed and tr but I can't get either to output correctly.

# sed 's/\\n/\n/g' example.txt
onenninetynninen

# cat example.txt | tr '\\' '\n'
one
nninety
nnine
n
 
Old 09-29-2010, 10:40 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
How about:
Code:
echo -e $(cat file) > new_file
 
Old 09-29-2010, 10:47 AM   #3
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707

Rep: Reputation: 55
Hi horu,

Same using sed

Code:
sed -i 's/\\n/\n/g' example.txt
Warm Regards,
 
Old 09-29-2010, 12:52 PM   #4
horu
LQ Newbie
 
Registered: Sep 2010
Posts: 2

Original Poster
Rep: Reputation: Disabled
thanks for the examples, I couldn't get sed to work but echo worked fine :-)
 
Old 09-29-2010, 07:15 PM   #5
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Code:
$ echo 'one\nninety\nnine\n' | ruby -pe 'gsub("\\n","\n")'
one
ninety
nine
 
  


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
sed: how to change MAC address string with sed cold Linux - Software 5 08-02-2010 07:43 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:38 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