Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 09-15-2009, 10:41 PM   #16
LQ Newbie
Registered: Aug 2006
Posts: 3

Rep: Reputation: 0


# Initialize the variable Result to NULL so that the logic below will work:


# Note: Hard-coding the file name here is not very reusable:

for Line in $(cat search.txt)
if [ -z "${Result}" ] # (If we haven't appending anything yet.)
Result="${Result},${Line}" # (Add a comma and the newest line.)

# Use the variable Result with another command:

echo "The resulting string is: [${Result}]."
Old 09-16-2009, 12:03 AM   #17
LQ Newbie
Registered: Feb 2008
Posts: 6

Rep: Reputation: 0
easy way

the most easy way is without script -- >

setenv LINE `cat search.txt | tr '\n' ','`
echo $LINE

and here you have your string in the variable.
Old 09-16-2009, 12:27 AM   #18
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
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.
Old 09-16-2009, 12:47 AM   #19
Registered: Oct 2005
Location: South Romania
Distribution: Lubuntu 13.04
Posts: 39

Rep: Reputation: 15
or you can use php

PHP Code:
$content file_get_contents($file);
$pieces = array();
$pieces explode("\n",$content);
$count count($pieces);

for (
$i 0$i <= $count$i++) {
$i != $count {
    else {
Old 09-16-2009, 04:27 AM   #20
LQ Newbie
Registered: Aug 2007
Posts: 11

Rep: Reputation: 0
string=`awk '{printf "%s,",$0}' search.txt|sed 's/,$//'`

Old 09-16-2009, 05:47 AM   #21
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
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.

2. This can be done in bash w/ 2 relatively simple commands. Some hints:
  1. echo is for variables.
  2. cat is for files.
  3. What is the difference between
    cat $VAR
    cat "VAR"
  4. tr is your friend.

Finally, please everyone, no more spoon fed answers!
Old 09-16-2009, 02:44 PM   #22
LQ Newbie
Registered: Jul 2009
Distribution: linux
Posts: 1

Rep: Reputation: 0
Originally Posted by deepakthaman View Post
Hi i have a file called search.txt

Which contains text like


Now i have to create a string from the file which should look like


( appending , is essential part) String must be stored in some variable so i can pass it as argument to some other command.

Thnx in advance
tr '\012' ',' < search.txt

variable=`tr '\012' ',' < search.txt`


awk -v ORS=, '{print $0 }' < search.txt

variable=`awk -v ORS=, '{print $0 }' < search.txt`

variable=`echo \`cat search.txt\` | tr ' ' ,`

Last edited by rz70; 09-16-2009 at 03:01 PM. Reason: Another solution
Old 09-16-2009, 05:15 PM   #23
LQ Newbie
Registered: Jun 2008
Posts: 11

Rep: Reputation: 1
How about:

cat filename | xargs | sed 's, ,\,,g'


homework, scripting, shell

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
C++ "counting words in a string" Rico16135 Programming 11 05-06-2009 12:30 PM
grep, sed, awk or tr - searching words in a string hal8000b Programming 2 03-06-2009 08:04 PM
creating array in c++ with string index mohtasham1983 Programming 3 03-11-2007 04:01 PM
PHP script/function to get 4-5 words around a keyword in a string(like google) rmanocha Programming 8 07-13-2004 06:19 AM
creating a unique id string in shell gumby Programming 4 05-07-2003 05:56 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:21 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration