LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Tags used in this thread
Popular LQ Tags , ,

Reply
 
Thread Tools
Old 09-15-2009, 11:41 PM   #16
kooth
LQ Newbie
 
Registered: Aug 2006
Posts: 2
Thanked: 0

[Log in to get rid of this advertisement]
#!/bin/ksh


# 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

# Use the variable Result with another command:

echo "The resulting string is: [${Result}]."
windows_xp_2003 kooth is offline     Reply With Quote
Old 09-16-2009, 01:03 AM   #17
DarkDraw
LQ Newbie
 
Registered: Feb 2008
Posts: 6
Thanked: 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.
windows_xp_2003 DarkDraw is offline     Reply With Quote
Old 09-16-2009, 01:27 AM   #18
jschiwal
Moderator
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 12,881
Thanked: 232
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.
linuxsuse jschiwal is offline     Reply With Quote
Old 09-16-2009, 01:47 AM   #19
adrianno2
LQ Newbie
 
Registered: Oct 2005
Location: South Romania
Distribution: Ubuntu 9.04
Posts: 28
Thanked: 0
or you can use php

PHP Code:
<?php
$file 
'search.txt';
$content file_get_contents($file);
$pieces = array();
$pieces explode("\n",$content);
$count count($pieces);

for (
$i 0$i <= $count$i++) {
  if 
$i != $count {
      echo 
$pieces[$i].",";
    }
    else {
      echo 
$pieces[$i];
    }
}
?>
windows_xp_2003 adrianno2 is offline     Reply With Quote
Old 09-16-2009, 05:27 AM   #20
griswald
LQ Newbie
 
Registered: Aug 2007
Posts: 3
Thanked: 0
string=`awk '{printf "%s,",$0}' search.txt|sed 's/,$//'`

Cheers,
Graham
windows_xp_2003 griswald is offline     Reply With Quote
Old 09-16-2009, 06:47 AM   #21
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 3,599
Blog Entries: 9
Thanked: 45
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:
  1. echo is for variables.
  2. cat is for files.
  3. What is the difference between
    Code:
    cat $VAR
    &
    Code:
    cat "VAR"
    ???
  4. tr is your friend.


Finally, please everyone, no more spoon fed answers!
linuxdebian archtoad6 is offline     Reply With Quote
Old 09-16-2009, 03:44 PM   #22
rz70
LQ Newbie
 
Registered: Jul 2009
Distribution: linux
Posts: 1
Thanked: 0
Quote:
Originally Posted by deepakthaman View Post
Hi i have a file called search.txt

Which contains text like

Car
Bus
Cat
Dog

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

Car,Bus,Cat,Dog

( 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
1)
tr '\012' ',' < search.txt

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

2)

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

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

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

Last edited by rz70; 09-16-2009 at 04:01 PM.. Reason: Another solution
windows_xp_2003 rz70 is offline     Reply With Quote
Old 09-16-2009, 06:15 PM   #23
solarkash
LQ Newbie
 
Registered: Jun 2008
Posts: 11
Thanked: 1
How about:

Code:
cat filename | xargs | sed 's, ,\,,g'
windows_xp_2003 solarkash is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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


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

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration