LinuxQuestions.org
Visit Jeremy's Blog.
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 02-13-2013, 05:01 AM   #1
grob115
Member
 
Registered: Oct 2005
Posts: 542

Rep: Reputation: 32
sed replace end of line with character


Hi, if I have a file with 10 lines, and all I want is to join the list of lines together in my file into a single line, by the characer '&', can someone explain why I need to do the following.
Code:
sed ':a;N;$!ba;s/\n/\&/g'
And can not with the following.
Code:
sed 's/\n/\&/g'
 
Old 02-13-2013, 05:44 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
because sed is line based, to doesn't see the \n character, as that delimits the lines it processes.

tr would work easily though...
Code:
tr '\n' '&'
 
Old 02-13-2013, 05:45 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by grob115 View Post
Hi, if I have a file with 10 lines, and all I want is to join the list of lines together in my file into a single line, by the characer '&', can someone explain why I need to do the following.
Code:
sed ':a;N;$!ba;s/\n/\&/g'
And can not with the following.
Code:
sed 's/\n/\&/g'
\n isn't part of the buffer (pattern space) when one line is stored, it is when multiple lines are stored in the buffer.

Have a look here: How to match newlines in sed

BTW: Wouldn't this be a better solution:
Code:
cat infile | tr '\n' '&'
 
Old 02-13-2013, 01:44 PM   #4
grob115
Member
 
Registered: Oct 2005
Posts: 542

Original Poster
Rep: Reputation: 32
Ah damn. Thanks guys. Knew 'sed' is line based but got lost in within what I was trying to do.
 
Old 02-13-2013, 03:01 PM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
I like to use paste for this transformation.

InFile ...
Code:
albert
bernard
charles
david
edward
frank
george
henry
irving
john
Code ...
Code:
paste -sd\& $InFile > $OutFile
OutFile ...
Code:
albert&bernard&charles&david&edward&frank&george&henry&irving&john
Daniel B. Martin

Last edited by danielbmartin; 02-14-2013 at 10:28 PM. Reason: Tighten the code, slightly
 
1 members found this post helpful.
Old 02-14-2013, 09:18 AM   #6
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
Code:
awk 'ORS="&"' file
 
Old 02-14-2013, 10:03 AM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Minor nitpick

Quote:
Originally Posted by grail View Post
Code:
awk 'ORS="&"' file
This awk produces a character string with a trailing ampersand. That's not exactly what the OP asked for.

Daniel B. Martin
 
Old 02-14-2013, 10:16 AM   #8
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by acid_kewpie View Post
tr would work easily though...
Code:
tr '\n' '&'
This code ...
Code:
echo
echo; echo "Method of LQ Moderator acid_kewpie as posted"
echo; echo "InFile ..."; cat $InFile
tr '\n' '&' $InFile > $OutFile
echo "OutFile ..."; cat $OutFile

echo
echo; echo "Method of LQ Moderator acid_kewpie with <"
echo; echo "InFile ..."; cat $InFile
tr '\n' '&' < $InFile > $OutFile
echo "OutFile ..."; cat $OutFile
... produces this output ...
Code:
Method of LQ Moderator acid_kewpie as posted

InFile ...
albert
bernard
charles
david
edward
frank
george
henry
irving
john
tr: extra operand `/home/daniel/Desktop/LQfiles/dbm630inp.txt'
Try `tr --help' for more information.
OutFile ...


Method of LQ Moderator acid_kewpie with <

InFile ...
albert
bernard
charles
david
edward
frank
george
henry
irving
john
OutFile ...
albert&bernard&charles&david&edward&frank&george&henry&irving&john&
Why is it necessary to use the "<" to avoid the "extra operand" error message?

Daniel B. Martin
 
Old 02-14-2013, 10:22 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by danielbmartin View Post
Why is it necessary to use the "<" to avoid the "extra operand" error message?
Because tr doesn't accept a file name as argument: it works only on standard input.
 
1 members found this post helpful.
Old 02-14-2013, 10:28 AM   #10
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by druuna View Post
I clicked and got a pure gray screen; no text.

Daniel B. Martin
 
Old 02-14-2013, 11:33 AM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by danielbmartin View Post
I clicked and got a pure gray screen; no text.

Daniel B. Martin
Works on my side. Checked with Opera and disabled: cookies, plugins and javascript.

Just tried it with Firefox (Iceweasel) and it shows a grey screen....... Try manually adding index.php
 
Old 02-14-2013, 12:03 PM   #12
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
Quote:
This awk produces a character string with a trailing ampersand. That's not exactly what the OP asked for.
As does the tr solution, I picked up on the idea of replace all newlines with an ampersand as opposed to create a string with ampersand as the separator.
 
Old 02-14-2013, 12:43 PM   #13
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by druuna View Post
Try manually adding index.php
This did the trick! Thank you.

Daniel B. Martin
 
Old 02-14-2013, 05:52 PM   #14
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Code:
sed -n -e1h -e'2,$'H -e'$x' -e'$s/\n/\&/g' -e'$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] sed help - replace line feed with different character bradvan Programming 7 04-22-2012 11:31 PM
sed code to replace character vishesh Linux - Newbie 2 05-17-2011 08:01 AM
sed - How do you replace end of line with a space pppaaarrrkkk Programming 7 02-07-2011 11:27 AM
sed add a character to the end of each line keenboy Linux - General 2 08-05-2010 12:36 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 03:12 AM.

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