Getting various parts from the log


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting various parts from the log
# 1  
Old 08-10-2015
Getting various parts from the log

I am dealing with some app log, see example below:
Code:
22:16:13.601 [192.168.1.183] ClientSession(905)--Connection(5)--SELECT GETDATE()
22:16:13.632 [192.168.1.183] ClientSession(158)--Connection(5)--SELECT 1
22:16:13.632 [192.168.1.175] ClientSession(848)--Connection(6735)--SELECT 1

So far I needed to collect certain column from it, such as get all the sql commands:
Code:
$ cut -d'-' -f5 xx
SELECT GETDATE()
SELECT 1
SELECT 1

or get all the session ids
Code:
$ cut -d'(' -f2 xx|cut -d')' -f1
905
158
848

So, cut helps me with this sort of things. But in some cases I need to get several columns at once, such as get the timestamp and the sql command
such as
Code:
22:16:13.601 SELECT GETDATE()
22:16:13.632 SELECT 1
22:16:13.632 SELECT 1

For this I am using awk, but it seems quite cumbersome and I understand that if I have known enough regexp then sed would be a quick and easy solution here, but I don't.

My attempt to use sed for getting time and sql command
Code:
sed 's/\([0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9][0-9]\)\(.*--\)\(*\)/\1 \3/' log.txt

outputs whole thing, as if I did cat it

Please help me with sed solution.
# 2  
Old 08-10-2015
an awk (or not):
Code:
awk '
/SELECT/ {sub(".*SELECT", "SELECT"); print; }
' infile

or sed:
Code:
sed -n '/SELECT/s/.*SELECT/SELECT/p;' infile


Last edited by rdrtx1; 08-12-2015 at 04:12 PM..
# 3  
Old 08-10-2015
A modification to yours
Code:
sed -ne 's/\([0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9][0-9]\).*\(SELECT.*$\)/\1 \2/p' log.txt

Some others in Perl
Code:
perl -ne '/^(.+?)\s.+(SELECT.+$)/ and print "$1 $2\n"' log.txt

Code:
perl -ne '/^((?:\d{2}[:.]){3}\d{3}).*(SELECT.*$)/ and print "$1 $2\n"' log.txt

# 4  
Old 08-11-2015
Try also
Code:
awk -F- '{split ($1, T, " "); print T[1], $NF}' file
22:16:13.601 SELECT GETDATE()
22:16:13.632 SELECT 1
22:16:13.632 SELECT 1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get first four parts of the string?

I have the string: XXXX.YYYY_ZZZ.20180724.01.txt I need to get rid of .txt and get full four parts XXXX.YYYY_ZZZ.20180724.01 I did: CTL=`echo XXXX.YYYY_ZZZ.20180724.01.txt | rev | cut -d"." -f4 | rev` But got only YYYY_ZZZ What should I do to get all four parts of that... (4 Replies)
Discussion started by: digioleg54
4 Replies

2. Shell Programming and Scripting

Parsing a log file to cut off some parts

Dear all, I would like to use SQL's log file to extract information from it. This file can include four different types of instruction with the number of lines involved for each of them: -> (1) "INSERT" instruction with the number of lines inserted -> (2) "UPDATE" instruction with the... (4 Replies)
Discussion started by: dae
4 Replies

3. Shell Programming and Scripting

Split file into n parts.

Hi all: I have a 5-column tab-separated file. The only thing that I want to do with it is to split it. However, I want to split it with a 80/20 proportion -- randomized, if possible. I know that something like : awk '{print $0 ""> "file" NR}' RS='' input-file will work, but it only... (6 Replies)
Discussion started by: owwow14
6 Replies

4. Shell Programming and Scripting

Incrementing parts of ten digits number by parts

I have number in file which contains date and serial number: 2013101000. The last two digits are serial number (00). So maximum of serial number is 100. After reaching 100 it becomes 00 with incrementing 10 which is day with max 31. after reaching 31 it becomes 00 and increments 10... (31 Replies)
Discussion started by: Natalie
31 Replies

5. Shell Programming and Scripting

Combine two parts of a file

Hello All, I have a file like this APPLY ( 'INSERT INTO brdcst_media_cntnt ( cntnt_id ,brdcst_media_cntnt_cd ,cntnt_prvdr_cd ,data_src_type_cd ,cntnt_titl_nm ,cntnt_desc ,batch_dt ,batch_id ) VALUES ( :cntnt_id (3 Replies)
Discussion started by: nnani
3 Replies

6. Shell Programming and Scripting

Extract Parts of File

Hello All, I have a file like this Define schema flat_file_schema ( a varchar(20) ,b varchar(30) ,c varchar(40) ); (Insert into table ( a ,b ,c ) values ( 1 ,2 ,3 ); (4 Replies)
Discussion started by: nnani
4 Replies

7. Shell Programming and Scripting

Parts is parts, but all together ...

I understand the individual pieces of the following (with one exception ..), but how does it all work as one? find ${HOME}/reports/ -name surveyresult*.txt -exec ls -1 {} \; | /usr/xpg4/bin/grep -E \ "${HOME}/reports/surveyresult{14,14}.txt" | sort > ${ResultsFileList} Find all files like... (1 Reply)
Discussion started by: jdorn001
1 Replies

8. Shell Programming and Scripting

extract certain parts from a file

I have a logfile from which i need to extract certain pattern based on the time but the problem here is the time is not same for all days. Input file: Mon 12:34:56 abvjingjgg Mon 12:34:57 ofjhjgjhgh . . . Mon 22:30:00 kkfng . . . Mon 23:12:23 kjgsdafhkljf . . . Tue 01:04:54... (8 Replies)
Discussion started by: gpk_newbie
8 Replies

9. Shell Programming and Scripting

getting parts of a file

Hello, I'm trying to retreive certain bits of info from a file. the file contains a list like this info1:info2:info3:info4 info1:info2:info3:info4 info1:info2:info3:info4 info1:info2:info3:info4 how do i pick out only info2 or only info3 without the others? Thanks (11 Replies)
Discussion started by: bebop1111116
11 Replies

10. UNIX for Dummies Questions & Answers

cksum parts of a file

Every time we build an executable the date and time are put into the file, I need to run checksum on just the working lines.(IE, no header files) Is this even possible, if so how would I go about it? I am using a HP-UX server any help you can give me will be greatly appreciated. Thanks (6 Replies)
Discussion started by: crazykelso
6 Replies
Login or Register to Ask a Question