Need help with a shell script for finding a certain pattern from log file


 
Thread Tools Search this Thread
Operating Systems Linux Red Hat Need help with a shell script for finding a certain pattern from log file
# 8  
Old 02-08-2011
Quote:
Originally Posted by rockf1bull
Sorry for bothering you, I just did that and tried few sed and uniq options to play around as well.

Thanks Frank for all your help, amazing how much one can learn in less than 24 hours; this was MUCH helpful for a newbie like me.

Best Regards,
Andy
No problem, you're welcome.Smilie
# 9  
Old 02-09-2011
I need some more help people and would be posting here shortly

Thanks in advance,
Andy

---------- Post updated at 12:59 PM ---------- Previous update was at 12:58 PM ----------

Ok, I must admit I have another small problem I need to tweak.
Initially I wanted only the URL portion of a line from a log file however, what I am after now is I need a little longer URL something like below.
Code:
10.155.65.5 - - [20/Jan/2011:07:41:58 +0100] "POST /cas/login?post=true&service=http://test.domain.ca:8000/psp/ppf88prd/?cmd=login%26languageCd=CAN%26userid=VP1%26pwd=TEST123 HTTP/1.1" 200 888

I need http://test.domain.ca:8000/psp/ instead of just http://test.domain.ca like below.

Can someone please help if how I do achieve that. I did try playing with a earlier script I had been given by Frank as below though unfortunately it didn't work
Code:
sed -n 's!.*service=\(http://[^:]*\):.*!\1!p' logfile > newfile

Also one more help would be to be aware of a script which would need to find a similar result from around 20 files instead of just 1 file as of now. I am doing it manually for now though need some help in this too.
The file format is something like below -
Code:
abc-accent.2011-01-20.log
abc-accent.2011-02-21.log
abc-accent.2011-03-22.log
abc-accent.2011-04-23.log
abc-accent.2011-05-24.log

Again, thanks once again for all help offered already!!!
Cheers,
Andy

---------- Post updated at 01:08 PM ---------- Previous update was at 12:59 PM ----------

Ok, I just found another script that may require some tweaking which I will do later though someone please confirm if it will suit the purpose. (Again I would be testing it in some time on a test box to be sure)
It basically searches and replaces strings while searching a certain parrtern of files in a directory we specify.

Code:
#!/bin/bash
OLD="xyz"
NEW="abc"
DPATH="/home/you/foo/*.txt"
BPATH="/home/you/bakup/foo"
TFILE="/tmp/out.tmp.$$"
[ ! -d $BPATH ] && mkdir -p $BPATH || :
for f in $DPATH
do
  if [ -f $f -a -r $f ]; then
    /bin/cp -f $f $BPATH
   sed "s/$OLD/$NEW/g" "$f" > $TFILE && mv $TFILE "$f"
  else
   echo "Error: Cannot read $f"
  fi
done
/bin/rm $TFILE


Any suggestions as always are quiet welcome.

Thanks,
Andy

Last edited by Franklin52; 02-09-2011 at 04:40 AM.. Reason: Please use code tags, thank you
# 10  
Old 02-09-2011
Quote:
Originally Posted by rockf1bull
I need http://test.domain.ca:8000/psp/ instead of just http://test.domain.ca like below.

Can someone please help if how I do achieve that. I did try playing with a earlier script I had been given by Frank as below though unfortunately it didn't work
Try this one:
Code:
sed -n 's!.*service=\(http://[^/]*/[^/]*/\).*!\1!p' file

Quote:
Originally Posted by rockf1bull
Also one more help would be to be aware of a script which would need to find a similar result from around 20 files instead of just 1 file as of now. I am doing it manually for now though need some help in this too.
You can use something like this to loop through the abs-accent log files:
Code:
ls abc-accent*.log |
while read file
do
  sed -n 's!.*service=\(http://[^/]*/[^/]*/\).*!\1!p' "$file" > URL_file
done

This User Gave Thanks to Franklin52 For This Post:
# 11  
Old 02-10-2011
You have been my saviour again Frank and the first script worked for me!!

I am yet to test the script part due to some meetings coming up ahead. Just can't express words when people provide so much help so quickly!!

I will post complete information on this thread shortly for all other newbies like me Smilie

Cheers,
Andy

---------- Post updated 02-10-11 at 11:23 AM ---------- Previous update was 02-09-11 at 06:27 PM ----------

Ok, following is what worked for me after MUCH required help from Franklin!!

I used the same script provided by Franklin to get my URLs filtered -

PHP Code:
sed -'s!.*service=\(http://[^/]*/[^/]*/\).*!\1!p' file 
However, I still haven't tested the script part provided by Franklin yet and I will post the output later.

Another problem I faced while trying to filter output received after using Franklin's script was I had few URLs with BIG strings with special characters and had to use following to get rid of them. (To get rid of &, ? and ' ' basically AND have them sorted)

PHP Code:
cat old.file awk -\& '{print $1}' awk -\? '{print $1}' |awk -' ' '{print $1}'sort -output.txt 
Thanks guys for all your support and now I have another issue cropping up which I will post in the next post since otherwise this post will be way too big.

Cheers,
Andy

---------- Post updated at 11:28 AM ---------- Previous update was at 11:23 AM ----------

One more problem I have is while trying to remove duplicate lines, I need to treat lower and upper cases in URLs carefully as below -

