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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
09-15-2009, 09:12 AM
|
#1
|
|
LQ Newbie
Registered: Sep 2009
Posts: 7
Rep:
|
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
|
|
|
|
09-15-2009, 09:20 AM
|
#2
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
That's a nice learning exercise
What have you tried so far?
|
|
|
|
09-15-2009, 09:33 AM
|
#3
|
|
Moderator
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,816
Rep: 
|
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.
|
|
|
|
09-15-2009, 11:15 AM
|
#4
|
|
LQ Newbie
Registered: Sep 2009
Posts: 7
Original Poster
Rep:
|
i need to write a script
bash
awk
sed
any of three will do ..
|
|
|
|
09-15-2009, 11:20 AM
|
#5
|
|
Senior Member
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,843
Rep: 
|
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.
|
|
|
|
09-15-2009, 11:25 AM
|
#6
|
|
LQ Newbie
Registered: Sep 2009
Posts: 7
Original Poster
Rep:
|
for i in 1 2 3 4
do
var1=$(awk '{print $i}')
var2=','
var3=$var1$var2
done
cat var3
|
|
|
|
09-15-2009, 11:32 AM
|
#7
|
|
Senior Member
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,843
Rep: 
|
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.
|
|
|
|
09-15-2009, 11:50 AM
|
#8
|
|
LQ Newbie
Registered: Sep 2009
Posts: 7
Original Poster
Rep:
|
while read line
do
var1=$(awk '{print $NF}')
var2=','
var3=$var1$var2
done
cat var3
|
|
|
|
09-15-2009, 11:55 AM
|
#9
|
|
LQ Newbie
Registered: Sep 2009
Posts: 7
Original Poster
Rep:
|
if some body has a solution plz share ....
|
|
|
|
09-15-2009, 12:05 PM
|
#10
|
|
Senior Member
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,843
Rep: 
|
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: 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.
|
|
|
|
09-15-2009, 12:30 PM
|
#11
|
|
LQ Newbie
Registered: Sep 2009
Posts: 7
Original Poster
Rep:
|
while read line
do
var1=$(awk '{print $NF}')
var2=','
var3=$var1$var2
done < search.txt
echo $var3
|
|
|
|
09-15-2009, 12:38 PM
|
#12
|
|
Member
Registered: Sep 2009
Distribution: Slackware, Debian, Mac OS X, Zenwalk, Puppy, Gentoo
Posts: 199
Rep:
|
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
|
|
|
|
09-15-2009, 12:48 PM
|
#13
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
Quote:
Originally Posted by deepakthaman
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
|
|
|
|
09-15-2009, 06:22 PM
|
#14
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 14,973
|
Try adding
set -xv
at the top to see what it's doing.
|
|
|
|
09-15-2009, 07:33 PM
|
#15
|
|
LQ Newbie
Registered: Dec 2005
Posts: 1
Rep:
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 07:13 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|