grep output of a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep output of a text file
# 1  
Old 11-19-2010
grep output of a text file

Hi. I have a unix script 'if' condition that greps through a file and searches for the strings ERROR and WARNING.

Code:
if egrep -q 'ERROR|WARNING' myfile.txt; then

When this 'if' condition is true then I want to be able to capture 10 lines before the string and 10 lines after the string. So a total of only 20 lines.

Can anyone help me with a way to grep through a files and then output only 20 lines of the file (10 before the string - 10 after) that surround the string when it's found?

Any help would be greatly appreciated!
# 2  
Old 11-19-2010
Code:
grep -A 10 -B 10 -E 'ERROR|WARNING' myfile.txt

?
# 3  
Old 11-19-2010
Quote:
Originally Posted by ctsgnb
Code:
grep -A 10 -B 10 -E 'ERROR|WARNING' myfile.txt

?
Hmm, just returning:

Code:
grep -A 10 -B 10 -E 'ERROR' myfile.txt
grep: Not a recognized flag: A

# 4  
Old 11-19-2010
Getting lines after it is doable since grep has a special option to help with that, -m. That will cause it to stop reading after the number of matches you tell it.

Code:
#!/bin/sh

# open the file
exec 5<filename

# Read up to the first match.
# because of -m, grep will leave the open file on the line after it.
# Then print 10 more lines.
( grep -m 1 "MATCH" && head -n 10 ) <&5

# close the file
exec 5<&-

Getting the lines before it is much harder, since a shell can't seek backwards.

---------- Post updated at 11:18 AM ---------- Previous update was at 11:17 AM ----------

Quote:
Originally Posted by buechler66
Hmm, just returning:

Code:
grep -A 10 -B 10 -E 'ERROR' myfile.txt
grep: Not a recognized flag: A

You never said what your system was. That's a GNU option and I guess you don't have GNU.
# 5  
Old 11-19-2010
Hmm...my system is AIX 5.3. Is this what you mean? Does this help?
# 6  
Old 11-19-2010
Yes, it means you don't have GNU grep, that's usually found in Linux, though occasionally installed elsewhere as a third-party app for its convenient options. Here's a brute-force non-GNU solution.

Code:
#!/bin/ksh

TMP="/tmp/$$"

while read LINE
do
        if [[ "${LINE}" == *ERROR* ]] || [ "${LINE}" == *WARNING* ]]
        then
                tail -n 10 "$TMP"
                echo "$LINE"
                head -n 10
                : > "$TMP"
        else
                echo "$LINE" >> "$TMP"
        fi
done < inputfile

rm -f "$TMP"

It's not perfect. If more than one error happens within the 10 lines it might not capture all the context.

---------- Post updated at 11:47 AM ---------- Previous update was at 11:32 AM ----------

Code:
#!/bin/ksh

TMP="/tmp/$$"

CONTEXT=0

while read LINE
do
        if [[ "${LINE}" == *ERROR* ]] || [ "${LINE}" == *WARNING* ]]
        then
		[[ "${CONTEXT}" -le 0 ]] && tail -n 10 "$TMP"
		CONTEXT=10
        fi

	if [[ "${CONTEXT}" -gt 0 ]]
	then
		echo "${LINE}"
	fi

        echo "${LINE}" >> "$TMP"

	((CONTEXT--))

done < inputfile

rm -f "$TMP"

This one should make better output, but is slower.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 11-19-2010
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

2. Shell Programming and Scripting

How to grep the desired output and output to a file?

currently I have process from a raw file to this stage ALTER TABLE "EXCEL_ADMIN"."TC_TXN_VOID" ADD CONSTRAINT "PK_TC_TXN_VOID" PRIMARY KEY ("TC_TXN_IID") ALTER TABLE "EXCEL_ADMIN"."TC_TXN_AMT" ADD CONSTRAINT "PK_TC_TXN_AMT" PRIMARY KEY ("TC_TXN_AMT_IID") ALTER TABLE... (10 Replies)
Discussion started by: jediwannabe
10 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

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

5. UNIX for Dummies Questions & Answers

Grab Portion of Output Text (sed, grep, awk?)

Alright, here's the deal. I'm running the following ruby script (output follows): >> /Users/name/bin/acweather.rb -z 54321 -o /Users/name/bin -c Clouds AND Sun 57/33 - Mostly sunny and cool I want to just grab the "57/33" portion, but that's it. I don't want any other portion of the line. I... (5 Replies)
Discussion started by: compulsiveguile
5 Replies

6. Shell Programming and Scripting

Dynamic output file generation using a input text file with predefined output format

Hi, I have two files , one file with data file with attributes that need to be sent to another file to generate a predefined format. Example: File.txt AP|{SSHA}VEEg42CNCghUnGhCVg== APVG3|{SSHA}XK|"password" AP3|{SSHA}XK|"This is test" .... etc --------- test.sh has... (1 Reply)
Discussion started by: hudson03051nh
1 Replies

7. Shell Programming and Scripting

grep reverses # output, but not text?

I have a working script to find out the IP of a hostname like this: #!/bin/bash h="www.google.com" echo $h j=`host $h | grep -o '\{1,3\}\.\{1,3\}\.\{1,3\}\.\{1,3\}'` echo "ip: $j" output ip: 74.125.19.99 the strange thing is if I use an IP instead of a hostname for $h, it inverts the... (2 Replies)
Discussion started by: unclecameron
2 Replies

8. Shell Programming and Scripting

Reading from a text file then /grep/output

I have a list of words that I want to grep in many files to see which ones have it and which ones dont. in the text file I have all the words listed line by line, ex: list.txt: check try this word1 word2 open space list .. I want to grep each line one by one. like I want it to... (7 Replies)
Discussion started by: s3rro
7 Replies

9. Shell Programming and Scripting

Ping text file of ip addressese and output to text file

I am basically a scripting noob, I have some programming logic, and I wouldn't post here if my 3 hours of searching actually found something. So far this is what I have: " #! /bin/ksh List=./pinglist1.txt cat $List | while read ip do Pingable="" ping $ip -n 2 | awk '/100%/ {print... (11 Replies)
Discussion started by: Lasthitlarry
11 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