Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Print file name when running grep from within find Post 302732627 by Don Cragun on Sunday 18th of November 2012 08:23:31 PM
Old 11-18-2012
Quote:
Originally Posted by sudon't
Or you could use the -H flag with grep, which will return both the filename and the match. I believe the -l flag stops at the first match, which may or may not be what you want.
The -H option is not in the standards and is not implemented on several UNIX and UNIX-like systems. (I don't think it is available on Solaris 10 systems.)

The -l option not only stops when it finds the first match, it only prints the name of the file containing the match (not the contents of the matching line).

A portable way to be sure the filename is printed is to be sure that at least two files are passed as operands to grep. For this case, especially if you have a lot of files, a better command line might be:
Code:
find . -name "findme*" -exec grep "roses" /dev/null {} +

which will call grep with several pathnames as long as {ARG_MAX} limits aren't exceeded. Adding /dev/null supplies a pathname that will never match any selected line and guarantees that at least two operands are given so grep will precede each matched line with the name of the file containing the matched line.
These 4 Users Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Can we grep a list of all running PIDs in a file !!??

Hi, In the following part of a script, I am grepping the list of all running PIDs in the File as in line 3 :- $pid_count=`grep -c "^${pid_process}$" $CRI_PUSH_BIN_HOME/bin/ PushProcessId` If I cannot grep this way, then how can I do so. 1 pid_process=`ps -ef -o pid,args |... (1 Reply)
Discussion started by: marconi
1 Replies

2. UNIX for Dummies Questions & Answers

find file grep it and print file name

i am trying to search a few hundred release note text files for a certain word. however when i use the below command i can find a file that contains it but i dont know the file name. how can i change this command to output the name of the file that grep was successful in? find builds -name... (4 Replies)
Discussion started by: borderblaster
4 Replies

3. UNIX for Dummies Questions & Answers

find locked files, print file path, unlock file

using OS X and the Terminal, I'd like to find all locked files in a specified directory, unlock them, and print a list of those files that were unlocked how can I do this? I'm familiar with chflags nouchg for unlocking one file but not familiar with unix enough to do what I'd like. Thanks! (0 Replies)
Discussion started by: alternapop
0 Replies

4. Shell Programming and Scripting

find file and print only contents with a hit by grep

Hi, can someone help me. I have some files and search a content in this files. If i have a hit I will print a output: filename:content But are more hits in one file: The output is always filename:content E.G. Seach about "three" file1 {one, two, three, four, three} file2... (5 Replies)
Discussion started by: Timmää
5 Replies

5. Shell Programming and Scripting

Find a string using grep & print the line above or below that.

Hi All, Please tell me how can I Find a string using grep & print the line above or below that in solaris? Please share as I am unable to use grep -A or grep -B as it is not working on Solaris. (10 Replies)
Discussion started by: Zaib
10 Replies

6. Shell Programming and Scripting

grep/awk to only print lines with two columns in a file

Hey, Need some help for command to print only lines with two columns in a file abc 111 cde 222 fgh ijk 2 klm 12 23 nop want the ouput to be abc 111 cde 222 ijk 2 Thanks a lot in advance!!! (3 Replies)
Discussion started by: leo.maveriick
3 Replies

7. Shell Programming and Scripting

How to use grep & find command to find references to a particular file

Hi all , I'm new to unix I have a checked project , there exists a file called xxx.config . now my task is to find all the files in the checked out project which references to this xxx.config file. how do i use grep or find command . (2 Replies)
Discussion started by: Gangam
2 Replies

8. Shell Programming and Scripting

Grep or print each section of a file on one line with a separator

I can obtain information from itdt inventory command however it display as below, I'd like to print each entity on one line but seperated by : the file is something like and each section ends with Volume Tag Drive Address 256 Drive State ................... Normal ASC/ASCQ... (3 Replies)
Discussion started by: gefa
3 Replies

9. Shell Programming and Scripting

Grep in file and print in the line

hi # cat test.txt Test Date: 20131008 1515 -------------------------------------------------------------------------------------------------------------- Saxx = Proc_m0_s13 : 1640 Saxx = Proc_m0_s15 : 1791 Saxx = Proc_m0_s17 ... (2 Replies)
Discussion started by: justbow
2 Replies

10. Shell Programming and Scripting

Grep/print/ a test file

cat abc.txt Filename: SHA_AED_Monthly_SNR_20150331.txt.gz Data Format: ASCII with carriage returns and linefeeds Compression: GZIP GZIP Bytes: 36893068 Unzipped Bytes : 613794510 Records: 851310 Record Length: 738 Blocksize: 32472 Filename: SHA_AED_SNR_ChangeLog_20150331.txt.gz Data... (16 Replies)
Discussion started by: dotran
16 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 01:09 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy