LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-01-2016, 10:24 PM   #1
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
tr command \n


I have a file and I'm trying to replace the contents from actual newlines to "interpreted" newlines. How do you escape something that is already meant to be escaped?

Eg.

Code:
hello
goodbye
to

Code:
hello\ngoodbye\n
Some stuff I've tried:

Code:
$ cat file | sed 's:\n:\\\n:g'
hello
goodbye

$ cat file | sed 's:\n:\n:g'
hello
goodbye

$ cat file | tr '\n' \\\n
hello
goodbye

$ cat file | tr '\n' \\\\n
hello\goodbye\\$ cat file | tr '\n' \\\\\n
hello\goodbye\\$ cat file | tr '\n' \\\\\\n 
hello\goodbye\\$ 
$

Last edited by Sefyir; 04-01-2016 at 10:25 PM.
 
Old 04-01-2016, 10:54 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
You can't change what ain't there - record oriented reads give you the record; up to the newline.
You need to be able to read the entire file as a single record so the newlines are still embedded. perl does it, awk can be fudged to do it, and you can use sed to read each record and append a newline before the next record is appended; the change them all to something else.

Use whichever you feel most comfortable with.
 
Old 04-01-2016, 11:28 PM   #3
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Original Poster
Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
I guess I don't fully understand. I was unable to get sed to work by itself (most searches were for creating actual newlines and not the characters \n)

After a lot of trial and error, this seems to work.

Code:
cat file | tr \\n \# | sed 's:#:\\n:g'
hello\ngoodbye\n\n
Hopefully there won't be any #'s in the file ever! (I don't expect there to be)
 
Old 04-01-2016, 11:32 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Yes I was also going to suggest something similar. I've used "\v" (rather than #) in the past; not likely to be many of them in normal text.

Last edited by syg00; 04-02-2016 at 12:49 AM. Reason: that should be "\v" - vertical tab
 
1 members found this post helpful.
Old 04-02-2016, 01:50 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by Sefyir View Post
I guess I don't fully understand. I was unable to get sed to work by itself
Just to explain a bit better: sed uses \n as line separator, that is not part of the text processed therefore you cannot replace it by that way.
tr itself replaces only chars to chars, so you cannot replace a single char (\n) to a longer string.
 
1 members found this post helpful.
Old 04-02-2016, 09:34 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
You can do it with sed, but you have to tell it to join all the lines into a single string. That string will have embedded newline characters, which you can then replace with whatever you choose.
Code:
sed -n 'H;${x;s/^\n//;s/\n/\\n/g;p}'
 
  


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
[SOLVED] [SOLVED] bash script: can echo command, command works if I type, but command doesn't work in script ... why? hopeless_n00b Linux - Newbie 10 07-12-2018 05:57 AM
SED command failing while substituting variable with error unterminated `s' command vnarvankar Linux - Software 2 03-10-2016 09:10 AM
Command/Script required to send an email alert in case command will not respond rajaniyer123 Linux - General 1 05-19-2012 01:12 PM
Executing shell command from JSP file with command line arg from URL orcusomega Programming 2 01-13-2012 03:38 PM
Bash Command Line Editor, while typing run another command before executing current? gumaheru Linux - General 5 04-13-2010 11:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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