Find sessionids after match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find sessionids after match
# 1  
Old 06-09-2015
Find sessionids after match

Hi Guys,

I need a help on writing shell scripting where I am stuck on finding keyword after a grep match.

file.log (sample)
Code:
line1 -> ppp dfasf adgasg session out sfsgsgsghsgh
line2 -> sdaf agasg sessionid: 1234Nhsh34 sdagsjhsh srhs
line3 -> sdgshytnba sessionid: 453omgt  thdjdh gfhjdj
line4 -> sdshdh fhadhj kdrtryf session out pekgtocv
line5 -> eropjsdf dgfp sessionid: 453omgt ppwnf
line6 -> eed bbbvf sessionid: 1234Nhsh34 dgswq dfvg


Example:
There is one log file where I grep one string like 'session out' and straight after this I need to get all the 'sessionid' before next 'session out' and scan to compare each sessionid to find if same sessionid exist in same log file.

Please let me know if more details are required.

Last edited by vbe; 06-09-2015 at 12:28 PM.. Reason: code tags please
# 2  
Old 06-09-2015
Not sure I understand. Please provide the logics to apply and the desired output.
# 3  
Old 06-09-2015
line1 -> ppp dfasf adgasg session out sfsgsgsghsgh
line2 -> sdaf agasg sessionid: 1234Nhsh34 sdagsjhsh srhs
line3 -> sdgshytnba sessionid: 453omgt thdjdh gfhjdj
line4 -> sdshdh fhadhj kdrtryf session out pekgtocv
line5 -> eropjsdf dgfp sessionid: 453omgt ppwnf
line6 -> eed bbbvf sessionid: 1234Nhsh34 dgswq dfvg

like I grep session out from file.log and after that in next few lines I encounter sessionid: 1234Nhsh34 and this sessionid is repeated in file.log then system will send a alert mail to client.

Last edited by Don Cragun; 06-09-2015 at 11:29 PM.. Reason: Add ICODE tags.
# 4  
Old 06-09-2015
What's wrong with only looking for duplicate sessionid's?
Code:
awk '{for (i=2;i<=NF;i++) if ($(i-1)=="sessionid:") if ($i in s) {print "duplicate",$i,"in lines",s[$i],"and",NR} else {s[$i]=NR; break}}' file.log
duplicate 453omgt in lines 3 and 5
duplicate 1234Nhsh34 in lines 2 and 6

# 5  
Old 06-09-2015
Code:
awk '{for(i=2;i<=NF;i++) if ($(i-1)=="sessionid:") if ($i in s) {print "duplicate",$i,"in lines",s[$i],"and",NF} else {s[$i]=
NF; break}}' server.log


-bash-3.2$ ./testscript.sh
awk: syntax error near line 1
awk: illegal statement near line 1
awk: bailing out near line 1

Thanks (MadeInGermany ) for your prompt response.

Actually there are around so many server files for which I am trying to create script where humongous sessionids will be there.
Therefore, I am differentiating or considering those sessionids which are encountered only after or before <session out> keyword.

Last edited by Don Cragun; 06-09-2015 at 11:30 PM.. Reason: Add CODE tags.
# 6  
Old 06-09-2015
The /usr/bin/awk in Solaris does not recognize the ($i in s)lookup.
Use nawk or /usr/xpg4/bin/awk instead!
# 7  
Old 06-09-2015
Oh sweet! that removed errors. Thanks

Is there a way we can also print its associated timestamp.

---------- Post updated at 04:35 PM ---------- Previous update was at 04:31 PM ----------

Is this checking the sessionid: associated string i.e 1234Nhsh34 (sessionid: 1234Nhsh34 )
and then comparing in whole log file.
As I have to create a cronjob for this.

Last edited by Don Cragun; 06-09-2015 at 11:31 PM.. Reason: Add ICODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Match file and find count

Hi All, I have transaction in one file.I want to match that to another file and find the number of time the transaction is available on the other file.I need to take each record from TRANSFILE and match that with SPEND FILE and find the number of counts of the transaction TRANSFILE: ... (4 Replies)
Discussion started by: arunkumar_mca
4 Replies

