grep in a shell question


 
Thread Tools Search this Thread
Operating Systems Solaris grep in a shell question
# 1  
Old 11-04-2011
grep in a shell question

Hi Experts,

I am able to run the below command via command line without any issues:

cat test.txt | grep -A 2 "Title"

However, when I put this same command on test.sh and do a ./test.sh, I get the error "grep: illegal option -- A
Usage: grep -hblcnsviw pattern file . . ."

Would appreciate your help.
# 2  
Old 11-04-2011
In the command line, type the below command

Code:
 
which grep

use the output of grep in your shell script ( absolute path of grep )

And no need to use the cat command in your script

Code:
 
grep -A 2 "Title" test.txt

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 11-04-2011
Perfect!!!
# 4  
Old 11-04-2011
If grep not supports -A and -B options ..
Code:
$ nawk 'c-->0;/Title/{c=2}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Question about grep

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

3. Shell Programming and Scripting

New Unix user with shell script question using grep

Hello, I am a new Unix user and new to shell programming. I am working on a script to go through a log file and find the text error: grep -i 'error' monplus.mplog if I find the text error in the log file I would like to echo a message to the operator staing there is an error I am currently... (2 Replies)
Discussion started by: dtracy01
2 Replies

4. Shell Programming and Scripting

Question in grep

I have a requirement where I need to search for 26 consecutive 000000.000 in a file. I tried using grep but it is not working. The string I need to search is ... (2 Replies)
Discussion started by: gpaulose
2 Replies

5. Shell Programming and Scripting

question on grep..

i have a question on grep input file: FIRST NAME :ANNE LAST NAME :CRUZ <text> <text> <text> FIRST NAME :JOHN LAST NAME :ALBA <text> <text> <text> FIRST NAME :IRMA LAST NAME :JONES <text> <text> <text> (4 Replies)
Discussion started by: kingpeejay
4 Replies

6. 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

7. AIX

AIX 4.2 Korn shell and grep question

Ho do I find out the verion of the Kron shell on my client`s system ? There is no one to ask. They are not knowledged enough (hard to believe but yes). Also, on that AIX 4.2, I am trying to figure out how to do a grep using a search patter like below but does not seam to work. The '*' do... (11 Replies)
Discussion started by: Browser_ice
11 Replies

8. UNIX for Dummies Questions & Answers

Another grep question

Hi guys, I'm very new to unix but am liking it a lot so far, so please be gentle on this newb. I tried using the search for this question, but no luck. Anyways, i've got a few data fields... for example John Adress1 Adress2 Sam Adress3 Adress4 Jason Adress5 Adress6 Is... (4 Replies)
Discussion started by: Uss_Defiant
4 Replies

9. UNIX for Dummies Questions & Answers

Another grep question

Say I want to grep for a line with EXEC in it grep EXEC blah.conf thats cool but what if i want to grep for EXEC and if it finds a line with that in it to also grab the two lines above it? (6 Replies)
Discussion started by: kingdbag
6 Replies

10. UNIX for Dummies Questions & Answers

Grep question

sometimes I'll do a search with grep and it just goes down a line and hangs. What is it doing? (4 Replies)
Discussion started by: eloquent99
4 Replies
Login or Register to Ask a Question