Storing awk command in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Storing awk command in a variable
# 1  
Old 09-10-2014
Storing awk command in a variable

I'm working on a script in which gives certain details in its output depending on user-specified options. So, what I'd like to do is something like:

Code:
if [ "$2" == "detailed" ]
then
     awkcmd='some_awk_command'
else
     awkcmd='some_other_awk_command'
fi

Then, later in the script, we'd do something like:

curk -sk "some_url" | $awkcmd

So far, so good... but I'm having a heck of a time escaping my awk command so that it works. Here's an example:

Code:
awk -F , -v sd=$sd -v ed=$ed -v tname=$tname 'BEGIN {OFS = ","} $3 !~ "Metric" && $3 == "1" {success++} $1 !~ "Metric" && $3 == "0" {fail++} END {total = success + fail ; p = (success * 100) / total ; print tname, sd, ed, total, success, p}'

These are what I've tried, with the resulting output from the script:

Code:
awkcmd='awk -F , -v sd=$sd -v ed=$ed -v tname=$tname '\''BEGIN {OFS = ","} $4 !~ "Metric" && $3 == "1" {success++} $1 !~ "Metric" && $3 == "0" {fail++} END {total = success + fail ; p = (success * 100) / total ; print tname, sd, ed, total, success, p}'\'''

Output:
awk: 'BEGIN
awk: ^ invalid char ''' in expression

Code:
awkcmd="'awk -F , -v sd=$sd -v ed=$ed -v tname=$tname 'BEGIN {OFS = "'"'","'"'"} $3 !~ "'"'"Metric"'"'" && $3 == "'"'"1"'"'" {success++} $1 !~ "'"'"Metric"'"'" && $3 == "'"'"0"'"'" {fail++} END {total = success + fail ; p = (success * 100) / total ; print tname, sd, ed, total, success, p}'"

Output:
./impact_sla.sh: line 48: 'awk: command not found

So, any thoughts? Is there some rule I'm missing that should allow this to work? Some clever way of getting bash to take care of it for me? I even tried running it through the bash printf %q formatter, but no luck there either.
# 2  
Old 09-10-2014
Try like this:

Code:
awkcmd=(awk -F , -v sd=$sd -v ed=$ed -v tname=$tname '
BEGIN {OFS = ","}
$4 !~ "Metric" && $3 == "1" {success++}
$1 !~ "Metric" && $3 == "0" {fail++}
END {total = success + fail ; p = (success * 100) / (total?total:1) ; print tname, sd, ed, total, success, p}' )

curk -sk "some_url" | "${awkcmd[@]}"

# 3  
Old 09-10-2014
Note that in your original post, you had:
Code:
... $3 !~ "Metric" && $3 == "1" {success++} $1 !~ "Metric" && $3 == "0" {fail++} ...

I would guess that the $3 shown in red should have been $1, as it was in the next test. It looks like Chubler_XL guessed that it should have been $4.

As you look for a solution to your quoting problems, please also verify that the awk script you provided is looking for "Metric" in the correct field in both places where you look for it.
# 4  
Old 09-10-2014
To tell the truth I didn't pay much heed to the actual awk script and simply copy/paste from the 2nd example.

Also note that it is not clear if the values of $sd $ed and $tname should be resolved when the awkcmd is assigned or when it's executed.

I've gone with the "assigned" option as the "when executed" probably involves eval or some search/replace operations.
# 5  
Old 09-11-2014
How about storing two awk scripts in two different files and then use sth. like curk ... | awk -f$awkscript?
# 6  
Old 09-11-2014
Thanks for all the replies. I'm trying out the various options, and I didn't want to seem ungrateful while I do so. So, in the interest of supporting future generations of awkers, I will post whatever I find. Again, I appreciate all the replies and help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to compare each file in two directores by storing in variable

In the below bash I am trying to read each file from a specific directory into a variable REF or VAL. Then use those variables in an awk to compare each matching file from REF and VAL. The filenames in the REF are different then in the VAL, but have a common id up until the _ I know the awk portion... (15 Replies)
Discussion started by: cmccabe
15 Replies

2. Shell Programming and Scripting

Storing multiple sql queries output into variable by running sql command only once

Hi All, I want to run multiple sql queries and store the data in variable but i want to use sql command only once. Is there a way without running sql command twice and storing.Please advise. Eg : Select 'Query 1 output' from dual; Select 'Query 2 output' from dual; I want to... (3 Replies)
Discussion started by: Rokkesh
3 Replies

3. Shell Programming and Scripting

Storing command output in a variable and using cut/awk

Hi, My aim is to get the md5 hash of a file and store it in a variable. var1="md5sum file1" $var1 The above outputs fine but also contains the filename, so somthing like this 243ASsf25 file1 i just need to get the first part and put it into a variable. var1="md5sum file1"... (5 Replies)
Discussion started by: JustALol
5 Replies

4. Shell Programming and Scripting

Storing output of "time" command to a variable

Hi all, I am new to Linux/shell scripting having moderate knowledge. In my script, I need to get execution time of a command (say 'ls') in mili seconds level. For this i tried using "time" command to retrieve the total execution time in milli seconds. But, the problem is that, how to save... (9 Replies)
Discussion started by: happening_linux
9 Replies

5. Shell Programming and Scripting

Storing o/p of a command to a variable

Hi, I have a ftp script there I want to store the o/p of the below command: sftp -b <batch file> user@password cat <batch file> get /remote/file/path/remote_file_name.csv*.gz /local/path Now the problem is that when I fire this command. Then it gives o/p as: File... (7 Replies)
Discussion started by: dips_ag
7 Replies

6. Programming

Need help in storing command line argument argv[2] to a variable of int type

The following program takes two command line arguments. I want the second argument (fileCount) to be stored/printed as a int value. I tried my best to typecast the char to int (check the printf statement at last) but is not working...the output is some junk value. This program is in its... (3 Replies)
Discussion started by: frozensmilz
3 Replies

7. UNIX Desktop Questions & Answers

problem while storing the output of awk to variable

Hi, i have some files in one directory(say some sample dir) whose names will be like the following. some_file1.txt some_file2.txt. i need to get the last modified file size based on file name pattern like some_ here i am able to get the value of the last modified file size using the... (5 Replies)
Discussion started by: eswarreddya
5 Replies

8. Shell Programming and Scripting

storing a command in a variable

how would i go about storing this command in a variable echo "$LINE" | awk -F"|" '{print $1"|"$2"|"$3"}' i have tried FOO = ${command up there} but receive the error FOO: not found aswell as a couple of other attempt but no luck (2 Replies)
Discussion started by: nookie
2 Replies

9. Shell Programming and Scripting

storing result of a command in variable

For whatever reason I cant seem to fix my syntax to do the following. I want to run a grep and count how many instances come up and store that number in a variable but I keep erroring out. Here's my code in bash: number=grep blah file.txt | wc -l (1 Reply)
Discussion started by: eltinator
1 Replies

10. Shell Programming and Scripting

storing output of awk in variable

HI I am trying to store the output of this awk command awk -F, {(if NR==2) print $1} test.sr in a variable when I am trying v= awk -F, {(if NR==2) print $1} test.sr $v = awk -F, {(if NR==2) print $1} test.sr but its not working out . Any suggestions Thanks Arif (3 Replies)
Discussion started by: mab_arif16
3 Replies
Login or Register to Ask a Question