ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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, 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.
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.
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.