LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-01-2011, 12:51 PM   #1
debianD
Member
 
Registered: Jul 2009
Posts: 37

Rep: Reputation: 16
How to search a pattern only first line using awk


Hi ,

I know this is a noob question but I am not able to find a solution .

I have many scripts which are either shell or perl scripts .
I want to count the number of perl scripts . I am searching the first line from all the files

This is my idea
Code:
find . -type f -perm -700 | xargs awk 'NR == 1 && /perl/ {print $0}'
This is not giving any output .

How can I search a pattern only in the first line of several files using awk.


Thanks.
 
Old 08-01-2011, 01:24 PM   #2
droyden
Member
 
Registered: Feb 2007
Location: UK
Posts: 150

Rep: Reputation: 19
Try a while loop or -exec with find

Find . Blah ¦ while read I;do awk etcetc "$I"; done

Sorry crappy formatting I'm on my phone
 
Old 08-01-2011, 01:53 PM   #3
karthik3152
LQ Newbie
 
Registered: May 2011
Posts: 10
Blog Entries: 3

Rep: Reputation: Disabled
Hi
You can use a simple perl program:

#!/usr/bin/perl
@aa=`find . -t-name '*'.sh -o '*'.pl `;
$dd=scalar(@aa);$dd will give no of perl files or script files
foreach $zx(@aa)
{
$line1=`cat $zx|head -n 1`; # $line1 gives the first line
print $line1 if $line1 =~ /pattern/; # substitute the pattern you want to match
}

Hope this solves the problem.
For further solutions you can visit my blogspot:
http://linux-forum-karthik.blogspot.com/
 
Old 08-01-2011, 02:01 PM   #4
debianD
Member
 
Registered: Jul 2009
Posts: 37

Original Poster
Rep: Reputation: 16
Thanks for the reply.

But I want to avoid any loop , using exec is the same as using xargs as I did in my code .

Any other idea.

@Karthink : I am sure this can be done in that way , I am looking for one line , just wondering if awk has a quick solution or not . Certainly there are other ways also.

Last edited by debianD; 08-01-2011 at 02:05 PM.
 
Old 08-01-2011, 03:06 PM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Since xargs will pass several file names to your AWK program, I suspect that your NR==1 test will only succeed for the first record of the first file in any batch. Perhaps a FNR==1 might be a better choice. And, if I were trying this, I'd want to print the FILENAME rather than the first record of the file. Perhaps you always keep the file name in there?
 
1 members found this post helpful.
Old 08-01-2011, 04:22 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by debianD View Post
using exec is the same as using xargs as I did in my code .
This is not true. xargs executes the command once and puts the arguments all together, whereas -exec executes the command multiple times, once per each file. In this case NR == 1 will be okay.
Code:
find . -type f -perm -700 -exec awk 'NR == 1 && /perl/ {print FILENAME}' {} \;
If you use xargs, you need FNR == 1, as PTrenholme already explained:
Code:
find . -type f -perm -700 | xargs awk 'FNR == 1 && /perl/ {print FILENAME}'

Last edited by colucix; 08-01-2011 at 04:23 PM.
 
1 members found this post helpful.
Old 08-01-2011, 07:10 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
May I also suggest adding 'nextfile' after your print statement, otherwise you will scan the full length of every file:
Code:
find . -type f -perm -700 | xargs awk 'FNR == 1 && /perl/ {print FILENAME}{nextfile}'
 
Old 08-02-2011, 12:58 PM   #8
debianD
Member
 
Registered: Jul 2009
Posts: 37

Original Poster
Rep: Reputation: 16
@colucix : Thanks , I will keep that in mind.

@grail : Does not the search is only restricted in the first line because of FNR == 1 , do I really need to do nextfile ?








Thanks.
D
 
Old 08-02-2011, 08:19 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
FNR == 1 says that only to do what is between the curly braces for this expression. That means for all lines that do not match the expression it will do nothing, but this does not stop them from being read.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple pattern search in a single file line by line saheervc Linux - Newbie 2 09-01-2010 11:45 PM
search for a pattern and comment the line swaroop.tata Programming 2 09-04-2009 07:23 PM
How to use variables in search pattern in gensub function of awk rajeshksv Linux - Newbie 1 08-07-2009 07:07 AM
replace a pattern with a line using sed/awk lokeshn05 Linux - Newbie 3 05-06-2009 03:01 PM
Pattern search in a line jitz Linux - General 2 12-06-2003 04:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration