Print rows, having pattern in specific column...

You are right, leaving out next only works in this case because the column number in the pattern file and the input file happen to differ. But even then it causes unnecessary processing.

Yes,
it executes the second action for every record in the first non empty input file also:

zsh-4.3.10[t]% print -l {1..1000}>pattern
zsh-4.3.10[t]% print -l A\ {1..1000}>input  
zsh-4.3.10[t]% head -3 pattern input 
==> pattern <==
1
2
3

==> input <==
A 1
A 2
A 3
zsh-4.3.10[t]% wc -l input pattern 
1000 input
1000 pattern
2000 total
zsh-4.3.10[t]% pgawk>/dev/null 'NR==FNR{_[$1]}$2 in _' pattern input 
zsh-4.3.10[t]% cat awkprof.out 
    # gawk profile, created Tue Oct  6 09:18:58 2009

    # Rule(s)

  2000  NR == FNR    { # 1000
  1000      _[$1]
    }

  2000  $2 in _    { # 1000
  1000      print $0
    }
zsh-4.3.10[t]% pgawk>/dev/null 'NR==FNR{_[$1];next}$2 in _' pattern input
zsh-4.3.10[t]% cat awkprof.out 
    # gawk profile, created Tue Oct  6 09:22:42 2009

    # Rule(s)

  2000  NR == FNR    { # 1000
  1000      _[$1]
  1000      next
    }

  1000  $2 in _    { # 1000
  1000      print $0
    }

Thanks ya, radoulov. I fully understand about it now :slight_smile:
In between, can I know how to print out the backstick(') by using awk?
Actually I want to express the backticks in command and attach it to a file.

For example:
I got a list of file end at .txt. I want all of them do the same command like
grep '^@' and attached it to a output .sh file.

This is the command I type:
ls *.txt | awk '{print "grep \' \^\@\' ",$1}' > txt.sh

My desired output is when I type the command "more txt.sh "
The desired output is like this:
sample1.txt | grep '^@'
sample2.txt | grep '^@'
sample3.txt | grep '^@'
sample4.txt | grep '^@'

I got the problem when trying to show out the ' (backtick) at the txt.sh file.
I used the \' to show out the ' (backticks).
Is't because I use wrong?
Thanks a lot for your advice and suggestion.

Hi Patrick.

There's a number of ways to display single quotes in AWK (these aren't backticks as you ask).

One is:

echo blah | awk '{print SQ $1 SQ}' SQ="'"
'blah'

Another is

echo blah | awk '{print "'"'"'" $1 "'"'"'" }'
'blah'

Yuk!

Actually I got a list of file end with *.txt
I want to use the same command apply to all the *.txt
Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics.

For example:
I got the file below:
file1.txt
file2.txt
file3.txt
file4.txt

I want all the *.txt do the same command like:
awk '{print $1"\t"}'

My desired output is got a script named as txt.sh look like this:
awk '{print $1"\t"}' file1.txt
awk '{print $1"\t"}' file2.txt
awk '{print $1"\t"}' file3.txt
awk '{print $1"\t"}' file4.txt

The way I do now is :
ls *.txt | awk '{print "awk \"{$_\"\\t\"}\"",$1}'

And the output I get is like this:
awk "{print $1"\t"}" file1.txt
awk "{print $1"\t"}" file2.txt
awk "{print $1"\t"}" file3.txt
awk "{print $1"\t"}" file4.txt

If I type something like:
ls *.txt | awk '{print "awk \'{$_\"\\t\"}\'",$1}'
It is not worked at all.

Thus I'm asking whether you know the way to express the single quote by awk print :slight_smile:
thanks again for your suggestion

Why don't you just apply that command directly?

You don't need awk for this (assuming the filenames do not contain embedded new lines):

(
  IFS='
'
printf "awk '{print \$1\"\\\t\"}' %s\n" $(<infile)
  )

And, as I already said, you can achieve the same with:

awk '{ print $1 "\t" }' *txt  

... or use xargs if you have too many files.

awk -vq=\' 'BEGIN {
  for (f=1; f<ARGC; f++)
    print "awk", q "{ print $1 \"\\t\" }" q, ARGV[f]
    }' *txt

For example:

% ls
file1.txt  file2.txt  file3.txt  file4.txt
% awk -vq=\' 'BEGIN {
  for (f=1; f<ARGC; f++)
    print "awk", q "{ print $1 \"\\t\" }" q, ARGV[f]
}' *txt
awk '{ print $1 "\t" }' file1.txt
awk '{ print $1 "\t" }' file2.txt
awk '{ print $1 "\t" }' file3.txt
awk '{ print $1 "\t" }' file4.txt