Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have following command:

usb_soundcard_sink=$(pactl list short sinks | grep "alsa_output" | awk '{ print $2 }' | tail -n1)

output of this command is:

alsa_output.pci-0000_00_1b.0.analog-stereo

And this is another command that find index number:

var=$(pactl list short sinks | awk '$2=="alsa_output.pci-0000_00_1b.0.analog-stereo" {print $1}')

output:

0

But I want pass the "usb_soundcard_sink" variable instead of hard coded value i.e. "alsa_output.pci-0000_00_1b.0.analog-stereo" in above command. b'coz value of "usb_soundcard_sink" variable may change dynamically.

Also I tried the following:

var=$(pactl list short sinks | awk '$2=="$usb_soundcard_sink" {print $1}')

But it is not working

so how can I pass value of "usb_soundcard_sink" variable to the above command

share|improve this question
up vote 11 down vote accepted

To use bash variables inside your awk, you have to use the -v syntax like this:

pactl list short sinks | awk -v myvar="$usb_soundcard_sink" '$2==myvar {print $1}'
#                               ^^^^^                            ^^^^^
#                                 |________________________________|

Example

$ a=3
$ awk -v myvar="$a" 'BEGIN{print myvar}'
3
share|improve this answer
    
If you want to pass two variables, make sure to repeat the -v switch, like echo | awk -v myVar1=12 -v myVar2="${usb_soundcard_sink}" '...' – RobertG 18 hours ago

I found that this works too

line=myfile.dat
f=8
s=11
echo $line | awk '{print $'$f',$'$s'}'

Here I have a file called myfile.dat piped into awk which spits out column 8 and 11.

share|improve this answer

Both MAWK (Ubuntu's default AWK version) and GAWK read non-option command line arguments in the form identifier=value as variable assignments, which are performed right after the BEGIN statements have been executed:

% echo | mawk -- '{print var}' var=foo 
foo
% echo | gawk -- '{print var}' var=foo
foo
% echo foo bar baz | mawk -- '{$1=$1}1' OFS=,
foo,bar,baz
% echo foo bar baz | gawk -- '{$1=$1}1' OFS=,
foo,bar,baz

I prefer this method to the -v option because it keeps the command cleaner, clearly separating the options to be passed to the interpreter and the program's details.

From man mawk:

The command line arguments divide into three sets: file arguments, assignment arguments and empty strings "". An assignment has the form var=string. When an ARGV[i] is examined as a possible file argument, if it is empty it is skipped; if it is an assignment argument, the assignment to var takes place and i skips to the next argument; else ARGV[i] is opened for input.

From man gawk:

If a filename on the command line has the form var=val it is treated as a variable assignment. The variable var will be assigned the value val. (This happens after any BEGIN rule(s) have been run.) Command line variable assignment is most useful for dynamically assigning values to the variables AWK uses to control how input is broken into fields and records. It is also useful for controlling state if multiple passes are needed over a single data file.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.