0

So you know this right?: Read a file line by line assigning the value to a variable

How do you do this without a loop, I'm in a situation where loops would case errors and a mess.(If you've ever tried messing with parallel) But I still need to read a file line by line.

Is there some way of doing it without a loop? xargs? awk? parallel?

  • 2
    You can't read line by line without a loop. It's not possible. Even if you read the entire file into memory all at once, to process it line by line you need a loop. Your question makes no sense. And if your code is so poorly written that a loop would *cause errors and a mess*, fix your code first and then add the loop. – Ken White May 31 '18 at 00:08
  • You can use `readarray` to read a file into an array. – Barmar May 31 '18 at 00:09
  • 3
    Remember to ask questions in such a way that the answer will be useful to you. You can totally do `parallel 'var={}; echo "$var"' < file` which fits the question exactly, but it may be slow, inapplicable or stupid for a real world script – that other guy May 31 '18 at 00:18
  • @thatotherguy Upvote for getting quoting of " space and \n correct. – Ole Tange Jun 01 '18 at 12:18

4 Answers4

2

Use readarray to read the whole file into an array variable. Each line will be an array element.

readarray -t arrayname < filename
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • It's synonym `mapfile` may also add to its use here. It was always a bit puzzling the lack of distinction between the two. The only discernible difference I could see was that `mapfile` would use the environment variable `MAPFILE` as the array. – David C. Rankin May 31 '18 at 04:25
  • @DavidC.Rankin Does it even have that difference? The `readfile` documentation says the variable is optional, so I assume it defaults the same. The documentation implies that `mapfile` is the preferred name, but I usually see people using `readarray` (the name seems more intuitive). – Barmar May 31 '18 at 15:45
1

The question really doesn't make much sense to me, but you can certainly do:

{
read line_one <&3; # Read from the input file
echo "The first line is $line_one"
read line_two -u 3;  # A different method to read
echo "The second line in $line_two"
...
} 3< input file

Note that it's not really necessary to read from fd 3, but it's much safer if your commands are not reading from the same input as the reads.

But if you're truly "in a situation where loops would case errors and a mess" then you have a problem for which this is not the solution.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
0

FWIW, using a recursive function (well, recursion might be a loop...). First some test material:

$ seq 1 4 > file

then the function fun:

$ fun() {
    if read -r line <&5      # read from defined FD 5
    then 
        echo process $line   # process the lines here
        fun                  # recursive function call
    fi
}

calling for fun:

$ exec 5< file
$ fun
process 1
process 2
process 3
process 4
$ exec 5<&-                  # close the FD
James Brown
  • 36,089
  • 7
  • 43
  • 59
0

It is a bit unclear what you are asking. Maybe you are simply asking for:

myvar="$(cat thefile)"
Ole Tange
  • 31,768
  • 5
  • 86
  • 104