sed script to parse logs issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed script to parse logs issue
# 1  
Old 11-12-2012
sed script to parse logs issue

I have this script to parse some logs:

Code:
#!/bin/bash
id=$1
shift
sed "/(id=$id)/,/^$/!d" "$@"

Usage: ./script.sh 1234 logfile

The logs have an empty line before the logged events/timestamps -- most of the time. And this is my issue, since when there is no empty line, it will catch things I don't want.

An example of the logs:

Code:
test@server 2012-11-12 10:00:00 (id=1234)
text
text
text

test@server 2012-11-12 10:01:00 (id=1234)
more text
more text
more text
test@server 2012-11-12 10:02:00 (id=4321)
even more text
even more text
even more text

Here my script would also return the events for id 4321, which I do not want.

Any great ides on how to solve this?
# 2  
Old 11-12-2012
If you don't mind some extra new-lines:
Code:
sed 's/^test@server/\
&/' file|sed "/(id=$id)/,/^$/!d"

This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 11-12-2012
if every tpeserver contains only 3 lines after it, then you can go for this
Code:
sed -n "/id=${id}/ {N;N;N;p}" input_file

# 4  
Old 11-12-2012
Quote:
Originally Posted by elixir_sinari
If you don't mind some extra new-lines:
Code:
sed 's/^test@server/\
&/' file|sed "/(id=$id)/,/^$/!d"

That solved my issue, thanks! Smilie

---------- Post updated at 03:02 PM ---------- Previous update was at 03:01 PM ----------

Quote:
Originally Posted by msabhi
if every tpeserver contains only 3 lines after it, then you can go for this
Code:
sed -n "/id=${id}/ {N;N;N;p}" input_file

No they don't, but thanks for the input. Smilie
# 5  
Old 11-12-2012
awk version:
Code:
awk '/id=/{print RS}1' "$@" | awk "/id=$id/" RS=

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If I ran perl script again,old logs should move with today date and new logs should generate.

Appreciate help for the below issue. Im using below code.....I dont want to attach the logs when I ran the perl twice...I just want to take backup with today date and generate new logs...What I need to do for the below scirpt.............. 1)if logs exist it should move the logs with extention... (1 Reply)
Discussion started by: Sanjeev G
1 Replies

2. Shell Programming and Scripting

Bash Script to parse Perforce Logs

Hi All, I need to write a bash script that will parse some perforce log files, the log files will contain user login information, the script would need to pare the log, and check who logs in, and if the user is a superadmin, then the script will check the ip address to see which server the... (4 Replies)
Discussion started by: BostonRob
4 Replies

3. Shell Programming and Scripting

sed command issue in script

Hi, I am using sed command to extract data from my log file for a certain time interval. From and To "time" are my input arguments. Now if i use the sed command on command line. I get the desired results and If i use it in script.It fails. sed command as command line: sed -n '/04-Mar-2015... (6 Replies)
Discussion started by: oberoi1403
6 Replies

4. Shell Programming and Scripting

Perl script to parse multiple windows event logs.

Hi all, I am developing a log parsing agent in perl to send windows Event logs to Zenoss Monitoring tool. Using Win32::EventLog i can able to get the Event messages but only one Eventype eg Application or System could able to parse at a time. Can you please help to how to open mutiple eventlogs... (3 Replies)
Discussion started by: kar_333
3 Replies

5. Shell Programming and Scripting

sed/awk script to parse list of bandwidth rules

Hello all gurus, I have a long list of rules as below: 20 name:abc addr:203.45.247.247/255.255.255.255 WDW-THRESH:12 BW-OUT:10000000bps BW-IN:15000000bps STATSDEVICE:test247 STATS:Enabled (4447794/0) <IN OUT> 25 name:xyz160 addr:203.45.233.160/255.255.255.224 STATSDEVICE:test160... (3 Replies)
Discussion started by: sb245
3 Replies

6. Shell Programming and Scripting

stuck on first script with sed issue

Hi, I'm developing my first bash script and have made good progress but stuck at this point. I've run sed on a text file to extract some data and have saved it into a variable e.g. $blah the variable contains emails as follows e.g. <a@b.com> <b@c.com> I'm now trying to edit... (2 Replies)
Discussion started by: newb1000
2 Replies

7. Shell Programming and Scripting

Facing issue while using xsltproc tp parse XML in bash

I have written a bash script which opens a folder, reads all the *.xml files in it, and pulls the required data that i need from XML tags. I am using xsltproc (my xsl name) (my xml folder location/*.xml) and running this in a for each loop The problem is that some XML files are having special... (3 Replies)
Discussion started by: shivashankar.g
3 Replies

8. Shell Programming and Scripting

Need AWk To parse XML logs

Hi , I need an Awk script to parse my log file . 2008-04-26 10:00:13,391 INFO Logger - <?xml version="1.0" encoding="UTF-8" standalone="no"?><2dm tmsg... (0 Replies)
Discussion started by: amit1_x
0 Replies

9. Shell Programming and Scripting

To parse through the file and print output using awk or sed script

suppose if u have a file like that Hen ABCCSGSGSGJJJJK 15 Cock ABCCSGGGSGIJJJL 15 * * * * * * : * * * . * * * : Hen CFCDFCSDFCDERTF 30 Cock CHCDFCSDHCDEGFI 30 * . * * * * * * * : * * :* : : . The output shud be where there is : and . It shud... (4 Replies)
Discussion started by: cdfd123
4 Replies

10. Shell Programming and Scripting

Issue with sed in script

I have a loop in a script that is given me an error but, when I do it on the command line it works perfectly. The sed statement has to use the variables from a file so the file is partitioned correctly. I am running on HP: <Begin error>: + cat /u01/bteam/CNAM/1121/.partition + read line + +... (3 Replies)
Discussion started by: bthomas
3 Replies
Login or Register to Ask a Question