Code: Select all
50F
25 in
32
NE
I have spent hour and hours (REALLY... at least 10!) trying different code but can't get anything to work!
I'm using a Rpi3b with the latest Rasbarian.
Thanks for any help.
Code: Select all
50F
25 in
32
NE
Code: Select all
#!/bin/bash
line_no=1
while read input
do
eval "line$line_no=\"$input\""
(( line_no += 1 ))
done < datafile
echo "line1 = $line1"
echo "line2 = $line2"
echo "line3 = $line3"
echo "line4 = $line4"
I'd suggest using an array and mapfile which is AKA readarray , e.g.:solo2500 wrote:Can somebody PLEASE write a few lines of code that will take data from a text file like this:and import it to a shell script and assign each line to a variable... (say line1, line2, line3, line4)Code: Select all
50F 25 in 32 NE
I have spent hour and hours (REALLY... at least 10!)
THANK YOU!!!!!!!!!!!!!stderr wrote:I'd suggest using an array and mapfile which is AKA readarray , e.g.:solo2500 wrote:Can somebody PLEASE write a few lines of code that will take data from a text file like this:and import it to a shell script and assign each line to a variable... (say line1, line2, line3, line4)Code: Select all
50F 25 in 32 NE
I have spent hour and hours (REALLY... at least 10!)
mapfile all_lines < "file.txt"
Then you can interact with the lines with something like:
echo "|${all_lines[0]}|"
echo "|${all_lines[1]}|"
echo "|${all_lines[2]}|"
The above | are just to show the start and end of each line. You can replace the 0, 1, 2 with a variable like n so you can go through each line. Then using a here string or something with read could be used to read chars into sub variables based on some delimiter as you interact with each line's actual contents. You haven't said what you are trying to do so it's difficult to get farther than that.
One thing about bash is that because it was constructed via evolution, it has all sorts of things that don't fit together in a manner that is logically consistent and it often has many ways to do the same thing and then no good way to do other things at all. But because it can just run any other command line program and do so in an essentially transparent manner, as part of the bash syntax itself, it and other similar shell interpreters are very powerful.
Code: Select all
#! /bin/bash
# Read the file in parameter and fill the array named "array"
getArray() {
array=() # Create array
while IFS= read -r line # Read a line
do
array+=("$line") # Append line to the array
done < "$1"
}
# Print the file (print each element of the array)
getArray "datafile.txt"
for e in "${array[@]}"
do
echo "$e"
done