Visit the LQ Articles and Editorials section
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-11-2008, 12:04 PM   #1
LQ Newbie
Registered: Oct 2007
Posts: 23

Rep: Reputation: 15
BASH: Grepping/sedding/etc out part of a file... (from one word to 'blank' line)

I have a file that lists data about a system. It has a part that can look like:

the errors I'm looking for with other errors:
 Password Incorrect
 Login Error
 Another Error
 Another Error 2
Other Info
or, just the errors I need to parse for:
 Password Incorrect
 Login Error
Other Info
or, if no alerts:
Other Info
What I need to do is parse the file, and branch iff the error "Password Incorrect" or "Login Error" comes up. If those come up with other errors that is fine. The file doesn't always include errors (actually this is usually the case). Also, what makes this difficult for me is the line after the last alert isn't blank -- it actually contains a single space.

I have code for it, but it's getting messier and messier, and I'm sure you guys help me out with something WAY more elegant.

Old 12-11-2008, 12:14 PM   #2
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 13,253

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Originally Posted by elinenbe View Post
I have code for it, but it's getting messier and messier
My natural reaction if I get to this point is to reach for perl.
Old 12-11-2008, 01:17 PM   #3
LQ Newbie
Registered: Oct 2007
Posts: 23

Original Poster
Rep: Reputation: 15
here's what I have

So, this is what I've got so far...

awk '/^Alerts/,/^Other Info/' myfile >myfile1
sed 'N;$!P;$!D;$d' myfile1 > myfile2
sed '1d' myfile2 > myfile3
if [[ `cat myfile3 | grep -E 'Password Incorrect|Login Error'` ]]


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
grab the line below a blank line and the line above the next blank line awk or perl? Pantomime Linux - General 7 06-26-2008 08:13 AM
script to check the last line in a file is blank line or not naveensankineni Programming 10 03-01-2008 11:13 PM
putting word in front of each line in a file stryka Slackware 3 12-06-2005 02:32 PM
add word to each line of the file? ziggie216 Linux - General 4 04-25-2005 04:19 AM
grepping last word of output rajatgarg Programming 3 11-25-2003 10:41 AM

All times are GMT -5. The time now is 02:13 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration