sed command to match log entries


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command to match log entries
# 1  
Old 07-23-2008
sed command to match log entries

Hello,

I would like to write a sed comman dwith pattern matching .to match those record which have some character(userd id) present in it.

Example :
Logfile contents --

127.0.0.1 - - [30/Apr/2008:22:48:20 +0530] "POST /cgi-bin/cgi-module-example.pl HTTP/1.1" 200 1866
127.0.0.1 - - [30/Apr/2008:22:48:44 +0530] "POST /cgi-bin/cgi-module-example.pl HTTP/1.1" 200 1921
127.0.0.1 - - [30/Apr/2008:22:49:28 +0530] "POST /cgi-bin/cgi-module-example.pl HTTP/1.1" 200 2145
127.0.0.1 - - [30/Apr/2008:22:49:39 +0530] "POST /cgi-bin/cgi-module-example.pl HTTP/1.1" 200 2165
127.0.0.1 - - [30/Apr/2008:22:51:43 +0530] "POST /cgi-bin/cgi-module-example.pl HTTP/1.1" 500 602
127.0.0.1 - jam [30/Apr/2008:22:53:02 +0530] "POST /cgi-bin/cgi-module-example.pl HTTP/1.1" 200 3228
127.0.0.1 - - [30/Apr/2008:22:53:54 +0530] "POST /cgi-bin/cgi-module-example.pl HTTP/1.1" 200 3228
127.0.0.1 - - [30/Apr/2008:22:54:58 +0530] "POST /cgi-bin/cgi-module-example.pl HTTP/1.1" 200 3234
127.0.0.1 - - [30/Apr/2008:22:55:26 +0530] "POST /cgi-bin/cgi-module-example.pl HTTP/1.1" 200 3234
127.0.0.1 - mpt [30/Apr/2008:22:56:19 +0530] "POST /cgi-bin/cgi-module-example.pl HTTP/1.1" 200 3234
127.0.0.1 - - [30/Apr/2008:22:57:04 +0530] "POST /cgi-bin/cgi-module-example.pl HTTP/1.1" 200 3229
127.0.0.1 - unix [30/Apr/2008:22:57:47 +0530] "POST /cgi-bin/cgi-module-example.pl HTTP/1.1" 200 3227
127.0.0.1 - forum [30/Apr/2008:22:58:14 +0530] "POST /cgi-bin/cgi-module-example.pl HTTP/1.1" 200 3218

---------
These are sample entries in a apache log file ..
I would like to write a sed command to out put those record which have id present in 3rd column .

Out put needed --

127.0.0.1 - jam [30/Apr/2008:22:53:02 +0530] "POST /cgi-bin/cgi-module-example.pl HTTP/1.1" 200 3228
127.0.0.1 - mpt [30/Apr/2008:22:56:19 +0530] "POST /cgi-bin/cgi-module-example.pl HTTP/1.1" 200 3234
127.0.0.1 - unix [30/Apr/2008:22:57:47 +0530] "POST /cgi-bin/cgi-module-example.pl HTTP/1.1" 200 3227
127.0.0.1 - forum [30/Apr/2008:22:58:14 +0530] "POST /cgi-bin/cgi-module-example.pl HTTP/1.1" 200 3218


------------------------------------

I tried some think like
sed -n /([0-9]\{3})\.\1\.\1 - .? [aA-zZ-0-9].*/p access_log

but i am getting syntax error
# 2  
Old 07-23-2008
With awk:

Code:
awk '$3 != "-"' file

# 3  
Old 07-23-2008
This would also work:
Code:
cat file | grep -v "\- \-"

# 4  
Old 07-23-2008
If you prefer sed you can do something like:

Code:
sed -n '/.*[0-9] - [a-z].*/p' file

# 5  
Old 07-23-2008
Since I happen to know the syntax of that file, I know you can get away with something even more simple:

Code:
grep -vF ' - [' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting out of sed on first NOT match

i have a large file where i want to look for any record that is is larger or smaller than 21 and if it is the case i want to report and break SED .. how can i achieve it ? i dont want sed to scan the complete file after one non match is found. (4 Replies)
Discussion started by: boncuk
4 Replies

2. Shell Programming and Scripting

sed print from last occurrence match until the end of last occurrence match

Hi, i have file file.txt with data like: START 03:11:30 a 03:11:40 b END START 03:13:30 eee 03:13:35 fff END jjjjjjjjjjjjjjjjjjjjj START 03:14:30 eee 03:15:30 fff END ggggggggggg iiiiiiiiiiiiiiiiiiiiiiiii I want the below output START (13 Replies)
Discussion started by: Jyotshna
13 Replies

3. Shell Programming and Scripting

Match exact String with sed command

I have a workaround to the problem i m posting, however if someone wants to look at my query and respond ... i will appreciate. This is in reference to this thread -> https://www.unix.com/shell-programming-and-scripting/267630-extract-between-two-exact-matched-strings.html I have data.txt as... (11 Replies)
Discussion started by: mohtashims
11 Replies

4. Shell Programming and Scripting

Need to log the sed command used to replace

In a shell script I am replacing the asterisks in a file: sed "s/\*/"0"/g" /home/download/$COMPANY_CODE/file_new > /home/download/$COMPANY_CODE/fileI need to log which positions were replaced & position(01:20) from the line it was replaced in. I am not sure how to do so. Also, instead of... (11 Replies)
Discussion started by: tomj5141
11 Replies

5. Shell Programming and Scripting

sed match

Hi can anyone help with the following: echo "Date range on 5th May is between -010516 and 050516- please continue "| sed 's/\(.*-\)\(.*-\)\(.*$\)/\2/' output 010516 and 050516- What i need is to include the - to be included. Desired output: -010516 and 050516- I know... (11 Replies)
Discussion started by: andy391791
11 Replies

6. Shell Programming and Scripting

Sed Pattern Match

Hi, I would like to use SED to do the following string replacement: asd1abc to www1cda asd2abc to www2cda ... asd9abc to www9cda I can use 'asd.abc' to find the orignal string, however I don't know how to generate the target string. Any suggestion? Thanks, ... (2 Replies)
Discussion started by: mail4mz
2 Replies

7. UNIX for Dummies Questions & Answers

sed can't match '\n' ?!

Hi: it seems very strange. there is a file with multiple lines. After I squeezed out the consecutive blank lines (and some other text processing), somehow the sed '/\n/! d' file can not generate any output, as if it can't find any line with newline. the file is has many lines, so... (9 Replies)
Discussion started by: phil518
9 Replies

8. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line. ... (1 Reply)
Discussion started by: cbo0485
1 Replies

9. Shell Programming and Scripting

how to use a command in sed s/match/replacement

hi, how can i make use of a command in the replacement segment.. cat a | sed '/^*]\{3\}$/{ s/\(.*\)/REPLACEMENT/g }' suppose if I want to use a awk command in the replacement section , how to achieve that ? Thanks (1 Reply)
Discussion started by: AbhishekG
1 Replies

10. Shell Programming and Scripting

how do I negate a sed match

I have a text file that has links in it. I can write a match for sed to replace the link with anything. For example: http://www.google.com becomes XxX But what I'm after is not to replace the link with something but to remove everything else and just leave the link. I want a... (5 Replies)
Discussion started by: muxman
5 Replies
Login or Register to Ask a Question