Linux - NewbieThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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
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
###############
#!/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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.