For following duplicate lines, I need to have only two URLs since currently they are all being treated as UNIQUE URLs.
(note: Separate IPs don't matter since I am only concerned with lower and upper case letters)

PHP Code:
56.555.72.69/crm_ababcdves/
81.745.42.59/CRM_Ababcdves/
38.475.62.19/squitv3/
92.625.42.89/Squitv3/
37.288.30.12/cview/
63.598.30.89/Cview/
85.048.30.52/CView
So final output should be -

PHP Code:
56.555.72.69/crm_ababcdves/
38.475.62.19/squitv3/
37.288.30.12/cview
Now if someone can help me with this, that would be really great since I am a newbie on these things so far though getting better since past few days.

Cheers,
Andy
# 12  
Old 02-10-2011
Is this what you're looking for?

Code:
$ cat file
56.555.72.69/crm_ababcdves/&kllk?jkjk op
81.745.42.59/CRM_Ababcdves/&kllk?jkjk op
38.475.62.19/squitv3/
92.625.42.89/Squitv3/&kllk?jkjk op
37.288.30.12/cview/
63.598.30.89/Cview/&kllk?jkjk op
85.048.30.52/CView/
$
$ awk -F"/" '{
  gsub("[ &,?].*",x)
  s=toupper($2)
}
s in a {next}
{a[s]}1' file
56.555.72.69/crm_ababcdves/
38.475.62.19/squitv3/
37.288.30.12/cview/
$

# 13  
Old 02-10-2011
Not sure how do I run that?
It looks like a small .sh script to me.
So please tell me whether I should save it as a sh file with all duplicate entries in it to ge the final output?

Regards,
Andy
# 14  
Old 02-10-2011
Code:
awk -F"/" '{
  gsub("[ &,?].*",x)
  s=toupper($2)
}
s in a {next}
{a[s]}1' file

This is a command to get rid of the characters &, ? and ' ' and to remove the duplicates.

The example above shows an example of the content of a file with those characters, the awk command and the final output.

You could use the command in a script or on the command line.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding the file name in a directory with a pattern

I need to find the latest file -filename_YYYYMMDD in the directory DIR. the below is not working as the position is shifting each time because of the spaces between(occuring mostly at file size field as it differs every time.) please suggest if there is other way. report =‘ls -ltr... (2 Replies)
Discussion started by: archana25
2 Replies

2. Shell Programming and Scripting

Finding file extension on C Shell script

I very new to C Shell. I am trying to do is read from Command line. Find the if the file is zip, .txt, symbloic link,pipe, unknow (if file is not zip, txt, sy....) here is what I what got so far. I am very stuck atm Please help me out : If the file is symblooc link what file is link to ... (12 Replies)
Discussion started by: madbull41
12 Replies

3. Shell Programming and Scripting

Script to search for a pattern in 30 minutes from a log file

Hello All, I have to write a script which will search for diffrent patterns like "Struck" "Out of Memory" , etc from a log file in Linux box's. Now I will be executing a cron job to find out the results by executing the script once in every 30 minutes. suppose time is 14-04-29:05:31:09 So I... (3 Replies)
Discussion started by: Shubhasis Mathr
3 Replies

4. Shell Programming and Scripting

Finding file pattern in ksh 88

Hi, I've to find the file which has the pattern "Delete Report for History Tables" and need to search this file pattern from directory which has sub directories as well. I'm using ksh 88 Please suggest me which command will be used to find the file pattern . Thanks. (1 Reply)
Discussion started by: smile689
1 Replies

5. Shell Programming and Scripting

Finding the pattern and replacing the pattern inside the file

i have little challenge, help me out.i have a file where i have a value declared and and i have to replace the value when called. for example i have the value for abc and ccc. now i have to substitute the value of value abc and ccc in the place of them. Input File: go to &abc=ddd; if... (16 Replies)
Discussion started by: saaisiva
16 Replies

6. Shell Programming and Scripting

Finding log files that match number pattern

I have logs files which are generated each day depending on how many processes are running. Some days it could spin up 30 processes. Other days it could spin up 50. The log files all have the same pattern with the number being the different factor. e.g. LOG_FILE_1.log LOG_FILE_2.log etc etc ... (2 Replies)
Discussion started by: atelford
2 Replies

7. Homework & Coursework Questions

shell script that can create, monitor the log files and report the issues for matching pattern

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write an automated shell program(s) that can create, monitor the log files and report the issues for matching... (0 Replies)
Discussion started by: itian2010
0 Replies

8. Shell Programming and Scripting

Script to monitor the pattern in the log file

hi All, how to find a pattern in the log file & display the above and below line for example in the log file, i have many lines, whenever i search for "Category" it should display the above line with only few parameter like i want only the location name & department name Thu Jul 02 11:05:23... (2 Replies)
Discussion started by: rithick256
2 Replies

9. Shell Programming and Scripting

script for finding an error from a log file

Hi , I have a doubt about a shell script to find an ERROR from the log file. But I need to specify a the scan from a particular date and time in the log. Till now I have developed the following script. Please suggest what shall I add in this for date and time. If the script finds a particular... (5 Replies)
Discussion started by: himvat
5 Replies

10. Shell Programming and Scripting

help with finding & replacing pattern in a file

Hi everyone. Could u be so kind and help me with on "simple" shell script? 1. i need to search a file line by line for a pattern. example of a lines in that file 2947 domain = feD,id = 00 0A 02 48 17 1E 1D 39 DE 00 0E 00,Name Values:snNo = f10 Add AttFlag = 0 2. i need to find... (0 Replies)
Discussion started by: dusoo
0 Replies
Login or Register to Ask a Question