I am trying to use the ampersand character of sed to replace with the original string, and then read from the array, but it is not working. I just get the error: 'operand expected (error token is "&")'.

Here is the relevant line of code:

gawk -F '\t' '{ print $5"\t"$1"\t"$2"\t"$3"\t"$4 }' $TEMPFILE | sed "s/^[0-9]\+/${CATEGORY[&]}/g"

The first gawk statement produces a two digit number at the beginning of the line. If I replace the ampersand in the sed statement with that number manually, everything works. I'd really appreciate some help.


The problem is that the shell expands the double-quoted string first, and then sends the expanded string as a parameter to sed. Sed doesn't know how to expand shell variables, though.

You should be able to do it in awk directly, though:

category=(red amber green)
awk -f <( # This generates the awk code:
          echo '{';
          for (( i=0; i<${#category[@]}; i++ )) ; do
              echo category"[$i]"=\"${category[$i]}\"
          echo 'print $1, category[$1]}'
      ) <( # This is the input file. 
          echo 0; echo 1 ; echo 2 )
| improve this answer | |
  • I'm not that familiar with complex awk statements, but this code just doesn't seem to work. The only thing that it prints is the number that you echoed at the end. – Sarah C. Corriher Jan 19 '16 at 15:22
  • @SarahC.Corriher: Updated with the category assignment. – choroba Jan 19 '16 at 15:24

As choroba pointed out, your problem is that your shell will try to expand ${CATEGORY[something]} before passing it to sed.

If you absolutely want to do this with a pipe from awk to sed, this is a workaround:

awk ... | sed 's/^[0-9]\+/echo ${CATEGORY[&]}/g' | bash

It works by using sed to construct shell statements that read the array. This will still only work if the array is exported.

You can also try

eval $(awk ... | sed 's/^[0-9]\+/echo ${CATEGORY[&]}/g')

which will also work without the array being exported.

I would recommend neither solution unless you can be 100% sure your sed script can't produce any dangerous commands (even if the input to awk is malicious).

| improve this answer | |
  • Thank you. Though I would point out for others that the quotes around the sed statement must be double quotes for this to work. – Sarah C. Corriher Jan 21 '16 at 11:38
  • No. If they're double quotes, the shell will try to expand ${CATEGORY[foo]}, which breaks the script (or I misunderstood what you're trying to do). I used single quotes specifically to avoid that, so that sed would see (and eventually output) ${CATEGORY[foo]} verbatim. – András Korn Jan 21 '16 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.