Linux - NewbieThis 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
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.
# Initialize the variable Result to NULL so that the logic below will work:
Result=
# Note: Hard-coding the file name here is not very reusable:
for Line in $(cat search.txt)
do
if [ -z "${Result}" ] # (If we haven't appending anything yet.)
then
Result="${Line}"
else
Result="${Result},${Line}" # (Add a comma and the newest line.)
fi
done
Do you need to convert the lines into one long csv line or lines with 4 items. Be sure to test any solution you come up with with various inputs. Does it break with only 2 lines. Does it produce as comma at the end. What do you do about blank lines? Maybe the input file will have a trailing blank line. Maybe the last line won't end with a newline.
Guys, this is clearly homework. It does nothing good for the poster to give the answer, instead of guiding him/her to one. I was going to rant at those who broke this unofficial LQ rule, until I looked at the answers given. -- Unless I am mistaken, all are either in the wrong language (I hope deliberately), or broken.
I see at least 2 simple paths to a solution w/in the parameters set:
1. A sed script which uses the 'N' command to access the newline between lines & then convert it to a comma. http://www.gnu.org/software/sed/manual/sed.html
2. This can be done in bash w/ 2 relatively simple commands. Some hints:
echo is for variables.
cat is for files.
What is the difference between
Code:
cat $VAR
&
Code:
cat "VAR"
???
tr is your friend.
Finally, please everyone, no more spoon fed answers!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.