solo2500
Posts: 123
Joined: Sat Jul 09, 2016 12:38 am

help with shell script

Sun Nov 20, 2016 6:35 pm

Can somebody PLEASE write a few lines of code that will take data from a text file like this:

Code: Select all

50F
25 in
32
NE
and import it to a shell script and assign each line to a variable... (say line1, line2, line3, line4)

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.
I'm a total novice, non-programer (...basically a hack.)

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: help with shell script

Sun Nov 20, 2016 6:51 pm

The example input is clear.

Can you provide an equally clear example of the expected output.

(It is unclear to me whether you really want to make this part of a script or whether you need a script to transform the data to an alternative presentation.)

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: help with shell script

Sun Nov 20, 2016 7:07 pm

A little messy, but is this the sort of thing you are after?

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"

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: help with shell script

Sun Nov 20, 2016 8:00 pm

solo2500 wrote:Can somebody PLEASE write a few lines of code that will take data from a text file like this:

Code: Select all

50F
25 in
32
NE
and import it to a shell script and assign each line to a variable... (say line1, line2, line3, line4)
I have spent hour and hours (REALLY... at least 10!)
I'd suggest using an array and mapfile which is AKA readarray , e.g.:

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.

solo2500
Posts: 123
Joined: Sat Jul 09, 2016 12:38 am

Re: help with shell script

Sun Nov 20, 2016 10:54 pm

stderr wrote:
solo2500 wrote:Can somebody PLEASE write a few lines of code that will take data from a text file like this:

Code: Select all

50F
25 in
32
NE
and import it to a shell script and assign each line to a variable... (say line1, line2, line3, line4)
I have spent hour and hours (REALLY... at least 10!)
I'd suggest using an array and mapfile which is AKA readarray , e.g.:

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.
THANK YOU!!!!!!!!!!!!!
I'm a total novice, non-programer (...basically a hack.)

jbudd
Posts: 1409
Joined: Mon Dec 16, 2013 10:23 am

Re: help with shell script

Sun Nov 20, 2016 11:11 pm

From an answer to pretty much the same question on stackexchange:

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

Return to “Beginners”