Does awk have parameter substitution?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Does awk have parameter substitution?
# 1  
Old 04-30-2015
Does awk have parameter substitution?

Can I specify a default value to a variable in AWK like BASH in one statement using parameter substitution?

BASH example:
Code:
argument=${$1-"default if empty"} (BASH)

I know I can do:
Code:
argument=$1; sub ( "^$", "default if empty", argument) (AWK)

Mike
# 2  
Old 05-01-2015
Well you could us the ? operator like this:

Code:
argument=length($1) ? $1 : "default if empty"

or like this:

Code:
argument=$1~"^$" ? "default if empty" : $1

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 05-01-2015
Quote:
Originally Posted by Michael Stora
Can I specify a default value to a variable in AWK like BASH in one statement using parameter substitution?

BASH example:
Code:
argument=${$1-"default if empty"} (BASH)

First, you have an extra dollar sign (shown in red above) that shouldn't be there.

Second: your default if empty string is misleading. With the form argument=${parm-word}, word is assigned only if parm is unset. If parm is set, but empty; argument will be set to the empty string.

To get what you probably intended (assign word if parm is set to an empty string or if parm is unset); you want:
Code:
argument=${1:-"default if unset or empty"}

Quote:
I know I can do:
Code:
argument=$1; sub ( "^$", "default if empty", argument) (AWK)

Mike
If I understand what you're trying to do (assign an awk variable the value of a shell variable if it is set and not empty, the easy way is to still use shell variable assignment:
Code:
awk -v argument=${1:-"default if unset or empty"} 'BEGIN{print argument}'

But, you can also do it entirely inside an awk script. Compare what happens with the following script:
Code:
#!/bin/ksh
date > now
awk -v v1="v1 -v" '
function setvars() {
	v1 = v1 ? v1 : "v1 unset, empty string, or 0"
	sub("^$", "v2 is unset or empty string", v2)
	printf("fn=%d, FILENAME=\"%s\"\nv1=\"%s\"\nv2=\"%s\"\n\n",
		fn, FILENAME, v1, v2)
}
BEGIN {	setvars()
}
FNR == 1 {
	fn++
	setvars()
	print fn, $0
	print ""
}
END {	print "in END clause"
	setvars()
}' v2='v2 before 1st file operand' now v1= v2= now v1=0 v2=0 now v1=abc \
   v2=def now v1='v1 after last file operand' v2=0

which, with any POSIX conforming shell such as bash or ksh (it has been tested with both) produces the output:
Code:
fn=0, FILENAME=""
v1="v1 -v"
v2="v2 is unset or empty string"

fn=1, FILENAME="now"
v1="v1 -v"
v2="v2 before 1st file operand"

1 Thu Apr 30 20:54:59 PDT 2015

fn=2, FILENAME="now"
v1="v1 unset, empty string, or 0"
v2="v2 is unset or empty string"

2 Thu Apr 30 20:54:59 PDT 2015

fn=3, FILENAME="now"
v1="v1 unset, empty string, or 0"
v2="0"

3 Thu Apr 30 20:54:59 PDT 2015

fn=4, FILENAME="now"
v1="abc"
v2="def"

4 Thu Apr 30 20:54:59 PDT 2015

in END clause
fn=4, FILENAME="now"
v1="v1 after last file operand"
v2="0"

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 05-01-2015
Thanks for pointing out the extra dollar sign. I made a typo while posting.

I am asking if there is an equivilent in awk to the BASH example (only awk variables involved--no shell variables involved).

Edit: I missed that one of your examples was applicable to my code based on my lack of undertanding of how ? worked. I'm glad to learn.

Mike

Last edited by Michael Stora; 05-01-2015 at 04:53 PM..
# 5  
Old 05-01-2015
Quote:
Originally Posted by Michael Stora
Thanks for pointing out the extra dollar sign. I made a typo while posting.

I am asking if there is an equivilent in awk to the BASH example (only awk variables involved--no shell variables involved).

Mike
I'm sorry my crystal ball didn't work.

Giving an awk expression with no context indicating whether the $1 you showed us in your awk expression is a shell command line variable or a field from an input line makes it hard to guess at what you're trying to do.

If you would show us an example of what you're really trying to do instead of giving us part of one line of an awk script and (making us guess at the context) is likely to get you suggestions that will come closer to doing what you want.
# 6  
Old 05-01-2015
I thought the meaning of $1 was pretty unambiguous in AWK.

Here is a little more context:
Code:
 
while (getline < "'"$failListFile"'") { split( $0, a, ","); failMessage[a[1]a[2]a[3]a[4]a[5]]=a[8]
fs=a[6]; sub ( "^$", "0000 01 01 00 00 00", fs ); failStart[a[1]a[2]a[3]a[4]a[5]]=fs
fe=a[7]; sub ( "^$", "9999 12 31 23 59 59", fe ); failEnd[a[1]a[2]a[3]a[4]a[5]]=fe

Trying to do the assignment and default in a single command if possible (possibly analagous to parameter substitution in BASH, if such a thing exists in AWK).

Mike

Last edited by Michael Stora; 05-01-2015 at 02:32 AM..
# 7  
Old 05-01-2015
Quote:
Originally Posted by Michael Stora
I thought the meaning of $1 was pretty unambiguous in AWK.

Here is a little more context:
Code:
 
while (getline < "'"$failListFile"'") { split( $0, a, ","); failMessage[a[1]a[2]a[3]a[4]a[5]]=a[8]
fs=a[6]; sub ( "^$", "0000 01 01 00 00 00", fs ); failStart[a[1]a[2]a[3]a[4]a[5]]=fs
fe=a[7]; sub ( "^$", "9999 12 31 23 59 59", fe ); failEnd[a[1]a[2]a[3]a[4]a[5]]=fe

Trying to do the assignment and default in a single command if possible (possibly analagous to parameter substitution in BASH, if such a thing exists in AWK).

Mike
There is no invocation of awk in this script. There is a reference to a shell variable failListFile (or maybe it is a reference to the current line in awk since the undefined awk variable failListFile with a $ in front of it references the contents of the current input line). There are mismatched braces. There is no description of what you are trying to do with this awk and/or bash script. I have no idea what "assignment and default in a single command" you are trying to perform.

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 haven't explained what you want to do that isn't being done by the sample script I showed you. Since I can't figure out what you're trying to do, I am afraid that I am unable to help you any further.
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