Unix Shell - Question about print from keyword

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2006 > Question about print from keyword





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Question about print from keyword
key9

2006-11-22, 1:39 am

Hi all

Q1. How to locate keyword in an file and print from that line?

Q2. If that keyword is not uniquely, how to print designated one ?
For example , the 4th keyword?


Thank you very much!
key9


Chris F.A. Johnson

2006-11-22, 7:27 am

On 2006-11-22, key9 wrote:
> Hi all
>
> Q1. How to locate keyword in an file and print from that line?


awk '/keyword/,0' FILE

> Q2. If that keyword is not uniquely, how to print designated one ?
> For example , the 4th keyword?


awk '/keyword/ && n++ == 4' FILE

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Janis Papanagnou

2006-11-22, 7:27 am

Chris F.A. Johnson wrote:
> On 2006-11-22, key9 wrote:
>
>
> awk '/keyword/,0' FILE
>
>
> awk '/keyword/ && n++ == 4' FILE


awk '/keyword/ && ++n == 4' FILE

Or if you want to print from the 4th occurrence of the keyword up to
the end of the file then combine the patterns of the two solutions...

awk '/keyword/ && ++n == 4 , 0' FILE


Janis
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com