Grep within a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep within a script
# 8  
Old 04-30-2008
Quote:
Originally Posted by chappidi_pradee
hope this works for you

please add this in a script and change the permissions to chmod 755 for that file to make it executable.



#!/bin/sh

echo "enter the first value : \c";
read first_value

echo "enter the second value : \c";
read second_value

grep "\[$fist_value \]" REUTERS_200804*.log | grep "\[$second_value\]";

exit 0;
That is very cool. Thanks! Now how could i debug it to make sure that it is working. For example, i have 2 files in my home directory which is the same place that i am running the script from. For the first value, i put in 2do (i have a file with that name) and also the name of the script for the second value (grepscript.sh). How long should it take it to find those values...in your best guess. I am running a vmware machine on Solaris 10

thank you
# 9  
Old 05-01-2008
The script allows you to enter the strings, but not the file to grep in. That was not in the initial requirement.

Also, you would need to fix the spelling of "fist_value" (^:

Passing the strings to grep for on the command line is a little cumbersome, but allows you to improve your searches iteratively if your shell has a history mechanism. Scripts which prompt you are easy for beginners, but you quickly grow out of using them.

If you want to avoid having to type the backslashed square brackets, you can internalize those into the script:

Code:
#!/bin/sh

case $# in 1|2) echo "Syntax: $0 string1 string2 files ..." >&2; exit 1;; esac

first=$1
shift
second=$2
shift

grep "\\[$1\\]" "$@" | grep "\\[$2\\]"

The search time will depend on the number of items in each file (and on the complexity of the search strings, but you are just searching for literal strings which are relatively long, so nothing very complex there). You must know better than any of us how long these things typically take. But chappidi_pradee's script expected the search strings, not file names, as input.

On another note, if you think the backslashes are cumbersome, fgrep will only search for literal strings, so doesn't need backslashes (and indeed, if you put in backslashes, or square brackets, it will search for those characters literally).
# 10  
Old 05-01-2008
Thanks, much appreciated - it gives me a good start and a little more understanding.
# 11  
Old 05-01-2008
Is there anyway I can do the same for the date - ie user input?

I tried a similar format to the scripts already given but it doesnt work
# 12  
Old 05-11-2008
The one I pasted above allows you to specify two search strings, and an arbitrary number of input files. It should not be too hard to extend to fill in part of the file name automagically if you want to specify just the date and have the script know to "pad" it with the right directory name and extension etc.
# 13  
Old 05-16-2008
Thanks Era - I have managed to implement the user date input.
# 14  
Old 05-16-2008
Ok, I can now run this script and choose which parts I want to grep and the date I want to check - I have another question but not sure this can be done.

When I use grep to grep out tag 39 and its value, is there anyway I can get the script to display [11 ] [055152-A-09MEI08] as this is a ref number



e.g.

01:59:36 -- sending request [type=8|desc=Execution Report]
| [35 ] [8]
| [57 ] [***]
| [115 ] [***]
| [128 ] [***]
| [50 ] [***]
| [56 ] [***]
| [49 ] [***]
| [34 ] [3]
| [52 ] [20080515-05:59:36]
| [15 ] [EUR]
| [22 ] [4]
| [48 ] [FR0000120404]
| [55 ] [FR0000120404]
| [38 ] [150]
| [32 ] [0]
| [75 ] [20080515]
| [54 ] [1]
| [59 ] [6]
| [44 ] [50]
| [31 ] [0]
| [60 ] [20080515-00:45:55]
| [126 ] [20080630-23:00:00]
| [39 ] [4]
| [20 ] [0]
| [37 ] [***]
| [14 ] [0]
| [6 ] [0]
| [17 ] [12108311550651]
| [11 ] [055152-A-09MEI08]
| [8 ] [FIX.4.0]
| [9 ] [0287]
| [10 ] [059]
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep within script

Hi, I found script which compare ciphers with openssl and return back all result in "YES" (for matching) and "NO" (for no match) I want to result only "YES" part which can be achieved using grep but not sure how and where to place in below script" Script:... (1 Reply)
Discussion started by: khuharshree
1 Replies

2. Shell Programming and Scripting

Grep Script

Hi, New to scripting and looking for some help. I am trying to write a script that will search a specified directory for any new or modified files within the last 7 days and display the results. This will be ran daily and emailed? Thanks (18 Replies)
Discussion started by: Con592
18 Replies

3. Shell Programming and Scripting

Help with script - GREP

Hallo gentlemen, i've a problem removing lines from txt file. To make it simple, here is an example: TEXT1.TXT --- contents: 9.9.9.9 geek.net 1.1.1.1 geek.com 2.2.2.2 leet.net TEXT2.TXT --- contents: geek.com coolbar.org I simply do: cat text1.txt | grep -f text2.txt >... (7 Replies)
Discussion started by: mirkocosta
7 Replies

4. Shell Programming and Scripting

Inconsistent `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`

i have this line of code that looks for the same file if it is currently running and returns the count. `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l` basically it is assigned to a variable ISRUNNING=`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh |... (6 Replies)
Discussion started by: wtolentino
6 Replies

5. UNIX for Dummies Questions & Answers

grep IP script

Hello, I have a command and would like to make that as a script. How do I do that? my command is: grep -Eo +\.+\.+\.+. and i want to have that like grepscript.sh x.txt > IP.txt Can somebody help me? (3 Replies)
Discussion started by: eightball
3 Replies

6. Shell Programming and Scripting

script use min resource ( grep grep)

Hi i wrote script use it as watchdog ( i mean it check another program (pooya) whenever that was killed (closed or crashed) it run another script (pooya_start.sh) to start it, this script work fine and do the job for me , i need help of an expert to tell me (exact command) how to change this... (8 Replies)
Discussion started by: pooyair
8 Replies

7. Shell Programming and Scripting

How to grep sql error in shell script and exit the script?

I need help in the following script. I want to grep the sql errors insert into the error table and exit the shell script if there is any error, otherwise keep running the scripts. Here is my script #!/bin/csh -f source .orapass set user = $USER set pass = $PASS cd /opt/data/scripts echo... (2 Replies)
Discussion started by: allinshell99
2 Replies

8. UNIX for Dummies Questions & Answers

trying to grep the first few lines of a continuos script, and exit the script anyidea

Hi. I am trying to extract the output of the first few lines of a continuos sh script. The when i run it, i wont to grep the the first 20 lines of it for an entry and basically do a crtl z out of it or to that effect, and output the results to a text file. I basically want to script... (5 Replies)
Discussion started by: k00061804
5 Replies

9. UNIX for Dummies Questions & Answers

grep script

hi guys, i was hoping you could help me out with the following im designing a help site for my work colleagues and would like a little script which would do a grep to check if an application is running on unix for example this is what i do on unix ps -ef | grep mqm and this tells me if... (4 Replies)
Discussion started by: drchris
4 Replies

10. UNIX for Advanced & Expert Users

grep from a script...

This is an extract from a script that i am trying to run. for tim in "2005:00:" "2005:01:" "2005:02:" "2005:03:" "2005:04:" "2005:05:"; do FormString="$tim" echo "grep '$FormString' access.10Aug-1201AM" count=`grep "$FormString" access.10Aug-1201AM | wc -l` ... (1 Reply)
Discussion started by: hamsasal
1 Replies
Login or Register to Ask a Question