LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Creating String from words in a file (https://www.linuxquestions.org/questions/linux-newbie-8/creating-string-from-words-in-a-file-755350/)

deepakthaman 09-15-2009 09:12 AM

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

catkin 09-15-2009 09:20 AM

That's a nice learning exercise :)

What have you tried so far?

crabboy 09-15-2009 09:33 AM

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.

deepakthaman 09-15-2009 11:15 AM

i need to write a script
bash
awk
sed

any of three will do ..

pwc101 09-15-2009 11:20 AM

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.

deepakthaman 09-15-2009 11:25 AM

for i in 1 2 3 4
do
var1=$(awk '{print $i}')
var2=','
var3=$var1$var2
done

cat var3

pwc101 09-15-2009 11:32 AM

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.

deepakthaman 09-15-2009 11:50 AM

while read line
do
var1=$(awk '{print $NF}')
var2=','
var3=$var1$var2
done

cat var3

deepakthaman 09-15-2009 11:55 AM

if some body has a solution plz share ....

pwc101 09-15-2009 12:05 PM

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.

deepakthaman 09-15-2009 12:30 PM

while read line
do
var1=$(awk '{print $NF}')
var2=','
var3=$var1$var2
done < search.txt

echo $var3

vendtagain 09-15-2009 12:38 PM

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

catkin 09-15-2009 12:48 PM

Quote:

Originally Posted by deepakthaman (Post 3683896)
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


chrism01 09-15-2009 06:22 PM

Try adding

set -xv

at the top to see what it's doing.

cmorais 09-15-2009 07:33 PM

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.


All times are GMT -5. The time now is 08:07 AM.