LinuxQuestions.org
Review your favorite Linux distribution.
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, 09:12 AM   #1
deepakthaman
LQ Newbie
 
Registered: Sep 2009
Posts: 7

Rep: Reputation: 0
Creating String from words in a file


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
 
Old 09-15-2009, 09:20 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
That's a nice learning exercise

What have you tried so far?
 
Old 09-15-2009, 09:33 AM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Yes it does look like a good exercise. What did your professor tell you to use?

bash
sed
awk
tr
perl
C
C++
Java

To name a few.
 
Old 09-15-2009, 11:15 AM   #4
deepakthaman
LQ Newbie
 
Registered: Sep 2009
Posts: 7

Original Poster
Rep: Reputation: 0
i need to write a script
bash
awk
sed

any of three will do ..
 
Old 09-15-2009, 11:20 AM   #5
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
So, what did you try? Show us some evidence you've actually put a bit more effort in than just posting to the first Linux forum you found on the internet.
 
Old 09-15-2009, 11:25 AM   #6
deepakthaman
LQ Newbie
 
Registered: Sep 2009
Posts: 7

Original Poster
Rep: Reputation: 0
for i in 1 2 3 4
do
var1=$(awk '{print $i}')
var2=','
var3=$var1$var2
done

cat var3
 
Old 09-15-2009, 11:32 AM   #7
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Have a look into tr. You can translate a particular character (in this case, I suggest a newline represented by \n) into another charater, a comma. Storing the output of a command as a variable is achieved through the use of $() in bash. So, if I wanted to store the output of the date command as a variable called currentDate, I'd do the following:
Code:
currentDate=$(date)
You can use this approach to store the output of the command you need to produce as a variable.

Hope this helps.
 
Old 09-15-2009, 11:50 AM   #8
deepakthaman
LQ Newbie
 
Registered: Sep 2009
Posts: 7

Original Poster
Rep: Reputation: 0
while read line
do
var1=$(awk '{print $NF}')
var2=','
var3=$var1$var2
done

cat var3
 
Old 09-15-2009, 11:55 AM   #9
deepakthaman
LQ Newbie
 
Registered: Sep 2009
Posts: 7

Original Poster
Rep: Reputation: 0
if some body has a solution plz share ....
 
Old 09-15-2009, 12:05 PM   #10
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
If you insist on using the approach of a loop, then you should know that you don't cat a variable, you echo it. And calling a variable requires that you prefix its name with a $. For example:
Code:
echo $var3
That should at least point you in something of the right direction to help you debug your code.

edit: As an extra hint, your while loop doesn't have any input. Try adding < search.txt after done.

Last edited by pwc101; 09-15-2009 at 12:07 PM.
 
Old 09-15-2009, 12:30 PM   #11
deepakthaman
LQ Newbie
 
Registered: Sep 2009
Posts: 7

Original Poster
Rep: Reputation: 0
while read line
do
var1=$(awk '{print $NF}')
var2=','
var3=$var1$var2
done < search.txt

echo $var3
 
Old 09-15-2009, 12:38 PM   #12
vendtagain
Member
 
Registered: Sep 2009
Distribution: Slackware, Debian, Mac OS X, Zenwalk, Puppy, Gentoo
Posts: 199

Rep: Reputation: 32
c++

c++ is only one i know, first draft-see if it works

#include <ifstream>
#include <string>
#include <iostream>

main()
{
ifstream fileinput;
string varfile;

int numOfVars = 4; //because 4 variables u need to read

fileinput.open("textfile.txt");

for(count=0;count<numOfVars;count++) // executes 4 times
{
fileinput>>varfile; //reads first string from textfile.txt
endfile = endfile + varfile + ","; //adds varfile string to end of endfile
}



return 0;
}


in this case the string would be in variable endfile
btw the end of the string would have have a comma u can fix this with an if statement under
 
Old 09-15-2009, 12:48 PM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by deepakthaman View Post
while read line
do
var1=$(awk '{print $NF}')
var2=','
var3=$var1$var2
done < search.txt

echo $var3
Maybe you don't need the awk. Maybe you're going to end up with a comma you don't want at the end. Maybe you're going to lose the car and the bus. It's useful to add some debug statements so you can see what your script is doing. Try this
Code:
while read line
do
    echo "DEBUG: line is '$line'"
    var1=$(awk '{print $NF}')
    var2=','
    var3=$var1$var2
    echo "DEBUG: var3 is '$var3'"
done < search.txt

echo $var3
 
Old 09-15-2009, 06:22 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Try adding

set -xv

at the top to see what it's doing.
 
Old 09-15-2009, 07:33 PM   #15
cmorais
LQ Newbie
 
Registered: Dec 2005
Posts: 1

Rep: Reputation: 0
In bash:

###############
#!/bin/bash
for item in $(cat search.txt) # put the output of cat <file> in a list for iteraction
do
result=$result$item, # That's the string concatenation "magic". Just try to understand, as I did.
done
echo $result # print the output

###########
I'd have done it more stylish in perl, but that fills what you asked.

Last edited by cmorais; 09-15-2009 at 08:17 PM. Reason: some mistakes =P
 
  


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 06:21 AM.

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