Search only first line in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search only first line in awk
# 1  
Old 08-01-2011
Search only first line in 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 . Smilie

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


Thanks.
# 2  
Old 08-01-2011
Here is one way of doing it:
Code:
find . -name "*.sh" -exec sed -n '/perl/p;2q' {} \; | wc -l

# 3  
Old 08-01-2011
Any idea using awk ?

I am sure there are several ways of doing this like one of yours, but I am looking for an awk solution (learning pupose)








Thanks.
D
# 4  
Old 08-01-2011
Yes, use this one:

Code:
... |
  awk 'FNR == 1 && /perl/'

If you have GNU awk:


Code:
... |
  awk 'FNR == 1 && /perl / { print; nextfile }'

This User Gave Thanks to radoulov For This Post:
# 5  
Old 08-01-2011
@raduolov : Worked , exactly I was looking for this .


Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk variable search and line count between variable-search pattern

Input: |Running the Rsync|Sun Oct 16 22:48:01 BST 2016 |End of the Rsync|Sun Oct 16 22:49:54 BST 2016 |Running the Rsync|Sun Oct 16 22:54:01 BST 2016 |End of the Rsync|Sun Oct 16 22:55:45 BST 2016 |Running the Rsync|Sun Oct 16 23:00:02 BST 2016 |End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
Discussion started by: busyboy
4 Replies

2. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

3. Shell Programming and Scripting

awk to search for pattern and remove line

I am an awk beginner and need help figuring out how to search for a number in the first column and if it (or anything greater) exists, remove those lines. AM11400012012 2.26 2.12 1.98 2.52 3.53 3.01 3.62 5.00 3.65 7.95 0.79 3.88 0.00 AM11400012013 3.39 2.29 ... (1 Reply)
Discussion started by: ncwxpanther
1 Replies

4. Shell Programming and Scripting

Multiple Line awk search and replace

I have a log file that contains many lines but contains the following line three times: related_pin : "t_bypass"; Here are the 3 occurrences and the two lines after from my file.txt: related_pin : "t_bypass"; sdf_cond : "rstq_b"; timing_sense : negative_unate; ... (6 Replies)
Discussion started by: bobbygb2003
6 Replies

5. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

6. Shell Programming and Scripting

AWK/SED line based search

Hi, I have a file with values like this 1 11 2 11 3 44 4 55 5 66 (an representative of what I have). I want to split this file into smaller files based on column 1 values (values within a range). The issue that I am facing is that the file is really big, and takes too long to... (21 Replies)
Discussion started by: new_one
21 Replies

7. Shell Programming and Scripting

awk multiple-line search and replace one-liner

Hi I am trying to search and replace a multi line pattern in a php file using awk. The pattern starts with <div id="navbar"> and ends with </div> and spans over an unknown number of lines. I need the command to be a one liner. I use the "record separator" like this : awk -v... (8 Replies)
Discussion started by: louisJ
8 Replies

8. Shell Programming and Scripting

Using Awk to Search Two Strings on One Line

If i wanted to search for two strings that are on lines in the log, how do I do it? The following code searches for just one string that is one one line. awk '/^/ {split($2,s,",");a=$1 FS s} /failure agaf@fafa/ {b=a} END{print b}' urfile What if I wanted to search for "failure agaf@fafa"... (3 Replies)
Discussion started by: SkySmart
3 Replies

9. Shell Programming and Scripting

awk print second line after search string

I have multiple config files where I need to pull the ip address from loopback3. The format is the same in every file, the ip is the second line after interface loopback3. interface loopback2 loopback description router ID ip address 192.168.1.1 interface loopback3 loopback description... (3 Replies)
Discussion started by: numele
3 Replies

10. Shell Programming and Scripting

awk search column, print line

Hello. I've been banging my head against walls trying to search a comma delimited file, using awk. I'm trying to search a "column" for a specific parameter, if it matches, then I'd like to print the whole line. I've read in multiple texts: awk -F, '{ if ($4 == "string") print $0 }'... (2 Replies)
Discussion started by: Matthias03
2 Replies
Login or Register to Ask a Question