Sponsored Content
Top Forums Shell Programming and Scripting Recursive find / grep within a file / count of a string Post 302738621 by RudiC on Sunday 2nd of December 2012 05:57:08 AM
Old 12-02-2012
Thank you, Don, for commenting on my proposal.
Quote:
Originally Posted by Don Cragun
. . . Since no pathname operands are given to awk, all of the filenames printed by awk will be an empty string.
At least with the combination of find and awk implemented on my linux system, there's a full path listing avalable, including filenames containing spaces:
Code:
find /var/log -iname \*.log -print0 |xargs -0 awk  -F\| 'FNR==1 {print FILENAME, NF}'
/var/log/auth.log 1
/var/log/dist-upgrade/history.log 0
. . .
/var/log/x y.log 3
/var/log/kern.log 1

Quote:
And, if there are x field separators on a line, there are x+1 fields.
Yes. Still I thought the number of fields to be more relevant than the number of separators. Might have been premature.

Quote:
Code:
find . -name 'FILENAME.TXT' -exec awk -F'|' 'FNR==1{printf("%s %d\n", FILENAME, NF-1)}' {} +

Works, and satisfies the standards, but:
Code:
time find . . . -print0 |xargs -0 awk  -F\| '. . .'
real    0m0.034s
time find . . . -exec awk -F\| '. . .' {} \;
real    0m0.208s

Quote:
Some implementations of awk have a nextfile statement
Special thanks for this; I was looking for that or an equivalent; unfortunately not available on my system.
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

find file with date and recursive search for a text

Hey Guyz I have a requirement something like this.. a part of file name, date of modification of that file and a text is entered as input. like Date : 080206 (MMDDYY format.) filename : hotel_rates text : Jim now the file hotel_rates.ZZZ.123 (creation date is Aug 02 2006) should be... (10 Replies)
Discussion started by: rosh0623
10 Replies

2. UNIX for Dummies Questions & Answers

to grep and find the count

Hi My files is like a|test|s| b|test2 | n| c|ggg|v| i want to count the no of lines which is ending with "|" ... Please let me know how can it be done. Thanks, Arun (4 Replies)
Discussion started by: arunkumar_mca
4 Replies

3. UNIX for Dummies Questions & Answers

Grep and count the string in a file.

Hi, I have to grep a word 'XYZ' from 900 files ( from 2007 till date), take its count month wise. The output should display month, count , word 'XYZ' . I tried searching the forum for solution but could find any. I would apprieciate if any one can help me asap .... Many Thanks:) (12 Replies)
Discussion started by: vikram2008
12 Replies

4. Shell Programming and Scripting

Grep string from logs of last 1 hour on files of 2 different servers and calculate count

Hi, I am trying to grep a particular string from the files of 2 different servers without copying and calculate the total count of its occurence on both files. File structure is same on both servers and for reference as follows: 27-Aug-2010... (4 Replies)
Discussion started by: poweroflinux
4 Replies

5. Shell Programming and Scripting

How to find the latest file on Unix or Linux (recursive)

Hi all, I need to get the latest file. I have found this command "ls -lrt" that is great but not recursive. Can anyone help? Thanx by advance. (7 Replies)
Discussion started by: 1or2is3
7 Replies

6. Shell Programming and Scripting

Tricky recursive removal (find with grep)

Tricky one: I want to do several things all at once to blow away a directory (rm -rf <dir>) 1) I want to find all files recursively that have a specific file extension (.ver) for example. 2) Then in that file, I want to grep for an expression ( "sp2" ) for example. 3) Then I want to... (1 Reply)
Discussion started by: jvsrvcs
1 Replies

7. UNIX for Dummies Questions & Answers

Recursive Find on file size

Is there a way to use the find command to recursively scan directories for files greater than 1Gb in size and print out the directory path and file name only? Thanks in advance. (6 Replies)
Discussion started by: jimbojames
6 Replies

8. Shell Programming and Scripting

Recursive search for string in file with Loop condition

Hi, Need some help... I want to execute sequence commands, like below test1.sh test2.sh ...etc test1.sh file will generate log file, we need to search for 'complete' string on test1.sh file, once that condition success and then it should go to test2.sh file, each .sh scripts will take... (5 Replies)
Discussion started by: rkrish123
5 Replies

9. Shell Programming and Scripting

Help with Passing the Output of grep to sed command - to find and replace a string in a file.

I have a file example.txt as follows :SomeTextGoesHere $$TODAY_DT=20140818 $$TODAY_DT=20140818 $$TODAY_DT=20140818I need to automatically update the date (20140818) in the above file, by getting the new date as argument, using a shell script. (It would even be better if I could pass... (5 Replies)
Discussion started by: SriRamKrish
5 Replies

10. Shell Programming and Scripting

Grep a string and count following lines starting with another string

I have a large dataset with following structure; C 0001 Carbon D SAR001 methane D SAR002 ethane D SAR003 propane D SAR004 butane D SAR005 pentane C 0002 Hydrogen C 0003 Nitrogen C 0004 Oxygen D SAR011 ozone D SAR012 super oxide C 0005 Sulphur D SAR013... (3 Replies)
Discussion started by: Syeda Sumayya
3 Replies
ZGREP(1)						      General Commands Manual							  ZGREP(1)

NAME
zgrep - search possibly compressed files for a regular expression SYNOPSIS
zgrep [ grep_options ] [ -e ] pattern filename... DESCRIPTION
Zgrep invokes grep on compressed or gzipped files. These grep options will cause zgrep to terminate with an error code: (-[drRzZ]|--di*|--exc*|--inc*|--rec*|--nu*). All other options specified are passed directly to grep. If no file is specified, then the standard input is decompressed if necessary and fed to grep. Otherwise the given files are uncompressed if necessary and fed to grep. If the GREP environment variable is set, zgrep uses it as the grep program to be invoked. EXIT CODE
2 - An option that is not supported was specified. AUTHOR
Charles Levert (charles@comm.polymtl.ca) SEE ALSO
grep(1), gzexe(1), gzip(1), zdiff(1), zforce(1), zmore(1), znew(1) ZGREP(1)
All times are GMT -4. The time now is 12:13 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy