grep Question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep Question
# 1  
Old 03-04-2005
grep Question

I have a file which loads procedure/function into ORACLE.Top of the file contains 3 rows which is FILENAME, FILETYPE AND COMMENTS and then the function/procedure etc.

FILENAME:
FILETYPE:
COMMENTS:
<Function/Procedure etc>

The Filename,FILETYPE, COMMENTS will be entered by the dba which will highlight what type of script and why it was used etc.

As part of my loading, I use multiple GREPS to check for FILENAME, FILETYPE, COMMENTS and then load them into logfiles for future viewing. Is there any better way to do this , because If I have 10 lines like this in the future then i may have to write 10 grep commands.

The command I use
echo "Name Of Object - `grep FILENAME $SQLFILE |cut -f2 -d:`" >> $LOGFILE/objects.lst
echo "Object Type - `grep FILETYPE $SQLFILE |cut -f2 -d:`" >> $LOGFILE/objects.lst
... and so on.

Thanks.
# 2  
Old 03-04-2005
Quote:
Originally Posted by yerra
I have a file which loads procedure/function into ORACLE.Top of the file contains 3 rows which is FILENAME, FILETYPE AND COMMENTS and then the function/procedure etc.

FILENAME:
FILETYPE:
COMMENTS:
<Function/Procedure etc>

The Filename,FILETYPE, COMMENTS will be entered by the dba which will highlight what type of script and why it was used etc.

As part of my loading, I use multiple GREPS to check for FILENAME, FILETYPE, COMMENTS and then load them into logfiles for future viewing. Is there any better way to do this , because If I have 10 lines like this in the future then i may have to write 10 grep commands.

The command I use
echo "Name Of Object - `grep FILENAME $SQLFILE |cut -f2 -d:`" >> $LOGFILE/objects.lst
echo "Object Type - `grep FILETYPE $SQLFILE |cut -f2 -d:`" >> $LOGFILE/objects.lst
... and so on.

Thanks.
grep [-f pattern_file] $SQLFILE
reads one or more patterns from patternfile. Patterns in patternfile are separated by newlines.so, write all the patterns you are searching for in a file and use the above cmd.Is this what you are looking for ?
# 3  
Old 03-04-2005
Quote:
Originally Posted by yerra
I have a file which loads procedure/function into ORACLE.Top of the file contains 3 rows which is FILENAME, FILETYPE AND COMMENTS and then the function/procedure etc.

FILENAME:
FILETYPE:
COMMENTS:
<Function/Procedure etc>

The Filename,FILETYPE, COMMENTS will be entered by the dba which will highlight what type of script and why it was used etc.

As part of my loading, I use multiple GREPS to check for FILENAME, FILETYPE, COMMENTS and then load them into logfiles for future viewing. Is there any better way to do this , because If I have 10 lines like this in the future then i may have to write 10 grep commands.

The command I use
echo "Name Of Object - `grep FILENAME $SQLFILE |cut -f2 -d:`" >> $LOGFILE/objects.lst
echo "Object Type - `grep FILETYPE $SQLFILE |cut -f2 -d:`" >> $LOGFILE/objects.lst
... and so on.

Thanks.
here's one alternative using awk:

yerra.awk:
Code:
BEGIN{
  tags="FILENAME:|FILETYPE:|COMMENTS:"
  values="Name Of Object|Object Type|Object Comments"

  n=split(tags, arr, "|");
  n=split(values, tmp, "|");
  for(i=1; i <=n; i++) {
     arr[arr[i]]=tmp[i]
     delete arr[i]
  }
}

$1 in arr { 
   match($0,$1); rest=substr($0, RSTART+RLENGTH+1)
   printf("%s - %s\n", arr[$1], rest)
}

nawk -f yerra.awk $SQLFILE >> $LOGFILE/objects.lst

You'll have to update the script "array" if you have other "tags".
Or you can have a config file for the script that can be read in by the script - that will require some modification.

or you can do something like this - not exactly what you have, but...:
Code:
_pat='(FILENAME|FILETYPE|COMMENTS)'
egrep "${_pat}" $SQLFILE | cut -f2 -d : >> $LOGFILE/objects.lst

Not sure if that's what you've been looking for....
# 4  
Old 03-05-2005
The nawk and using the file to check the pattern is good . Can these both be incorporated in the same script rather than 2 scripts one the main script and other script for comparing the patterns ?
# 5  
Old 03-05-2005
Quote:
Originally Posted by yerra
The nawk and using the file to check the pattern is good . Can these both be incorporated in the same script rather than 2 scripts one the main script and other script for comparing the patterns ?
not sure what you mean.
the hypothetical call would be:

