LinuxQuestions.org
Register a domain and help support LQ
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
 
LinkBack Search this Thread
Old 02-25-2013, 10:56 AM   #1
marksa
LQ Newbie
 
Registered: Feb 2013
Posts: 5

Rep: Reputation: Disabled
Parse a log file with a beginning and end word pattern


Hi,

I would like to parse a log file to only have the following text (see below):

cluster: traf2 (S-traf2)

P-3 (sSu=EA-MMAS-SU-0)
P-4 (sSu=EA-MMAS-SU-1)

The output below can vary (the amount of P-'s can change)

I was thinking of using grep -A4 -w 'traf2'
but setting the line count to a number won't be a solution since it varies, it needs to terminate before the next "cluster" pattern appears.

I am guessing there must be a way to parse by looking for 'traf2' (looking for 'cluster' is too generic) and ending the file once 'cluster' is detected again through sed/awk, but I have no clue.

Any ideas ?

======================

Log file:

cluster: oa (S-oa)

S-2 (safSu=EA-MMAS-SU-1)
S-1 (safSu=EA-MMAS-SU-0)

cluster: traf2 (S-traf2)

P-3 (sSu=EA-MMAS-SU-0)
P-4 (sSu=EA-MMAS-SU-1)

cluster: traf (S-traf)

P-4 (safSu=EA-MMAS-SU-1) (safSu=EA-MMAS-SU-3) (safSu=EA-MMAS-SU-4)
P-3 (safSu=EA-MMAS-SU-0) (safSu=EA-MMAS-SU-2)
P-5 (safSu=EA-MMAS-SU-5) (safSu=EA-MMAS-SU-6)
 
Old 02-25-2013, 11:20 AM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,035

Rep: Reputation: 276Reputation: 276Reputation: 276
InFile ...
Code:
cluster: oa (S-oa)

S-2 (safSu=EA-MMAS-SU-1)
S-1 (safSu=EA-MMAS-SU-0)

cluster: traf2 (S-traf2)

P-3 (sSu=EA-MMAS-SU-0)
P-4 (sSu=EA-MMAS-SU-1)

cluster: traf (S-traf)

P-4 (safSu=EA-MMAS-SU-1) (safSu=EA-MMAS-SU-3) (safSu=EA-MMAS-SU-4)
P-3 (safSu=EA-MMAS-SU-0) (safSu=EA-MMAS-SU-2)
P-5 (safSu=EA-MMAS-SU-5) (safSu=EA-MMAS-SU-6)
This code ...
Code:
sed -n '/cluster: traf2 (S-traf2)/,/cluster:/p' $InFile |sed '$d' >$OutFile
... produces this OutFile...
Code:
cluster: traf2 (S-traf2)

P-3 (sSu=EA-MMAS-SU-0)
P-4 (sSu=EA-MMAS-SU-1)
Daniel B. Martin
 
Old 02-25-2013, 11:33 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,205

Rep: Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798
Code:
 awk '/traf2/ && $0 = RT $0' RS="cluster" file
 
Old 02-25-2013, 12:20 PM   #4
marksa
LQ Newbie
 
Registered: Feb 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks for the responses!
 
Old 02-25-2013, 09:46 PM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,141

Rep: Reputation: 329Reputation: 329Reputation: 329Reputation: 329
If you have gawk available, here's a gawk solution that could scale into a bash script:
Code:
$ x=traf;gawk '/cluster: '${x}' /,/cluster:/&&!/ '${x}' /{if ($0!~/cluster:/||$0~/'${x}'/)print}' log.txt
cluster: traf (S-traf)

P-4 (safSu=EA-MMAS-SU-1) (safSu=EA-MMAS-SU-3) (safSu=EA-MMAS-SU-4)
P-3 (safSu=EA-MMAS-SU-0) (safSu=EA-MMAS-SU-2)
P-5 (safSu=EA-MMAS-SU-5) (safSu=EA-MMAS-SU-6)
The bash might look something like this:
Code:
#!/bin/bash
# Arguments: $1 - key
#            $2 - input file (stdin if not specified)
show_range()
{
  local x
  x="[[:space:]]${1}[[:space:]]"
  gawk '/cluster:'${x}'/,/cluster:/&&!/'${x}'/{if ($0!~/cluster:/||$0~/'${x}'/)print}' "${2:-"/dev/stdin"}"
}
show_range "${1}" "${2}"
Warning: That script has not been tested.

P.S.: If you're satisfied with the solution(s), expand the thread tools pull-down menu (at the head of the thread, above) and mark it as "Solved."
 
Old 02-25-2013, 10:05 PM   #6
marksa
LQ Newbie
 
Registered: Feb 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Setting to Solved,
thanks PTrenholme
 
  


Reply


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] add text at the beginning and at the end of lines in a file smithy2010 Linux - Newbie 6 07-22-2012 04:07 AM
How do you write a magic file test pattern to match the end of a file? arj Linux - Software 3 02-10-2011 05:38 PM
Clean Up Log - Search for Pattern in Log file and Output result bridrod Linux - Newbie 10 01-05-2010 09:49 AM
matching a pattern in a file & prefixing a word shivarajM Linux - Software 3 05-28-2009 01:14 PM
c++ header file at end or beginning? kpachopoulos Programming 1 05-25-2009 05:36 AM


All times are GMT -5. The time now is 06:55 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration