grep a line from a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep a line from a text file
# 1  
Old 09-14-2010
grep a line from a text file

Hi all,

I need to grep a line from a log file which ensures me that the application server script is executed successfully. Some body please help me on this.

I also need to write a while loop in which i need to use the status of the above grep output. Could some one please tell me how to use that.

Thanks in advance.
Firestarr
# 2  
Old 09-14-2010
Code:
grep -q "search_str" log_file

Gives the status of the command
# 3  
Old 09-14-2010
Can i use
Code:
var=`grep -q "search_str" log_file`
echo $var

but its returning nthg...

Last edited by Yogesh Sawant; 09-15-2010 at 09:16 AM.. Reason: added code tags
# 4  
Old 09-14-2010
Code:
grep -q "search_str" log_file
var=$?
echo $var

# 5  
Old 09-15-2010
@anbu23

thank you for the reply

---------- Post updated at 10:30 AM ---------- Previous update was at 10:21 AM ----------

Can i use this way...
Code:
grep -q "search_str" log_file
while [ $? ]
do 
echo wroking
done

will this work??

Last edited by Scott; 09-15-2010 at 05:51 PM.. Reason: Code tags please...
# 6  
Old 09-15-2010
If the grep command finds the string, the exit status of command is successful. Then While loop is entered. For the next run of while loop $? comes from the last command you placed in the while loop.

If you can explain your requirement we can help you in better way.
# 7  
Old 09-15-2010
Code:
grep -i "search_string" log_filename
status=$?
if [ $status -eq 0 ]
then
  echo "grep successful"
 else
  echo "unsucessful"
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to grep and print portions of a very loooong line of text.?

Hi, A bit stumped here trying to grep and wanting to display portion of a text Using the grep below, I am able to find the line containing the string select ~]$ ~]$ srvctl -h | grep -i "select" Usage: srvctl add service -db <db_unique_name> -service <service_name> {-preferred... (1 Reply)
Discussion started by: newbie_01
1 Replies

2. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

3. UNIX for Dummies Questions & Answers

How to grep multiple lines from a text file using another text file?

I would like to use grep to select multiple lines from a text file using a single-column text file. Basically I want to only select lines from the first text file where the second column of the first text file matches the second text file. How do I go about doing that? Thanks! (5 Replies)
Discussion started by: evelibertine
5 Replies

4. Shell Programming and Scripting

Grep to isolate a text file line and Awk to select a word?

I am looking at using grep to locate the line in the text file and them use awk to select a word or words out of it. I know awk needs -v to allow a variable to be used, but also needs -F to allow the break up of the sentence and allow the location of separate variables. $line = grep "1:" File |... (8 Replies)
Discussion started by: Ironguru
8 Replies

5. Shell Programming and Scripting

search text file in file if this file contains necessary text (awk,grep)

Hello friends! Help me pls to write correct awk and grep statements for my task: I have got files with name filename.txt It has such structure: Start of file FROM: address@domen.com (12...890) abc DATE: 11/23/2009 on Std SUBJECT: any subject End of file So, I must check, if this file... (4 Replies)
Discussion started by: candyme
4 Replies

6. UNIX for Dummies Questions & Answers

Using grep to move files that contain a line of text

I have a folder with about 4000 files in it. I need to extract the files that contain a certain line of text to another directory. for example files with 'ns1.biz.rr.com' need to be extracted to the directory above or some other directory. I tried using the following but was not successful. (6 Replies)
Discussion started by: spartan22
6 Replies

7. Shell Programming and Scripting

Search text from a file and print text and one previous line too

Hi, Please let me know how to find text and print text and its previous line. Please don't get irritated few days back I asked text and next line. I am using HP-UX 11.11 Thanks for your help. (6 Replies)
Discussion started by: kamranjalal
6 Replies

8. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies

9. Shell Programming and Scripting

Appending Text To Each Line That Matches Grep

I'm currently digging for a way to append a line to a text file where each line begins with the word "setmqaut". This is a continuation of my IBM MQSeries backup script I'm working on to make my life a little easier. What I would like to do is have each line that looks like this: setmqaut -m... (4 Replies)
Discussion started by: sysera
4 Replies

10. 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
Login or Register to Ask a Question