3

I have a file with multiple lines and want to do the following with each line..

  1. Set three variables from an awk output.
  2. Do something with the variable data (in my example, echo them).

My script successfully does what I want it to, however only for the first line of the file.

#!/bin/bash

while read LINE; do
set -- $(awk -F";" '{print $1, $2, $3}')
var1=$1
var2=$2
var3=$3

echo $1
echo $2
echo $3

done < /path/file

Sample of file data:

NPK;20140325;272128;4579
NPK;20140329;272826;4977
NPK;20140320;271832;4248
NPK;20140415;273213;6698
NPK;20140406;272703;5708
NPK;20140408;272204;5811
NPK;20140324;271966;4465
NPK;20140401;272507;5253
NPK;20140403;272638;5487

Any ideas?

cheers M

1 Answer 1

4

Your awk statement has no explicit input, so it consumes the rest of the stdin input - the remaining lines of your input file.

You must pass the $LINE variable explicitly to the awk command, which you can do with a here-string, namely <<< $LINE:

while read LINE; do

  set -- $(awk -F";" '{print $1, $2, $3}' <<< $LINE)
  var1=$1
  var2=$2
  var3=$3

  echo $1
  echo $2
  echo $3

done < /path/file

However, you could greatly simplify your loop by letting read do all the parsing:

while IFS=\; read var1 var2 var3 unused; do

  echo $var1
  echo $var2
  echo $var3

done < /path/file
Sign up to request clarification or add additional context in comments.

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.