nawk -f yerra.awk fileWithPatterns2check.txt $SQLFILE >> $LOGFILE/objects.lst
# 6  
Old 03-05-2005
I mean I have to one more script named yerra.awk which I use in my main script to check the pattern which loads the SQLs into the databases.
Can I the code of yerra.awk in this main program, so that I dont have to maintain 2 scripts for it. Since I would implement this in my Production env.

If I need to maintain yerra.awk, a generalized script would be helpful which check the pattern in any file till the newline is reached and generates the o/p.
# 7  
Old 03-05-2005
Quote:
Originally Posted by yerra
I mean I have to one more script named yerra.awk which I use in my main script to check the pattern which loads the SQLs into the databases.
Can I the code of yerra.awk in this main program, so that I dont have to maintain 2 scripts for it. Since I would implement this in my Production env.

If I need to maintain yerra.awk, a generalized script would be helpful which check the pattern in any file till the newline is reached and generates the o/p.
oh, I see - sure.
It would be something like this in your main shell script:
Code:
.................
.................
nawk '
# here goes the body of the previously posted awk script
' $SQLFILE >> $LOGFILE/objects.lst

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep Question

My grep returns a row of data like this: 75=20130130;60=074338;61=985;511=55473883;452=115439;62=196;267=1; Is there a way for the grep to only return 60="something" and 511="something" ? Thanks in advance. (10 Replies)
Discussion started by: Carl2013
10 Replies

2. UNIX for Dummies Questions & Answers

Question on grep

Hello all, I'm trying to grep the string "scott" from all files whose names are like srvr*.log and that were created "Nov 15"...I'm trying the following command but throws an error message...seems like the syntax is incorrect.. grep scott < ls -l srvr*.log|grep "Nov 15" Thanks for your... (9 Replies)
Discussion started by: luft
9 Replies

3. Shell Programming and Scripting

Question about grep

is there anyway i can ask grep to only get the first line? as in the top command line line 1 <-- just grep this line line 2 line 3 ---------- Post updated at 04:24 PM ---------- Previous update was at 04:19 PM ---------- nvm.. found out that i can do it with |head (12 Replies)
Discussion started by: Nick1097
12 Replies

4. Shell Programming and Scripting

Question about grep

can anyone tell me what the \/$ means? from grep \/$ (8 Replies)
Discussion started by: Nick1097
8 Replies

5. Shell Programming and Scripting

grep question please

i have files with "DOMAINSOLVER ACMS" with any number of spaces in between the two words on its own line and i can find it with the following: grep -c "DOMAINSOLVER* ACMS" $FILENAMEbut i need to exclude any lines matching: "$DOMAINSOLVER". i've tried a variety of quoting and escaping with no luck.... (4 Replies)
Discussion started by: crimso
4 Replies

6. Shell Programming and Scripting

grep question

Hello, Is there a way in grep to remember patterns? For eg: int a,b,c,d,a; If a variable is declared twice, like in the previous example, I should be able to print only those lines. Is there a way to print only the lines where the variable name occurs more than once, using grep... (1 Reply)
Discussion started by: prasanna1157
1 Replies

7. UNIX for Dummies Questions & Answers

grep question

Instead of using the following command #dmesg | grep -v sendmail | grep -v xntpd How can I use just one grep -v and give both arguments. Please suggest thanks (4 Replies)
Discussion started by: Tirmazi
4 Replies

8. Shell Programming and Scripting

grep question

hello people, All my servers have 4 mounts with this norme. For example, if my hostname is siroe. df -h | grep `hostname` /dev/dsk/c1t3d0s6 404G 399G 800M 100% /siroe3 /dev/dsk/c1t2d0s6 404G 399G 800M 100% /siroe2 /dev/md/dsk/d6 20G 812M 19G ... (3 Replies)
Discussion started by: melanie_pfefer
3 Replies

9. UNIX for Dummies Questions & Answers

Grep Question

Hello Everybody, I have files; yyyymmdd.log which the data look like this; "Txid=9426043&MsgTxt=Thankyou&UserId=john&Password=jh2501" "Txid=9426150&MsgTxt=Thankyou&UserId=john&Password=jh2501" . . . "Txid=9426200&MsgTxt=Thankyou&UserId=john&Password=jh2501" Question 1: How to... (3 Replies)
Discussion started by: nazri76
3 Replies

10. UNIX for Dummies Questions & Answers

grep question

what is the format for grep if I want to search from the current directory and through all its subdirectories?:) (3 Replies)
Discussion started by: pkappaz
3 Replies
Login or Register to Ask a Question