LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


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

Rep: Reputation: 0

#!/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}]."
 
Old 09-16-2009, 12:03 AM   #17
DarkDraw
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
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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
adrianno2
Member
 
Registered: Oct 2005
Location: South Romania
Distribution: Lubuntu 13.04
Posts: 39

Rep: Reputation: 15
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];
    }
}
?>
 
Old 09-16-2009, 04:27 AM   #20
griswald
LQ Newbie
 
Registered: Aug 2007
Posts: 11

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

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

Rep: Reputation: 234Reputation: 234Reputation: 234
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!
 
Old 09-16-2009, 02:44 PM   #22
rz70
LQ Newbie
 
Registered: Jul 2009
Distribution: linux
Posts: 1

Rep: Reputation: 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 03:01 PM. Reason: Another solution
 
Old 09-16-2009, 05:15 PM   #23
solarkash
LQ Newbie
 
Registered: Jun 2008
Posts: 11

Rep: Reputation: 1
How about:

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


Reply

Tags
homework, scripting, shell



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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