0

I would like to create a single sentence (In order partially answer my question: AWK Assignment and execute operation with variables, using split and concatenation without space).

% awk 'BEGIN { str1 = "foo"; str2 = "bar"; str3 = str1 str2; print str3 }'
foobar

That is very easy. But, before is static!!!

Taking in account:

% echo $(echo "foo") 
foo

Now, I would like to "calculate" the value of str1.

% awk 'BEGIN { str1 = $(echo "foo"); str2 = "bar"; str3 = str1 str2; print str3 }'
awk: illegal field $(foo), name "(null)"
source line number 1

Is it possible to do the assignment dynamically (product of other action/command) the value for str1 using AWK?

As @anubhava help me:

I get:

% awk -v str1="$(echo "foo")" 'BEGIN {str2 = "bar"; print str1 str2 }'
foobar

Now, How I can use the first variable as argument for assignment for second variable?

% awk -v str1="$(echo "foo")" -v str2="$(echo str1)bar" 'BEGIN {my operation with str2 }'

But Currently I get:

% awk -v str1="$(echo "foo")" -v str2="$(echo str1)bar" 'BEGIN {print str2 }'
str1bar

Partially:

% str1="$(echo 'foo')"; str2="$(echo ${str1}'bar')";awk -v result="$str2" 'BEGIN{print result}'
foobar
joseluisbz
  • 1,491
  • 1
  • 36
  • 58
  • 2
    Use `awk -v str1="$(some_command)" 'BEGIN {str2 = "bar"; print str1 str2 }'` – anubhava Jul 15 '22 at 15:08
  • 1
    Don't just abandon [your previous question](https://stackoverflow.com/q/72991038/811293), people are trying to help you there. – Ed Morton Jul 15 '22 at 15:21
  • 1
    I think you are getting your shell and the commands you run in it confused with the entirely complete scripting language of awk. You are conflating the two and trying to do operations mixing the two entirely different languages. Why? Do shell stuff in shell and do awk stuff in awk. Just about any problem you can solve in one language, you can solve entirely in the other and vice versa. – JNevill Jul 15 '22 at 16:00
  • You can assign output of awk to another variable like: `var2="$(awk -v str1="$(echo "foo")" 'BEGIN {str2 = "bar"; print str1 str2 }')"` – anubhava Jul 15 '22 at 16:49
  • `str1="$(echo 'foo')"` is just `str1='foo'`. You're throwing `echo`s and command substitution in multiple places where they aren't needed and introduce potential breakage. – Ed Morton Jul 15 '22 at 19:59

2 Answers2

0

As I mentioned in my comment, do shell stuff in shell and awk stuff in awk. Don't try to hamfist your shell logic into your awk script.

Consider your attempt:

awk -v str1="$(echo "foo")" -v str2="$(echo str1)bar" 'BEGIN {my operation with str2 }'

You want, in shell, to echo "foo" into a variable. Then, in shell, you want to concatenate "bar" with that prior variable. So... do it in shell before calling your awk script:

str1="$(echo 'foo')"
str2="$(echo ${str1}'bar')"
awk -v foobar="$str2" '{BEGIN my operation with str2}'

All that -v flag does is say "Set my internal awk variable to this value" so there is no reason to try to hamfist logic into those flags.

JNevill
  • 46,980
  • 4
  • 38
  • 63
  • I'm studying posibilities of AWK applying to my doubts... Thanks in advance. – joseluisbz Jul 15 '22 at 16:23
  • is it not possible in a single line or command? That whas my doubt! – joseluisbz Jul 15 '22 at 16:24
  • 1
    If you can do those first two lines in one command in shell (since that's shell logic, either way you cut it), then you could toss that single line inside of `awk -v foobar="" ''. At some pojnt though you are likely to get in single/double quote inception nightmare. I get the feeling that this is all one big "XY problem" and you are asking questions from the context of a solution you are considering to a problem you aren't sharing with us. – JNevill Jul 15 '22 at 16:27
0

You're overcomplicating things or the example you chose is too simplistic. There is no job for awk here, all can be done simply on command line.

$ str1=foo; str2=bar; echo ${str1}${str2}

foobar
karakfa
  • 66,216
  • 7
  • 41
  • 56