INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

pattern matching and print filename

pattern matching and print filename

(OP)

CODE -->

awk '/ pattern/ {show[NR+2]++} show[NR]' *.txt

The above code search for pattern in several text files and when pattern is found than prints the 2'nd row after.
How do I add the filename to be printed in the same line with the pattern?

Thanks in advance

RE: pattern matching and print filename

Hi

Not sure what you mean by "in the same line with the pattern". The pattern is never printed there.

CODE --> Awk

awk '/ pattern/ {show[NR+2]++} show[NR] {print FILENAME,$0}' *.txt
Tested with gawk and mawk.

Feherke.
http://feherke.github.com/

RE: pattern matching and print filename

(OP)
Thank you for your respond,
Yes you are right not the pattern gets diplayed, it get displayed the 2'nd row after matching the pattern.
The script works fine, I need only a small tweak if it's possible. The filename get's duplicated as follows:

CODE -->

file1.txt 0 file1.txt 2'nd row after pattern file2.txt 0 file2.txt 2'nd row after pattern file3.txt 0 file3.txt 2'nd row after pattern

RE: pattern matching and print filename

Hi

Not for me. Tell us which Awk implementation are you using and show some exact sample input for which the duplication occurs.

Feherke.
http://feherke.github.com/

RE: pattern matching and print filename

(OP)
Hi,
Please find bellow the input files:

CODE -->

$ cat file1.txt 10% 20% 30% | | | transfered 35168 bytes $ cat file2.txt 10% 20% 30% | | | transfered 435168 bytes $ cat file3.txt 10% 20% 30% | | | transfered 568 bytes $ cat file4.txt 10% 20% 30% | | | transfered 18168 bytes

The script result is:

CODE -->

$ awk '/ 10% 20% 30%/ {print FILENAME , show[NR+2]++} show[NR] {print FILENAME , $0}' *.txt file1.txt 0 file1.txt transfered 35168 bytes file2.txt 0 file2.txt transfered 435168 bytes file3.txt 0 file3.txt transfered 568 bytes file4.txt 0 file4.txt transfered 18168 bytes


I'm using :

CODE -->

$ awk -W version GNU Awk 4.0.1 Copyright (C) 1989, 1991-2012 Free Software Foundation.

Many Thanks

RE: pattern matching and print filename

Hi

That is not the code I posted. If you not want to have FILENAME printed twice, then why you added the extra print FILENAME ?

Feherke.
http://feherke.github.com/

RE: pattern matching and print filename

Dare I suggest:

CODE

$ grep transfered *.txt
?

RE: pattern matching and print filename

(OP)
Sorry for that, the first print was part of my trials,
Thank you for you help.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close