Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I escape a field variable in an awk command in an alias?

Tags:

escaping

csh

Here are the contents of a file:

one two three
four five six

And here is my alias

alias testawk "awk '{print $2}' file"

This is what I get:

> testawk
one two three
four five six

But when I give this command, then I get what I want:

> awk '{print $2}' file
two
five

How do I escape the field specifier in the alias? NOTE: I'm using csh

like image 942
B Johnson Avatar asked Jun 24 '10 13:06

B Johnson


People also ask

Can I use a variable in an awk command?

Variables are used to store any value temporary in any programming language. Defining the variable in awk command is similar to bash scripting language and it works like bash when the shell variable is used with a single quote and double quote. Awk command has many built-in variables for various purposes.

What does NF mean in awk?

NF is a predefined variable whose value is the number of fields in the current record. awk automatically updates the value of NF each time it reads a record. No matter how many fields there are, the last field in a record can be represented by $NF .

What does $1 $2 indicate in awk file?

The awk variables $1 or $2 through $nn represent the fields of each record and should not be confused with shell variables that use the same style of names. Inside an awk script $1 refers to field 1 of a record; $2 to field 2 of a record.

How do you store awk output in variables?

Passing variable as input & storing output in other variable var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results...


1 Answers

Wrap the alias w/ ' and use '\'' for the embedded '.

alias testawk 'awk '\''{print $2}'\'' file'
like image 80
Louis Marascio Avatar answered Oct 22 '22 12:10

Louis Marascio