3

I'm trying to store an awk command (command, not the result) in a variable. My objective is to use that variable later in the script, with different inputs. For example:

cmd=$(awk '/something/ {if ...} {...} END {...}')
$cmd $input

I've tried to store the command with $() (like in the example), also with backticks ... But I'm not able to achieve it.

I appreciate the help or any suggestion :)

4
  • What is the general problem you are having? To store a command and then use it does not look like a good solution in general. Commented Jun 2, 2014 at 15:56
  • The problem is that I'm not having any output... I mean, I edit a script file (example: script.sh), I write inside cmd=$(awk '/something/ {if ...} {...} END {...}') $cmd $input , and I execute it (bash script.sh) .. and it gets stuck .. I hope I'm explaining. And you are right. But I need to run that large AWK command several times in my main bash script, and it is a bit "ugly". So I was thinking to store the command in a variable... Sorry! I'm a bit newbie! Commented Jun 2, 2014 at 16:01
  • 1
    FYI if you had asked the question "how do I execute the same script from several locations without having to duplicate it every time?" I guarantee no-one would have suggested storing the code in a variable, the only solutions you got would be to store it as an executable file, an alias, or a function. It's because you instead asked "how do I store a script in a variable?" that you got an answer telling you how to do that. The lesson is - be careful when asking questions to ask for WHAT you want to do, not specifically HOW to implement what you think might be a solution. Commented Jun 2, 2014 at 17:57
  • 2
    @EdMorton you're right. As you can see, besides learning bash scripting, I should also learn to use this forum properly :). Sometimes is more difficult to ask the correct question than to answer it :). I had only thought (badly thought), that in this particular case (with only the input as a variable), to store the command in a variable would work and it would be enough. Commented Jun 3, 2014 at 8:04

2 Answers 2

8

Do not do that, use a function instead of a variable, it's what functions exist to do:

$ cmd() { awk -v x="$1" 'BEGIN{print x}'; }
$ cmd foo
foo
$ cmd bar
bar

A different example:

$ cat file1
a
b

$ cat file2
c
d
e

$ cmd() { awk '{print FILENAME, $0}' "$1"; }

$ cmd file1
file1 a
file1 b

$ cmd file2
file2 c
file2 d
file2 e
Sign up to request clarification or add additional context in comments.

7 Comments

+1, because it's generally the better approach; however, it's not clear that variables need to be passed to the awk program in this case - the question suggests that what varies between invocations is the filename argument (which may ultimately also be a variable assignment, however).
You might be right, he says it's a "large awk script" but idk if he needs more than the input file name to change or not. If there's no args required then an alias might be a better answer. A function that takes the file name as the arg wouldn't hurt though. Either way, I'd never store the awk script in a variable.
Thanks for your suggestions. OK, it's not a very large large awk, but it is quite large to put in one-line, or too many lines if you write it in multiline format. I need to call the awk function several times during my bash main script, so I thought to put it in a variable because I also thougth that it was easier than in a function. The only argument is the input variable. I prefer to use a function than a variable, but I don't know what is better in this case...any suggestion? I hope I'm explaining more or less...
Yes! It works! nice one!Thanks for the explanations..I should learn more about functions/aliases. Thanks also for the link :).
You're welcome and thanks for being willing to listen to an approach other than the answer to the specific question you asked. I really think you'll be much happier in the long run using aliases and functions than trying to store commands in variables.
|
5

Forget the backticks and parentheses. You just want to store the awk script itself

cmd='/something/ {if ...} {...} END {...}'
awk "$cmd" $input

6 Comments

OK! This works but you need to call the variable like this: awk '$cmd' $input . Otherwise you have a awk: fatal error. :)
@cucurbit you're right (except I think you need " vice '), and the answer is edited to reflect.
Also with ' works (don't know why), but I think that is better to put "". Thanks :)
@cucurbit: If you're indeed using bash as you state in a comment on your question, then '$cmd' would NOT work - NO variable expansion is performed in single-quoted strings.
This is a newcomer to bash struggling to learn how to use it. He probably understands variables but apparently does not know when it's appropriate to use those vs aliases and functions. He has a large awk script that he wants to call in several places. I think it's important we show him the right way to do it instead of supporting his desire to implement the wrong way, even if the wrong way might function adequately in this particular case (and I'm not convinced it would, depending on the script and/or input file contents, but I'm not interested in trying to figure out if/where it fails).
|

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.