Use of grep with multiple parameters in shell scripts


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Use of grep with multiple parameters in shell scripts
# 1  
Old 10-02-2010
Use of grep with multiple parameters in shell scripts

I am learning how to write shell scripts and have come across an issue. I'm trying to write a script that looks for a directory called public_html, and if it finds one, to print the number of lines that contain applet tags (containing '<applet') in all files that end in either .html or .htm that are under the public_html directory . Here's what I have so far:

[ -d public_html ] || echo no website here
[ -d public_html ] && `grep -e .*\<applet.* -e \.html$ -e \.htm$ public_html`

I know that
the first line works, but I have doubts about the second. I'm sure that the problem is in my use of grep, but beyond that I'm stumped.

Any ideas?
# 2  
Old 10-02-2010
Filename globbing is done in the shell, not in grep, and is not a regex. so just use simple shell globbing i.e. public_html/*.htm public_html/*.html
# 3  
Old 10-02-2010
I think I see what you mean. How's this:

[ -d public_html ] && grep public_html/*\<applet* public_html/*.html$ public_html/*.htm$

Also, do I need that backslash before the '<'? I'm not sure if that character requires it.
# 4  
Old 10-02-2010
There's no reason to put 'public_html/' in your regex unless your HTML files actually contain that anywyere, which I sincerely doubt. Search for what you're trying to match inside the file, paths to the file you're searching for don't belong there.

You've put more regex junk back into my shell globs, like an $ to match the end of line. Shell globs continue to not be regular expressions, that syntax is wrong. It's unnecessary anyway since shell globs never return incomplete matches -- the entire filename must always be matched to return a result, '*.htm' will match 'this.htm' but not 'this.html'. I suggest actually using what I posted, no more, no less.

Shell globbing is very simple compared to regular expressions. It does only the following things:
  • Literal characters
  • ? will match any single character(not zero or one like in a regex)
  • A block like [a-z123] will match one single character a through z or 1,2,3.
  • * will match 0 or more characters
No modifiers, no beginning or end of line, no backreferences. That's all you get.

Last edited by Corona688; 10-02-2010 at 07:57 PM..
# 5  
Old 10-03-2010
OK, I think that I'm sterting to get it. How does this look?

[ -d public_html ] && grep public_html/*<applet* public_html/*.htm public_html/*.html public_html
# 6  
Old 10-03-2010
I like to do piped, multiprocessing scripts in ksh:

find ... -type d -name public_html 2>/dev/null | while read d
do
cd $d
echo ===== $d
grep ... *.html *.htm 2>&1
echo
done >list_file
# 7  
Old 10-03-2010
Forgive the endless replies for a simple subject - the text tat I'm working from isn't very clear about... well, anything.

In response to Corona688 - I see what you mean, but I also need to look for '<applet' within the files that grep looks for. Would this do that?

[ -d public_html ] && grep public_html/.*\<applet.* public_html/*.htm public_html/*.html

The more I look at this I can't help but think that I need to enclose part of this in backquotes (`). Just how wrong am I?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple shell scripts executed in one script

Hi every one, i am new to shell script. my people given a task to write a shell script that should execute number of shell scripts in that. in that, if any shell script is failed to execute, we have to run the main script again, but the script should start execute from the failed script only.. it... (6 Replies)
Discussion started by: Madhu Siddula
6 Replies

2. Shell Programming and Scripting

Multiple shell scripts executed in one script

Hi every one, i am new to shell script. my people given a task to write a shell script that should execute number of shell scripts in that. in that, if any shell script is failed to execute, we have to run the main script again, but the script should start execute from the failed script only.. it... (1 Reply)
Discussion started by: Madhu Siddula
1 Replies

3. Homework & Coursework Questions

Help with Shell Scripts Using sed in multiple files.

Hi, I was hoping that someone could help me. I have a problem that i am trying to work on and it requires me to change text within multiple files using sed. What i have so far is !/bin/sh File1="$3" File2="$4" File3="$5" testNum="$File1" while test "$testNum" <= "$File3"; do echo... (12 Replies)
Discussion started by: Johnny2518
12 Replies

4. Shell Programming and Scripting

Help with Shell Scripts Using sed in multiple files.

Hi, I was hoping that someone could help me. I have a problem that i am trying to work on and it requires me to change text within multiple files using sed. I use the program to change an occurance of a word throughout different files that are being tested. At first i had to Create a new script,... (1 Reply)
Discussion started by: Johnny2518
1 Replies

5. Shell Programming and Scripting

shell scripting-processing multiple scripts

I have four scripts to run. My 1st script will make script2 and script3 to run. I am setting a cron job for this script1 to run continuously. This script1 will check for 2 text files and based on the existance of those text files it will initiate the script2 and script3. Now my doubt is that... (2 Replies)
Discussion started by: RSC1985
2 Replies

6. Shell Programming and Scripting

Passing parameters to Shell script for GREP command

I am using grep to capture date from a file . Since i need to use the shell script for different dates ,is it possible to pass the date parameter to the shell script the Script is as below grep -E "08 Aug 2008|2008-08-08"* somefile.txt>test.txt The above script file greps the... (1 Reply)
Discussion started by: sud.tech
1 Replies

7. Shell Programming and Scripting

Manipulating a variable across multiple shell scripts

I have a shell script similar to: #!/bin/sh a=1 source a1.sh -- Modifies a source a2.sh -- Modifies a echo "After execution, value of a is $a" What i need is a1.sh script modify the same variable a and same with a2.sh. Now the echo "After execution, value of a is $a" should print the... (1 Reply)
Discussion started by: indianjassi
1 Replies

8. Shell Programming and Scripting

multiple child scripts running in backgroud, how to use grep on the parent?

Hi I have a shell script A which calls another 10 shell scripts which run in background. How do i make the parent script wait for the child scripts complete, or in other words, i must be able to do a grep of parent script to find out if the child scripts are still running. My Code: ... (5 Replies)
Discussion started by: albertashish
5 Replies

9. Shell Programming and Scripting

shell scripts to grep dn in ldif file

Hi gurus out there, 1)I am using ksh, in solaris 10. 2)I have one ldif file, I need to output user DN with attributes=<some pattern> to a file. Example: dn: uid=joy,ou=People,o=abc.com,o=isp nswmExtendedUserPrefs: meAutoSign=true nswmExtendedUserPrefs: meSignature=Regards, Joy... (3 Replies)
Discussion started by: bulkbiz
3 Replies

10. UNIX for Dummies Questions & Answers

Grep on multiple parameters

All, Is is possible to grep on two parameters in a file: ex: If data in a file(GROCERIES.TXT) were like this ABC GROCERIES 38-904949 16 987.89 Then grep would be grep '38-904040' '16' GROCERIES.TXT Thanks KP (5 Replies)
Discussion started by: kingofprussia
5 Replies
Login or Register to Ask a Question