Reputation: 87
I am trying to read from a file that is multiple lines with multiple variables per line. There are four variables: item, price, max_qty, and min_qty. The file looks like this:
item price
min_qty max_qty
I have tried:
while IFS=' ' read -r first second third fourth; do
echo "$first $second $third $fourth
done
This does not work. I want the output to be:
item price min_qty max_qty
I have also thought about somehow replacing the new lines with spaces and then reading from that line. I don't want to actually change the file though.
Upvotes: 0
Views: 857
Reputation: 8084
If it is true that "the file looks like this" (two lines, two values per line, not a larger file with many pairs of lines like that), then you can do it with a single read
by using the -d
option to set the line delimiter to empty:
read -r -d '' first second third fourth <file
echo "$first $second $third $fourth"
errexit
or the ERR
trap set, the program will exit immediately, and silently, immediately after the read
is executed. See Bash ignoring error for a particular command for ways of avoiding that.IFS
(space+tab+newline). If IFS could be set to something different elsewhere in the program, set it explicitly with IFS=$' \t\n' read -r -d '' ...
.Upvotes: 0
Reputation: 50750
read
twice:
while read -r first second && read -r third fourth; do
echo "$first $second $third $fourth"
done < file
Upvotes: 3