1

My script when run

./program < "$1" | awk -F '!' '{print $3 $4, $5, $6, $7}

returns the values

900 200 30 400 5
51 31 2 3 4

How do I set a variable for the colummns for each line I have attempted

LINE=`./program < $1`
for i in $LINE
do
$LINE | awk -f '!' | read a b c d e <<< "$(echo $i | cut -f4 -d:}'

but no matter how I do it I can't seem to get values into variables

2
  • IFS='!' read a b c d e < <(./program < "$1") ? EDIT: Since there are multiple lines in the output of ./program, you need a while loop around read Commented Apr 1, 2015 at 6:57
  • Your question looks like you have !-delimited values in the output from program, but cut -f4 -d: looks like you want to extract the fourth out of a list of colon-delimited fields. Which is correct? Or do you have !-delimited fields inside :-delimited fields? Commented Apr 1, 2015 at 7:34

2 Answers 2

3

You can use process substitution to read these values for each line of output from awk:

while read -r a b c d e; do
   echo "<$a><$b><$c><$d><$e>"
done < <(./program < "$1" | awk -F '!' '{print $3 $4, $5, $6, $7}')

In fact since awk is only printing columns delimited by ! you can even remove awk completely:

while IFS='!' read -r a b c d e; do
   echo "<$a><$b><$c><$d><$e>"
done < <(./program < "$1")
Sign up to request clarification or add additional context in comments.

Comments

0
for i in $LINE

will loop over the tokens in LINE. You can modify IFS to make a token equal a line, but then you might as well

./program <"$1" |
while IFS='!' read -r a b c d e f g _; do
    echo "<$c$d><$e><$f><$g>"
done

Other problems with your attempt:

  • LINE is not a good variable name. Use lowercase variable names; uppercase should be regarded as reserved for the shell's special variables.

  • Your process substitution attempted to use the loop variable $i outside the loop, but that's not possible; it will be undefined.

  • LINE is not a command name, so its standard output cannot be piped into awk. To pass a string to Awk, do echo "$LINE" | awk or, in Bash only, a here string: <<<"$LINE" awk

  • You have a double redirection to read. It can't read standard input from both the pipe and the here string; the here string will win. But in this case, because read is at the end of a pipeline, it will run in a subshell, and lose its value when the subshell exits. (You can avoid this in Bash. See http://mywiki.wooledge.org/BashFAQ/024 for a discussion.)

  • You had mismatched brackets in $(echo ... | cut ... }

  • You should double-quote the string in echo "$i" (although in this isolated case, it might actually have worked). Generally, unless you specifically require the shell to perform wildcard expansion and token splitting on a variable's value, you should double-quote it.

  • No done to close the for ...; do block.

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.