LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 12-11-2008, 12:04 PM   #1
elinenbe
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:
Code:
Alerts
 Password Incorrect
 Login Error
 Another Error
 Another Error 2
 
Other Info
or, just the errors I need to parse for:
Code:
Alerts
 Password Incorrect
 Login Error
 
Other Info
or, if no alerts:
Code:
 
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.

Thanks,
Eric
 
Old 12-11-2008, 12:14 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Quote:
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
elinenbe
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...

Code:
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'` ]]
then
     DO CODE HERE
fi
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:23 AM.

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