How do i find "&variable[0]" using grep -r?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do i find "&variable[0]" using grep -r?
# 8  
Old 08-05-2014
Quote:
Originally Posted by Scrutinizer
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:
Code:
find . -name '*.c' -exec grep -E '&[[:alpha:]]+\[[0-9]+\]' {} +

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:
Code:
find . -name '*.c' -exec grep -E '&[[:alpha:]]+\[[0-9]+\]' /dev/null {} +

Does any of your code contain array variables with underscores or digits in their names? If so, maybe you want:
Code:
find . -name '*.c' -exec grep -E '&[_[:alpha:]][_[:alnum:]]*\[[0-9]+\]' /dev/null {} +

This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 08-05-2014
How do i find "&variable[0]" using grep -r?

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:
Code:
$ cat testdir/infile1
x
$ grep x ./testdir/infile1 
x
$ find . -name 'i*1' | xargs grep 'x'
./testdir/infile1:x
$ find . -name 'i*1' -exec grep 'x' {} +
./testdir/infile1:x
$ find . -name 'i*1' -exec grep 'x' {} \;
x

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..
# 10  
Old 08-05-2014
Quote:
Originally Posted by Scrutinizer
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:
Code:
$ cat testdir/infile1
x
$ grep x ./testdir/infile1 
x
$ find . -name 'i*1' | xargs grep 'x'
./testdir/infile1:x
$ find . -name 'i*1' -exec grep 'x' {} +
./testdir/infile1:x
$ find . -name 'i*1' -exec grep 'x' {} \;
x

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:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

find files in sub dir with tag & add "." at the beginning [tag -f "Note" . | xargs -0 {} mv {} .{}]

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)
Discussion started by: Nexeu
6 Replies

2. Shell Programming and Scripting

Using "Find" & Storing To Multiple Variables

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)
Discussion started by: alphekka
2 Replies

3. Shell Programming and Scripting

finding the strings beween 2 characters "/" & "/" in .txt file

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)
Discussion started by: Behrouzx77
10 Replies

4. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

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)
Discussion started by: jcdole
4 Replies

5. UNIX for Dummies Questions & Answers

find/xargs/*grep: find multi-line empty "try-catch" blocks - eg, missing ; not in a commented block

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)
Discussion started by: lifechamp
0 Replies

6. Shell Programming and Scripting

Problem with "find" and "grep" command

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)
Discussion started by: pstein
5 Replies

7. Shell Programming and Scripting

ps -ef | grep "string1" "string2" " "string3"

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)
Discussion started by: steve2216
11 Replies

8. Shell Programming and Scripting

Grep & EMail "HOW to quit ??? "

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)
Discussion started by: wakhan
1 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

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)
Discussion started by: Lokesha
4 Replies

10. Shell Programming and Scripting

grep to find content in between curly braces, "{" and "},"

problem String ~~~~~~~~~~~~~~~~~~ icecream= { smart peopleLink "good" LC "happy" , smartpeopleLink "dull" LC "sad" } aend = {smart vc4 eatr kalu} output needed ~~~~~~~~~~~~~~~~~~ smart peopleLink "good" LC "happy" , smartpeopleLink "dull" LC "sad" smart vc4... (4 Replies)
Discussion started by: keshav_rk
4 Replies
Login or Register to Ask a Question