AWK: list files with 1rst col=N and char position 123=N

I need to list all files where 1rst column=ABK and char position 123 to 125=ZBK:

For the first part I can I can do a awk '{$1="ABK";print}' file and for the second a cut -c123-125 file | grep ZBK but this would only work partially..

How can I do this with only one awk command ?

Thanks in advance,

awk ' $1="ABK" && substr($0,123,1)=="N"  ' file > newfile
awk '$1 == "ABK" && index($0, "ZBK") == 123' infile

That worked perfectly as I expected thanks a lot for both answers!

I have just one more question: Since I have hundred of files instead of doing your awk command file, I would like to run it to all files (awk command *) and therefore I would also need to list/print all files containing the result of my awk command.

May you help me one more time ?

Thanks once again

hi,
I am the new member, I want to place a question on shell scripting, can you please tell me to options to place my post.

Thanks

Use this:

awk 'FNR == 1 { c = 0 }
$1 == "ABK" && index($0, "ZBK") == 123 {
  c++ || $0 = FILENAME RS $0; print 
  }' *

Or this:

awk '$1 == "ABK" && index($0, "ZBK") == 123 { 
  print FILENAME, $0 
  }' *

---------- Post updated at 07:25 PM ---------- Previous update was at 07:20 PM ----------

Welcome to UNIX.COM!
Go to the Shell Programming and Scripting forum and use the new thread button in the upper left corner.

... and don't forget to read the forum rules.

I have nothing more to say..., just a BIG BIG THANK YOU radoulov!

Thanks for your great help :wink:

I corrected the first one (you can use it if you want to print the filename once per input file).
And yet another version:

awk '$1 == "ABK" && index($0, "ZBK") == 123 {
  _[FILENAME]++ || $0 = FILENAME RS $0; print 
  }' *

By Sed

sed -n '/^ABK.\{120\}ZBK/p' urfile

There is a bug in my code: the index function will return the string position only for the first match, so if there is a match before the specific position the record will not be printed even if it contains the required data.

So, the OP should use the substr function.

... && substr($0,123,3) == "ZBK"

And the bits are for Jim :slight_smile:

Thanks for your reaction radoulov. As a matter of fact I saw this morning in one of our production servers that yesterday's command only worked in some files.

I'm using substr insted of index in your awk command and for now it seems it's working properly, I'll come back to you if I have other problem.

Many thanks for your help!