0

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.

4 Answers 4

2

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)!

Sign up to request clarification or add additional context in comments.

3 Comments

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: 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.
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.
0

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.

3 Comments

Something is missing. In my file I have something like that: ....... AAA_4436188 37935 In the param all I have is 3793536188, so the last line and the half of the one before.
Can you post a fuller snippet of the file?
It was a DOS file with CR messing things up - see comments below my answer.
0
param="$(< myFile.txt)"

or

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

Comments

0

awk

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

ksh

 while read -r line
 do
   t="$t $line"
 done < file
 echo $t

1 Comment

Actually to display it, it works, but I cannot put it into a variable/ I'm able to display all the line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.