Customized text searches by using grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Customized text searches by using grep
# 1  
Old 02-12-2018
Customized text searches by using grep

I tried to ease text searches so made a customized grep:

Code:
g () {
if [ -n "$2" ]
then
 i=
 for s in $2
 do
	i="$i --include=*.$s"
 done
 else
	i='--include=*.txt --include=*.ini --include=*.*sh --include=*.c* --include=*.h --include=*.js --include=*.reg'
fi
grep -P -e \'$1\' -r "$i"
}

but I used it then

Code:
$ g sys ini
grep:  --include=*.ini: No such file or directory

if I insert 'echo' to last line for tracking and test:

Code:
echo grep -P -e \'$1\' -r "$i"

then reload the alias file and get to shell,

Code:
$ g sys ini
grep -P -e 'sys' -r  --include=*.ini

it'll echo correctly to become perfectly a grep command line...

Any sincere guide to help me out by which we all have benefits ?
# 2  
Old 02-12-2018
What's your OS and grep version?

Howsoever, try again with the double quotes around $i removed.

Last edited by RudiC; 02-12-2018 at 07:46 AM..
# 3  
Old 02-12-2018
What output do you get if you add a set -x statement before the first if? It should show you what it's doing and why.


Does this help? Paste the output back in here if it is confusing and we can work through it.




Kind regards,
Robin
# 4  
Old 02-12-2018
I doubt this will work as intended the --include= option is supposed to filter the filenames passed to grep. However, your g() function doesn't pass any filenames on the grep command.

As RudiC pointed out, the double quotes are causing the --include= option to be treated as a filename for grep.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Customized command excution

Hi, The below commands will be inside a KSH script and SOLARIS machine. Command-1 //Executes in 10s,5s,..5mins,etc TIMER = Look for Command-1 execution status - IF finished in 25secs move to next command in the script IF NOT kill above command and move to next command. Command-2 Any... (9 Replies)
Discussion started by: penqueen
9 Replies

2. Shell Programming and Scripting

Need help in awk for multiple searches

I have a below file RCS File name : abc.txt something something .... symbolic names: implemented : 1.1 ssssssumthing Revision 1.2 date : 12/12/12 author : abc Revision 1.1 date : 11/11/11 author xyz So now , in this file i have to first look for the implemented... (1 Reply)
Discussion started by: ashishagg2005
1 Replies

3. Shell Programming and Scripting

Grep text matching problem with script which checks if web page contains text.

I wrote a Bash script which checks to see if a text string exists on a web page and then sends me an email if it does (or does not e.g. "Out of stock"). I run it from my crontab, it's quite handy from time to time and I've been using it for a few years now. The script uses wget to download an... (6 Replies)
Discussion started by: gencon
6 Replies

4. Forum Support Area for Unregistered Users & Account Problems

customized username?

Hi i have registered but my username is set to default value ( my email). can i change this ? (2 Replies)
Discussion started by: customizeemai
2 Replies

5. Shell Programming and Scripting

Can nmon be customized?

Hi, my name is Steve Ngai from Malaysia. This is my first post. Hope to learn more about Unix from this forum. My first question is can nmon be customized? When I run nmon, I need to manually type c to see CPU usage, then m for memory usage. Can I pass it some nmon option to automatically see... (2 Replies)
Discussion started by: ngaisteve1
2 Replies

6. Shell Programming and Scripting

Customized copy.

HI guys, I'm working on a code with the following specs: 1. Retrieving files with a particular extensions from a location specified by the users. 2. Copying those files to user specified location i) but i need to pause copy if the network is busy ii) and the copy process must... (3 Replies)
Discussion started by: bill88
3 Replies

7. Shell Programming and Scripting

Perl syntax for sed searches

I am aware that Perl has a lot of features that originally came from sed and awk. I have a pattern that I am using like this: sed -n '/|Y|/p' I want to do the same thing in Perl and be able to either save that value in some kind of variable or array or potentially write it out to a file. ... (11 Replies)
Discussion started by: masinick
11 Replies

8. Shell Programming and Scripting

Creating searches?

Hello. Could do with some help on where to get started really. If anyone could help me it would be greatly appreciated. I have been working on this for a while now and I don't really know where to start but I am looking into creating a script that will process website hit files and output... (2 Replies)
Discussion started by: amatuer_lee_3
2 Replies

9. UNIX for Dummies Questions & Answers

grep multiple text files in folder into 1 text file?

How do I use the grep command to take mutiple text files in a folder and make one huge text file out of them. I'm using Mac OS X and can not find a text tool that does it so I figured I'd resort to the BSD Unix CLI for a solution... there are 5,300 files that I want to write to one huge file so... (7 Replies)
Discussion started by: coppertone
7 Replies

10. UNIX for Dummies Questions & Answers

grep: do multiple searches?

I want to search the file /etc/passwd for all lines containing 'csh' but exlude all those lines that have '/usr' in them and dump the results into the file result. IMPORTANT: I need to do this in one command line. The following does not work: grep -v \(\/usr\) \(csh\) /etc/passwd >... (4 Replies)
Discussion started by: sdemba
4 Replies
Login or Register to Ask a Question