tags:

views:

54

answers:

4

Hi.

I have a simple thing to do, but I'm novice in UNIX. So, I have a file and on each line I have an ID. I need to go through the file and put all ID's into one variable.

I've tried something like in Java but does not work.

for variable in `cat myFile.txt`
do
    param=`echo "${param} ${variable}"`
done 

It does not seems to add all values into param.

Thanks.

A: 

Try this:

param=`cat myFile.txt | tr '\n' ' '`

The tr command translates all occurrences of \n (new line) to spaces. Then we assign the result to the param variable.

Lovely.

Tom Duckering
Something is missing.In my file I have something like that:.......AAA_443618837935In the param all I have is 3793536188, so the last line and the half of the one before.
CC
Can you post a fuller snippet of the file?
Tom Duckering
It was a DOS file with CR messing things up - see comments below my answer.
Jonathan Leffler
Ah - that old chestnut.
Tom Duckering
+1  A: 

awk

var=$(awk '1' ORS=" " file)

ksh

 while read -r line
 do
   t="$t $line"
 done < file
 echo $t
ghostdog74
Actually to display it, it works, but I cannot put it into a variable/I'm able to display all the line.
CC
+2  A: 

I'd use:

param=$(<myFile.txt)

The parameter has white space (actually newlines) between the names. When used without quotes, the shell will expand those to spaces, as in:

cat $param

If used with quotes, the file names will remain on separate lines, as in:

echo "$param"

Note that the Korn shell special-cases the '$(<file)' notation and does not fork and execute any command.

Also note that your original idea can be made to work more simply:

param=
for variable in `cat myFile.txt`
do
    param="${param} ${variable}"
done

This introduces a blank at the front of the parameter; it seldom matters. Interestingly, you can avoid the blank at the front by having one at the end, using param="${param}${variable} ". This also works without messing things up, though it looks as though it jams things together. Also, the '${var}' notation is not necessary, though it does no harm either.

And, finally for now, it is better to replace the back-tick command with '$(cat myFile.txt)'. The difference becomes crucial when you need to nest commands:

perllib=$(dirname $(dirname $(which perl)))/lib

vs

perllib=`dirname \`dirname \\\`which perl\\\`\``/lib

I know which I prefer to type (and read)!

Jonathan Leffler
It looks fine your piece of code, but the same result. I only have the last line and the half of the one before.What can be wrong with my txt file?
CC
@CC: CRLF line endings? As in, the file comes from DOS, so what you see in the variable is '`name1\rlongername\rn3`' with the output showing just 'n3ngername'. '`\r`' represents a carriage return.
Jonathan Leffler
I got it. Actually my txt file was not in UNIX format. I just opened the file and choosed UNIX format from my editor. I guess there must have been some special characters not visibles.Thanks alot everybody.
CC
A: 
param="$(< myFile.txt)"

or

while read line
do
    param="$param$line"$'\n'
done < myFile.txt
Dennis Williamson