Perl calls the method of external command (g) awk

Time:2022-5-7

However, awk, a small tool commonly used in Linux system, is inherently friendly to column operation. Sometimes it is more convenient and effective to call awk directly in Perl than to write the corresponding Perl script. For example, insert awk statement with system:

 

Copy codeThe code is as follows:

system(gawk ‘{print $1}’ $filename);


Execute this L statement, and you will find that this line will report an error, prompting you to use the read only variable $1.
So how to deal with the conflict between the two ecosystems?
There is a simple solution:

 

 

Copy codeThe code is as follows:

my $i=”$1″;
system(gawk ‘{print $i}’ $filename);

 

This solves the conflict between (g) awk’s $1 and Perl’s built-in variable $1.