0

I am new in linux and I try reading a text file line by line. The lines are numbers. I want to add each line in an array and consider each number as a variable. My trial is as below:
Example of txt file:

1976   1   0  0.00    0.    68.    37.     0.   105.  0.14 0.02    4.3    1.1    2.2

What I need:
Putting each number in a variable. for example a = 1976 and b = 1 etc...
My code:

IFS=$'\n'
for next in `cat $filename`
do

line=$next

echo ${line[0]}
done

Result:

1976   1   0  0.00    0.    68.    37.     0.   105.  0.14 0.02    4.3    1.1    2.2
5
  • I suggest to use awk for this. Commented Jan 3, 2016 at 10:44
  • I am searching for a comand using awk to do this Commented Jan 3, 2016 at 10:48
  • Is it ok if it would be array? Commented Jan 3, 2016 at 10:57
  • I found this command which results in the first number in the array 1976 but i do not know how to loop over the rest of the array echo $array | sed 's/^.* (".*"$)/\1/' Commented Jan 3, 2016 at 11:02
  • I tried this with changing the number in {print $0} to get the second and third field but it prints 1976 each time : while read -r line; do array=( $line ) echo $array | awk '{print $0}' done < read1.out Commented Jan 3, 2016 at 11:10

2 Answers 2

1

It's quite easy to store each value in an array. Here is an example:

while read -r -a line
do
 echo "${line[0]}"
 echo "${line[1]}"
 echo "${line[2]}"
done < $filename

-a line splits the input-line into words (white space seperated by default) and store the results in line array.

A snippet from read man:

-a Each name is an indexed array variable (see Arrays above).

You man not need the '-r' option. It basically make read to treat \ as nothing special in the input.

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

4 Comments

Omar's code in the question suggested me that's what he's trying to achieve actually.
thank you sir, the provided script works with removing the dollar sign from the file name
Sir, Sorry I am still new. The fields are well extracted by your script but i do not know how to save them in variables for instance a=echo "${line[0]}" does not work
You can assign like so: a=${line[0]} , without echo. Still I'm not sure what you're trying to achieve and how helpful is that for you. That way you assign variables in a loop. You can still access them after the loop is executed, but if you do something like a=${line[0]} then a would contain a value for the last line read.
0
# Add each line to Array
readarray -t aa < $filename
# Put each line into variables using Here String
for l in "${aa[@]}"; do
  read a b c <<< $l;     # Example using 3 variables, could be as many as on line
  # Do whatever has to be done with a, b, c, etc
done

Comments

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.