Does awk have parameter substitution?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Does awk have parameter substitution?
# 8  
Old 05-01-2015
Quote:
Originally Posted by Don Cragun
There is no invocation of awk in this script.
That is because these are but three lines of a 70 line AWK script. Isn't it enough to say code is AWK code or BASH code or Korn code?
Quote:
There is a reference to a shell variable failListFile
indeed there is. I am passing filenames from the Bash wrapper to the AWK script this way.
Quote:
There are mismatched braces.
I disagree.
Quote:
There is no description of what you are trying to do with this awk and/or bash script.
Those 3 lines are what I am doing (they work just fine). I am just looking for a more elegant solution
Quote:
I have no idea what "assignment and default in a single command" you are trying to perform.
Quote:
I have shown you (in a working shell script that invokes awk) how you can assign an awk variable in an awk script based on a variable left uninitialized by the shell, a variable initialized to an empty string by the shell, and a variable initialized to a non-empty string by the shell and use that inside an awk script to set an awk variable to a default value based on a couple of different conditions.
You showed me several things including the solution I used in my very first post in this thread.
Code:
argument=$1; sub ( "^$", "default if empty", argument) (AWK)

Quote:
You haven't explained what you want to do that isn't being done by the sample script I showed you.
That's becaue the sample script you showed me works just fine as do the scripts and portions of scripts I posted.
Quote:
Since I can't figure out what you're trying to do, I am afraid that I am unable to help you any further.
My question was "Does awk have parameter substitution (or something with the same functionality)?" I'm trying my best to explain what I mean by that. I gave a BASH example of parameter substitution and I gave example of AWK code that performs the same function but using more commands.

Let me try again: In BASH you can equate one BASH variable to another BASH variable with a default in a single command.
Code:
a=${b:-"default"}

Is there a similar command in AWK (equate one AWK variable to another AWK variable with a default value in a single command)?

Mike

Last edited by Michael Stora; 05-01-2015 at 05:00 AM..
# 9  
Old 05-01-2015
But - hasn't that been shown to you in posts #2 and #3?
Code:
awk -vb=XXX 'BEGIN {a=b?b:"Default"; print a}' 
XXX
awk  'BEGIN {a=b?b:"Default"; print a}' 
Default

These 2 Users Gave Thanks to RudiC For This Post:
# 10  
Old 05-01-2015
Quote:
Originally Posted by RudiC
But - hasn't that been shown to you in posts #2 and #3?
Code:
awk -vb=XXX 'BEGIN {a=b?b:"Default"; print a}' 
XXX
awk  'BEGIN {a=b?b:"Default"; print a}' 
Default

Post 2, I recognised that it worked (the reason I thanked the post).
Post 3, the second example you reposted is applicable to my script but I failed to understand it.

It appears that I did not appreciate the difference between how AWK ? operates from the BASH conditionals based on std error I am used to.
Code:
 
$ echo | awk 'BEGIN { a=b?b:"Default"; print a }' # I expected this result
Default
 
$ echo | awk 'BEGIN { b=""; a=b?b:"Default"; print a }' # I did not expect this result
Default
 
$ echo | awk 'BEGIN { c="something"; a=b?b:"Default"; print a }' # expected
something
 
$ echo | awk 'BEGIN { b="0"; a=b?b:"Default"; print a }' # expected
0
mestora@MESTORA-MOBL1 ~
$ echo | awk 'BEGIN { b=0; a=b?b:"Default"; print a }' # was not sure what to think but this is useful.
Default

Mike

PS. Going back to thank post 3.
PPS. That example works for one part of my sciprt that does include truely null values. In another file I am working with, the column is never truely empty but it consists of double quoted emptyness, in which case I need to stick with the gsub.
PPS. I also was not aware that you could simultaneously do the assignement and test the assignment. I would have thought the required syntax was:
Code:
a= a=b? c : "Default"; #which also works


Last edited by Michael Stora; 05-01-2015 at 05:00 PM..
# 11  
Old 05-01-2015
Quote:
Originally Posted by Michael Stora
Post 2, I recognised that it worked (the reason I thanked the post).
Post 3, the second example you reposted is applicable to my script but I failed to understand it.

