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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH: Grepping/sedding/etc out part of a file... (from one word to 'blank' line)
# 1  
Old 12-11-2008
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
# 2  
Old 12-11-2008
can you post your code?? so that we can suggest some change in that code only..
# 3  
Old 12-11-2008
Question Can you show a larger sample of the file?

I have some thoughts by using sed, tr, awk, etc...
But, cannot put to paper without seeing more of the input data and also what you are trying to get as output.
# 4  
Old 12-11-2008
more of the input would look something like:
Code:
Time
 12:34 PM

Alerts
 Password Incorrect
 Login Error
 Another Error
 Another Error 2
 
Other Info
 Memory Status 12
 CPU Status 12.44

Bandwidth
 SATA 57%
 ATA 12%

Processes
 Process  Memory CPU
 AAA      12.4GB 34%
 BBB      8.4GB  17%

# 5  
Old 12-11-2008
Hammer & Screwdriver What about the following

Code:
> sed "s/^[A-Z]/~&/" file110 | tr "\n" "|" | tr "~" "\n" | egrep "^Alerts|^Other Info" | tr "|" "\n"
Alerts
 Password Incorrect
 Login Error
 Another Error
 Another Error 2
 

Other Info
 Memory Status 12
 CPU Status 12.44

file110 is simply the log file I copied your sample to
the first sed marks the major headings
then convert new-lines to | (as delimiter)
force new-lines before major headings
grep for either of two major headings
convert the | back to new-lines
# 6  
Old 12-11-2008
Here's the code I currently have

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

# 7  
Old 12-11-2008
Hammer & Screwdriver OK, building off your approach

Code:
> if [[ `awk "/^Alerts/,/^Other Info/" file110 | egrep "Password Incorrect|Login Error"` ]] ; then echo "Do this"; fi
Do this
>

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In a file, replace blank line by the last line not blank above

Dear All, In a CSV file, say that a given column has been extracted. In that column, information is missing (i.e. blank lines appear). I would like to replace the blank lines by the last valid line (not blank) previously read. For example, consider the extract below: 123 234 543 111... (7 Replies)
Discussion started by: bagvian
7 Replies

2. Shell Programming and Scripting

Bash - sed - Remove first word from line which can begin eventually with blank

hello. How to remove first word from line. The line may or may not start with blank. NEW_PARAM1=$(magic-command " -t --protocol=TCP -P 12345-u root -h localhost ") NEW_PARAM2=$(magic-command "-t --protocol=TCP -P 12345 -u root -h localhost ") I want NEW_PARAM1 equal to NEW_PARAM2 equal ... (2 Replies)
Discussion started by: jcdole
2 Replies

3. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

4. Shell Programming and Scripting

Grepping a word from a .xml file dynamically

Hi I have the xml file as <Head="Test" Id="3" > <Title="mode" > I have used the code to grep the words "Test" and "mode" as Head=`cat file.xml | grep "Head" | awk -F "=" '{print $2}' | awk -F " " '{print $1}'` Tilte=`cat file.xml | grep "Title" | awk -F "=" '{print... (3 Replies)
Discussion started by: Priya Amaresh
3 Replies

5. Shell Programming and Scripting

Grepping a word from a .xml file

Hi I have a xml file vi lpower.xml <head = power_health> Now, I need to grep "power_health" alone from that file using shell.. Please help (3 Replies)
Discussion started by: Priya Amaresh
3 Replies

6. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

7. Shell Programming and Scripting

bash keep a word from a line

hi guys in my bash script I have a file which has lines that look the following way: dog cat "bird" red/ (blue/pink) OR dog cat "bird" red/ (blue/pink) no matter what are the words of the line or how many they are I only want to keep from each line--that can look like either the... (1 Reply)
Discussion started by: vlm
1 Replies

8. Shell Programming and Scripting

extracting part of a line excluding particular word from it

here is the line on which i want to process `empNo` int(13) NOT NULL AUTO_INCREMENT, it sometimes doesnt have comma at the end too `empNo` int(13) NOT NULL AUTO_INCREMENT i want to extract all except "AUTO_INCREMENT" not only this line i ,want the code to work on any line if it has... (5 Replies)
Discussion started by: vivek d r
5 Replies

9. Shell Programming and Scripting

[Bash]Attempting to Merge text from one file into another file at the line directly under a word

Hello, This is my first post on the forums. So I want to start by thanking anyone who is kind enough to read this post and offer advise. I hope to be an active contributor now that I've found these forums. I have an issue that I figure would be a good first post.. I have 2 text files... (5 Replies)
Discussion started by: efciem
5 Replies

10. Shell Programming and Scripting

Returning only part of a line when grepping

I want to grep out a part of a snort rule based on the SID given, but all i want as the output is the part in the quotes after the msg: An example line looks something like this: alert tcp any any -> 127.0.0.1 any (msg:"Example Message"; classtype:Example; sid:123456;) I would want it to... (7 Replies)
Discussion started by: riott
7 Replies
Login or Register to Ask a Question