search for lines in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search for lines in a file
# 1  
Old 04-21-2008
search for lines in a file

Hello

I need to check if following three files exist in a file, how to do that in shell script:
1. ALL MACHING RECORD COLUMNS MATCHED (Baseline and Regression File)
2. Total Mismatched Records (Baseline File): 0
3. Total Mismatched Records (Regression File): 0

Currently I am seaching only for one line "ALL MACHING RECORD COLUMNS MATCHED (Baseline and Regression File)" and doing it like this:

Code:
 if [  -s $regfiles_name ]; then
                grep "ALL MACHING RECORD COLUMNS MATCHED (Baseline and Regression File)" $regfiles_name
                retval=$?
                if [ $retval != 0 ]; then
                   echo $regfiles " - There is a mismatch" >> reg_email_body
                else
                   echo $regfiles " - Matched" >> reg_email_body
                fi
fi

How can I tweak it to check if all 3 lines exist?

Thanks!!

Last edited by Yogesh Sawant; 04-22-2008 at 02:24 AM.. Reason: added code tags
# 2  
Old 04-21-2008
Just want to make my question more clear. I want to check if all 3 lines exist or not, meaning an AND condition.

Thanks!!
# 3  
Old 04-21-2008
Quote:
Originally Posted by shalua
Just want to make my question more clear. I want to check if all 3 lines exist or not, meaning an AND condition.

Thanks!!
Solution:
Check with grep -e "searchText1" -e "SearchText2" <FILENAME>

This makes the multiple search possible.

With AND caluse you can use sed option to search.
As :
sed '/pattern/action' filename

for multiple searchs use..
sed -e 'pattern1/p' -e '/pattern2/p' -e '/pattern3/p' FILE

Hope this'll work for you !!
Thanks.Smilie
# 4  
Old 04-21-2008
Quote:
Originally Posted by shalua
Just want to make my question more clear. I want to check if all 3 lines exist or not, meaning an AND condition.

Thanks!!
Give this a try:

Code:
#!/bin/sh
#set -x

filename="your_file"

lineA="ALL MATCHING RECORD COLUMNS MATCHED (Baseline and Regression File)"
lineB="Total Mismatched Records (Baseline File): 0"
lineC="Total Mismatched Records (Regression File): 0"


if grep "$lineA" "$filename" && grep "$lineB" "$filename" && grep "$lineC" "$filename"
then
echo "Match"  >> reg_email_body
else
echo  "There is a mismatch"  >> reg_email_body
fi

# 5  
Old 04-22-2008
neither worked!!

In both cases it hangs, looks like some syntax issue. any further suggestions!!
# 6  
Old 04-22-2008
also my script is a ksh script.
# 7  
Old 04-22-2008
sed -n -e '/BBB/p' -e '/AAA/p' emp.dat | sed -n '$='

works for me on the command line and gives me the count of lines matching either of these two patterns. But when I put it in the shell script, it returns errors.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search lines between two timestamps in a file

Hi, I want to pull lines between two timestamps from a file. When I just insert values directly it is working fine. sed -n '/2014-02-14 05:30/,/2014-02-14 05:31/p' Logfile When I try to insert variable it is not working. sed -n '/TZ=GMT+1 date +%Y-%m-%d" "%H:%M:%S/,/TZ=GMT date... (2 Replies)
Discussion started by: Neethu
2 Replies

2. Shell Programming and Scripting

How to Search string(which is in 2 lines) in a file?

Hello, I want to search 2 lines from the file. I wanted to display all such matches. Example file: ================== Testfile is test TEST1 TEST2 testing the file string to do testing TEST1 TEST2 sample strings ================= I wanted to search the file with 2 lines " TEST1... (3 Replies)
Discussion started by: balareddy
3 Replies

3. Shell Programming and Scripting

To search lines between two timestamps in a file

I have a log file where every line starts with a time stamp. I have to extract lines from the file within a given time stamp. For example: IF the file is like this: 2012-08-19 10:34:03,446|WebContainer : 56|OrderHeaderDaoImpl|findByKeys|26| 2012-08-20 11:34:03,463|WebContainer :... (8 Replies)
Discussion started by: abhinalluri
8 Replies

4. Shell Programming and Scripting

search and delete the lines in a file

HI group members I am new in unix I want to search # symbol in a file. if found need to delete the entire row in the file. need to move the actual data(with out # symbol data) to another file. Thanks (2 Replies)
Discussion started by: pmreddy
2 Replies

5. Shell Programming and Scripting

reading lines from a file between two search patterns

Hi, I am new to shell scripting and is working on a script to extract lines from a log file between two time stamps using awk command. After some research I used following command: awk '/01 Oct 2011/{p=1} /10 Oct 2011/{p=0} p' test.log >> tmp.log This works fine. But now i want to... (3 Replies)
Discussion started by: davidtd
3 Replies

6. Shell Programming and Scripting

awk how to search strings within a file from two different lines

Hi, i would really appreciate any help anyone can give with the following info. Thanks in advance. I need to run a search on a file that contains thousands of trades, each trade is added into the file in blocks of 25 lines. i know the search has to take place between a time stamp specified... (4 Replies)
Discussion started by: sp3arsy
4 Replies

7. UNIX for Dummies Questions & Answers

Search for lines in one file in another

I am trying to search for lines present in file a that do not exist in file b and print out the lines. E.g. file a apple pear banana orange file b apple banana orange would output to stdout: pear. ... (3 Replies)
Discussion started by: thurmc
3 Replies

8. Shell Programming and Scripting

search for an expression in a file and print the 3 lines above it

Hi, I have a file like this comment.txt 1.img 2.img 3.img OK x.img y.img z.img not ok 1.img 2.img 3.img bad 1.img 2.img 3.img (7 Replies)
Discussion started by: avatar_007
7 Replies

9. Shell Programming and Scripting

Search and Remove Lines within File

Hello, I've searched through the scripting section but could not find what I need. I need to search for empty sections within a file and remove them. Here is an example file: Title 123 A B C D E Title 098 Title 567 Z Y (4 Replies)
Discussion started by: leepet01
4 Replies

10. Shell Programming and Scripting

Search for multiple lines in large file

Hi, I have a requirement to search for a string in a large log file along with few lines before and after the the string. The following script was sufficient to search such an entry. STRING_TO_GREP="$1" FILE_TO_GREP="$2" NUMBER_OF_LINES_BEFORE=$3 NUMBER_OF_LINES_AFTER=$4 for i in `grep... (3 Replies)
Discussion started by: praveen123
3 Replies
Login or Register to Ask a Question