It appears that I did not appreciate the difference between how AWK ? operates from the BASH conditionals based on std error I am used to.
Code:
 
$ echo | awk 'BEGIN { a=b?b:"Default"; print a }' # I expected this result
Default
 
$ echo | awk 'BEGIN { b=""; a=b?b:"Default"; print a }' # I did not expect this result
Default
 
$ echo | awk 'BEGIN { c="something"; a=b?b:"Default"; print a }' # expected
something
 
$ echo | awk 'BEGIN { b="0"; a=b?b:"Default"; print a }' # expected
0
mestora@MESTORA-MOBL1 ~
$ echo | awk 'BEGIN { b=0; a=b?b:"Default"; print a }' # was not sure what to think but this is useful.
Default

Mike

PS. Going back to thank post 3.
PPS. That example works for one part of my sciprt that does include truely null values. In another file I am working with, the column is never truely empty but it consists of double quoted emptyness, in which case I need to stick with the gsub.
PPS. I also was not aware that you could simultaneously do the assignement and test the assignment. I would have thought the required syntax was:
Code:
a= a=b? c : "Default"; #which also works

If you would just tell us the constraints under which you want to assign one of a group of values to a variable, we can probably help you construct a single command using one or more ternary expressions to get what you want. But just saying "I want to set a default value like bash does." doesn't really make sense. The awk utility doesn't have the concept of an "unset" scalar variable. (It does for elements of an array variable; but not for scalar variables.) And, in awk the empty string and the string "0" and the number 0 will be treated as identical in many awk expressions. But, if we know explicitly what you are trying to do we can differentiate between unset array elements, an empty string, and a 0 (although you can't differentiate a string 0 from a number 0 in awk). And, if you reference an array element (other than by using the in operator), you create that array element. For instance, if you split a line using:
Code:
n = split($0, a, ",")

and you want to set the variable v to "unset" if array[5] has not been set (i.e., split() returned a value less than 5), "empty" if a[5] is an empty string, "zero" if a[5] compares equal to 0 (such as with "0", "000", or "0.0"), and to the contents of a[5] for anything else you could use:
Code:
v = (5 in a) ? a[5] == "" ? "empty" : a[5] == 0 ? "zero" : a[5] : "unset"

The command:
Code:
b=""; a=b?b:"Default"

sets a to Default because the ternary operator evaluates the 1st expression (b in this case) and if that expression evaluates to TRUE returns the 2nd expression; otherwise it returns the 3rd expression. In awk a variable evaluates to TRUE if it is a non-zero string, a non-zero number, or a non-empty string; otherwise, it evaluates to FALSE.

Scalar variables in awk are never "unset" as in a shell variable that has never been set. In awk, every unset scalar variable has the value 0 if it is referenced as a numeric value and has the value empty string if referenced as a string.
# 12  
Old 05-02-2015
Quote:
Originally Posted by Michael Stora
.
.
.
Code:
 $ echo | awk 'BEGIN { c="something"; a=b?b:"Default"; print a }' # expected
something

.
.
.
That surprises me. c is "something", but neither assigned to a nor printed anywhere. So, the result should be "Default".

As to what pertains to the "double quoted emptyness", awk can't differentiate between empty and unset. But, neither can bash (man bash):
Quote:
${parameter:-word}
Use Default Values. If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.
# 13  
Old 05-02-2015
Quote:
Originally Posted by RudiC
[..]

As to what pertains to the "double quoted emptyness", awk can't differentiate between empty and unset. But, neither can bash (man bash):
Code:
${parameter:-word}
Use Default Values. If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

Also from man bash:

Quote:
... bash tests for a parameter that is unset or null; omitting the colon results in a test only for a parameter that is unset.
So to test for an unset variable, use:
Code:
${parameter-word}

Which is also standard for any POSIX-conformant shell..

--
Regarding awk, it can detect set or unset array elements through the relational expression "element in array", where the element does not get created...

With regular variables, it is not so much that awk cannot detect unset variables, but rather - as a design choice - awk works such, that referencing a variable creates it first with an empty value.

Last edited by Scrutinizer; 05-02-2015 at 05:31 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 14  
Old 05-02-2015
Should have read on...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parameter substitution is not working with sed

I am trying add a prefix variable(string) to command output. sed parameter substitution is not working. - I have found some issues on my end of testing,, please delete this thread for now. (1 Reply)
Discussion started by: kchinnam
1 Replies

2. Shell Programming and Scripting

awk substitution

Hi all, I need some help with substitution in awk. Is it possible to substitute field from awk output with string from file? For example: zcat /SMS/CDR/cdr_TC/callLogs*_*_2013092710*.gz | sed 's/:/;/g' | awk -F";" '{if($2==1 && $10~/389123456789/) print $36";"$37}' 2;19733248 I want... (6 Replies)
Discussion started by: vasil
6 Replies

3. Shell Programming and Scripting

Parameter substitution with##

Hi experts I want to use the parameter substitution in the bash with ## to get a=mfs1000 (not the "mfs" maybe other string and the length is not the same" I want to get 1000 any help? I don't know use which pattern I use echo ${a##*} It doesn't work Lei (5 Replies)
Discussion started by: yanglei_fage
5 Replies

4. Shell Programming and Scripting

Substitution in AWK

I am trying to use AWK to replace dallinux02 to dallinux03 everywhere in the servers.txt file and move it over to "awk2". Here is my script "awk2.awk": gsub(/dallinux02/, "dallinux03"); print > "awk2" I am trying to run this using the following: $ awk -f awk2.awk... (3 Replies)
Discussion started by: ora_umair
3 Replies

5. UNIX for Dummies Questions & Answers

Parameter substitution with alias

Hello, in my .bashrc I tried to setup some aliases. alias scp_cmd="scp -P 8888 $1 me@somehost:." is supposed to copy a local file to somehost via scp. However it seems that the command line substitution does not work here. However this works: alias lst="ls -l $1" The above scp command can... (1 Reply)
Discussion started by: strobotta
1 Replies

6. Shell Programming and Scripting

Substitution using awk/gawk

Hello, I have a file containing lines such as: (1 104 (16) (17) (18) (102))$ (1 105 (16) (17) (19:21) (102))$ I would like to extract the numbers, only by using awk (or gawk). I do not want to use "sed" as it is very slow. For now my solution consists in... (2 Replies)
Discussion started by: jolecanard
2 Replies

7. Shell Programming and Scripting

help with awk substitution

Hi again. A have a CSV-file in the following format: 2008.09.01,15:17:42,9227096485,9233175320,CTC10,SMS,0901151742098314,Target_MSIS DN_is_blacklisted I want to have an awk command that will say: If the first 3 digits of $4 does not begin with 922 or 923, then make $8 say "Invalid... (3 Replies)
Discussion started by: daytripper1021
3 Replies

8. Shell Programming and Scripting

AWK substitution

I need to copy field 2 to field 3 for only those records that have the 1st field equal to account e.g. file account|123|789|xxx|yyy|zzz|... account_group|444|555|xxx|yy|zz|.... account|456|901|aaa|bbb|ccc|..... after running awk script should look like account|123|123|xxx|yyy|zzz|...... (4 Replies)
Discussion started by: klut
4 Replies

9. Shell Programming and Scripting

Filed substitution with awk

guys, I'm trying to 9k lines of the following: aaa aaa 1 1 1 to aaa aaa 1 01 1 Im pretty ignorant when it comes to subtituting fields using awk any help ? Tony (1 Reply)
Discussion started by: tony3101
1 Replies

10. UNIX for Dummies Questions & Answers

awk variable substitution

for the command below, it looks for the 3rd field value matching "P" and printing it. awk '{if ($3 == "P") print}' file how would i express this if i use a loop to find more that 1 variable fro a list? this doesn't seem to work... cat list | while read n do awk '{if ($3 == "$n") print}'... (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question