Note that grep -r statement will not find *.c files in subdirectories. The find statement will also fail to do that if the are any *.c files present in the current directory . In both cases *.c is expanded by the shell and the the result is provided as parameters to the command. To find all *.c files *.c needs to be quoted:
Another thing is that xargs will break things if there are spaces in the filename. The example shows how it could be done without xargs, by using -exec . The + sign makes it at least as efficient as xargs
Note that with either find ... -exec ... + or with xargs, if only one matching file is found or if there are enough matching files to invoke grep multiple times and the last invocation only passes one filename to grep, the filename will not be printed along with the matching line. To protect against this rare case, add another file operand that you know will never match your pattern:
Does any of your code contain array variables with underscores or digits in their names? If so, maybe you want:
This User Gave Thanks to Don Cragun For This Post:
Hi Don, originally I wanted to expand my post with something similar (using -H if the user's grep supports it). With /dev/null option I worried that if the ARG_MAX would be reached with that xargs or + option, ARG_MAX might become exceeded when those arguments are fed to grep together with /dev/null. What are your thoughts on this?
I also did a small test with only one file and I found this, using OSX:
So it would seem that somehow the /dev/null precaution is only necessary when using \;
--EDIT--
Nevermind about that last bit, it seems this is not common behaviour , on other OS it does not print the file name, so this appears to be implementation specific.
Last edited by Scrutinizer; 08-05-2014 at 05:11 AM..
Hi Don, originally I wanted to expand my post with something similar (using -H if the user's grep supports it). With /dev/null option I worried that if the ARG_MAX would be reached with that xargs or + option, ARG_MAX might become exceeded when those arguments are fed to grep together with /dev/null. What are your thoughts on this?
I also did a small test with only one file and I found this, using OSX:
So it would seem that somehow the /dev/null precaution is only necessary when using \;
--EDIT--
Nevermind about that last bit, it seems this is not common behaviour , on other OS it does not print the file name, so this appears to be implementation specific.
According to the standards, grep is not allowed to print the name of the matched file unless two or more file operands are specified; and is required to print the name of the matched file if two or more file operands are specified. (The standards do not specify the -H option, but that is an allowed extension.)
find ... -exec ... + and xargs are required by the standards to limit the command line length such that when the command is invoked, the combined argument and environment lists shall not exceed {ARG_MAX} bytes. Of course, if the environment, fixed arguments, and one pathname exceed {ARG_MAX}; neither find nor xargs will be able to invoke any commands. And, with some options, xargs is also constrained by {LINE_MAX}.
This User Gave Thanks to Don Cragun For This Post:
I am trying find files in sub dir with certain tags using tag command, and add the period to the beginning. I can't use chflags hidden {} cause it doesn't add period to the beginning of the string for web purpose. So far with my knowledge, I only know mdfind or tag can be used to search files with... (6 Replies)
Hello all!
I'm pretty new to bash scripting, so this should be a pretty easy question to solve.
For the last few hours, I've been creating a script that will list some of the following (based on a path I specify):
# of directories
# of files
# of executable files
files older than 365... (2 Replies)
Hi all.
I have a .txt file that I need to sort it
My file is like:
1- 88 chain0 MASTER (FF-TE) FFFF 1962510 /TCK T FD2TQHVTT1 /jtagc/jtag_instreg/updateinstr_reg_1 dff1 (TI,SO)
2- ... (10 Replies)
Hello.
Following recommendations for one of my threads, this is working perfectly :
#!/bin/bash
CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" )
Now I need a grep success for some thing like :
#!/bin/bash
CNT=$( grep -c -e "some text_1... (4 Replies)
How can I recursively find all files in a directory and print out the file and first line number of any text blocks that match the below cases?
This would seem to involve find, xargs, *grep, regex, etc.
In summary, I want to find so-called empty "try-catch blocks" that do not contain code... (0 Replies)
I want to list all files/lines which except those which contain the pattern ' /proc/' OR ' /sys/' (mind the leading blank).
In a first approach I coded:
find / -exec ls -ld {} | grep -v ' /proc/| /sys/' \; > /tmp/list.txt
But this doesn't work. I got an error (under Ubuntu):
grep:... (5 Replies)
Hi all,
can any one suggest me the script to grep multiple strings from ps -ef
pls correct the below script . its not working/
i want to print OK if all the below process are running in my solaris system. else i want to print NOT OK.
bash-3.00$ ps -ef | grep blu
lscpusr 48 42 ... (11 Replies)
Hello All,
I am using the below code to grep particular word from file and then emailing it through mail command. the problem is this that when i run the script so it stops and ask me for the mail body then it asks for cc: and then runs.
I dont want to give body and cc: address, i just want... (1 Reply)
Hi Friends,
Can any of you explain me about the below line of code?
mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`
Im not able to understand, what exactly it is doing :confused:
Any help would be useful for me.
Lokesha (4 Replies)