2. Shell Programming and Scripting

Find the exact and best match between 2 files

Dear Forum, File1: Reference 4474189 United Kingdom Mobile 4474188 United Kingdom Mobile 4474187 United Kingdom Mobile 447 United Kingdom 93 AFGHANISTAN 0093 1907 ALASKA 001907 355 ALBANIA 00355 35568 ALBANIA MOBILE 0035568 35569 ALBANIA MOBILE 0035569 213 ALGERIA 00213 2137 ALGERIA... (2 Replies)
Discussion started by: yahyaaa
2 Replies

3. UNIX for Dummies Questions & Answers

Find match between two datasetsand add value

Dear all, could you help with following: i have two large datasets that contain two columns. dataset1: 752566 rs3094315 752721 rs3131972 753541 rs2073813 760300 rs11564776 768448 rs12562034 776546 rs12124819 dataset2: 55550 0.000000 82571 0.080572 88169 0.092229... (3 Replies)
Discussion started by: kush
3 Replies

4. Shell Programming and Scripting

Use of sed to find everything after first match!

Hi Guys So far I have got this to work: set x = temp1:temp2:temp3 echo $x | sed 's/.*:\(.*\).*/\1/' Answer: temp3 But I want answer as temp2:temp3, that is everything after the first ":" is found. If anybody can help with a bit of description that will be great. Thanks in Advance (1 Reply)
Discussion started by: dixits
1 Replies

5. Shell Programming and Scripting

Help me with s script to find exact match

Hi, im extracting data from oracle DB. As the data is case sensitive, i have to extract the data which doesn't match exactly. im poor in unix scripting, can someone plz help me with a script. Here are the details. Need to compare the second column of the each line of the file1.csv with the data in... (5 Replies)
Discussion started by: JSKOBS
5 Replies

6. Shell Programming and Scripting

Find out match characters on all lines

I have a file with 22 lines. Each line has only 5 different chars, no white space, and each line is 3,278,824 in length. The 5 chars is "-", "A", "B", "C", "D". Below is an example of the first 25 chars of the first four lines of the file. -----ABCDA--CD-BBBBB----D --A--ABCD--DCD-BBBBC-----... (12 Replies)
Discussion started by: cwzkevin
12 Replies

7. Shell Programming and Scripting

Match look up file and find result

Hi I ahve a lookup file wiht seven words CD HT CAD HT T1D T2D BDanother file contain data like this CHRM1 P11229 Pirenzepine DAP000492 Peptic ulcer disease Approved T2D CHRM1 P11229 Glycopyrrolate DAP001116 Anesthetic Approved T2D CHRM1 P11229 ... (7 Replies)
Discussion started by: manigrover
7 Replies

8. Shell Programming and Scripting

Find out the match data content?!

Hi, Long list of Input file1 content: 1285_t 4860_i 4817_v 8288_c 9626_a . . . Long list of Input file2 content: 1285_t chris germany 8288_c steve england 9626_a dave swiss 9260_s stephanie denmark . . . (14 Replies)
Discussion started by: patrick87
14 Replies

9. Shell Programming and Scripting

find: No match due to find command being argument

I am using csh and getting the error "find: No match." but I cannot figure out why. What I am trying to do is set the find command to a variable and then execute the variable as a command. I ran it through a debugger and it looks like $FIND is getting set but the find command can not actually be... (2 Replies)
Discussion started by: mst3k4l
2 Replies

10. Shell Programming and Scripting

How to find first match and last match in a file

Hi All, I have a below file: ================== 02:53 pravin-root 02:53 pravin-root 03:05 pravin-root 02:55 pravin1-root 02:59 pravin1-root ================== How do I find the first and last value of column 1. For example, how do I find 02:53 is the first time stamp and 03:05 is... (3 Replies)
Discussion started by: praving5
3 Replies
Login or Register to Ask a Question