I have a question about awk or sed
I have a file like this
Session 1 192.168.0.1 send something ----------------- Session 2 192.168.0.2 send something ----------------- Session 3 192.168.0.3 receive something ------------------ Session 4 202.201.222.11 receive something ------------------ Session 5 10.96.0.3 receive something ------------------ I want the result like this Session 3 192.168.0.3 receive something ------------------ Session 5 10.96.0.3 receive something ------------------ that means ip is not "202.201.222.11" and have "receive" I'm so sorry for my bad english thank you |
I want use awk or sed or grep to fix it
thanks |
what have you tried so far !!! ??? :tisk:
here it is, Code:
# !/usr/bin/ksh |
This also works as a command. Save the input-file as 'file' first
: /edit (tanks kshkid !) Code:
cat file|sed -n '/^$/,/202.201.222.11/p'|grep -B 3 -A 1 'receive'|grep -v '^--$' sed -n '/^$/,/202.201.222.11/p' Cut out the lines after 202.201.222.11 (it contains 'receive') Actually: show only blocks between empty lines and stop output after 202.201.222.11 is encountered. grep -B 3 -A 1 'receive' So now only lines with 'receive' do not have the previous line with 202.201.222.11 Only show lines with 'receive'; -B 3 shows also the three previous lines. -A 1 also shows the next line '-----------' The option -B and -A cause grep to place a line containing -- between contiguous groups of matches. grep -v '^--$' Filter out the lines with only -- on them that were put in by grep between matches. |
Quote:
syntactically it should throw unable to open error and it is !!! :) |
grep-2.5.1a-5 on suse10.0
What do you mean: the sequence in "grep 'receive' -B 3 -A 1" is not in the form of "grep [options] PATTERN [FILE...]" ?? That's right, although it works on my machine :D So it should be: Code:
sed -n '/^$/,/202.201.222.11/p' file |grep -B 3 -A 1 'receive' |grep -v '^--$' |
Quote:
I didnt mean it that way. with Code:
grep 'receive' -B 3 -A Thanks for the reply !!! |
I'm so sorry , I think my example was to short , so I put the complete example here
Sat 2006-03-18 00:00:27: Session 9991; child 3 Sat 2006-03-18 00:00:27: [9991:3] receive POP [192.168.0.1 : 3486] Sat 2006-03-18 00:00:27: [9991:3] --> +OK abc.com.cn POP MDaemon 7.1.2 ready <MDAEMON-F200603180000.AA002746MD4560@abc.com.cn> Sat 2006-03-18 00:00:27: [9991:3] <-- USER peng.chen Sat 2006-03-18 00:00:27: [9991:3] --> +OK peng.chen... Sat 2006-03-18 00:00:27: [9991:3] <-- PASS ****** Sat 2006-03-18 00:00:27: [9991:3] --> +OK peng.chen@abc.com.cn Sat 2006-03-18 00:00:27: [9991:3] <-- STAT Sat 2006-03-18 00:00:27: [9991:3] --> +OK 0 0 Sat 2006-03-18 00:00:27: [9991:3] <-- QUIT Sat 2006-03-18 00:00:27: [9991:3] --> +OK peng.chen@abc.com.cn abc.com.cn POP Sat 2006-03-18 00:00:27: [9991:3] POP connect finish Sat 2006-03-18 00:00:27: ---------- Sat 2006-03-18 00:00:27: Session 9992; child 6 Sat 2006-03-18 00:00:27: [9991:3] receive POP [202.201.222.11:3486] Sat 2006-03-18 00:00:27: [9991:3] --> +OK abc.com.cn POP MDaemon 7.1.2 ready <MDAEMON-F200603180000.AA002746MD4560@abc.com.cn> Sat 2006-03-18 00:00:27: [9991:3] <-- USER yi.yi Sat 2006-03-18 00:00:27: [9991:3] --> +OK yi.yi... Sat 2006-03-18 00:00:27: [9991:3] <-- PASS ****** Sat 2006-03-18 00:00:27: [9991:3] --> +OK yi.yi@abc.com.cn Sat 2006-03-18 00:00:27: [9991:3] <-- STAT Sat 2006-03-18 00:00:27: [9991:3] --> +OK 0 0 Sat 2006-03-18 00:00:27: [9991:3] <-- QUIT Sat 2006-03-18 00:00:27: [9991:3] --> +OK yi.yi@abc.com.cn abc.com.cn POP Sat 2006-03-18 00:00:27: [9991:3] POP connect finish Sat 2006-03-18 00:09:16: Session 30; child 1; thread 1380 Sat 2006-03-18 00:09:15: [30:1] receive SMTP from [222.111.212.33 : 2765] connect Sat 2006-03-18 00:09:15: [30:1] --> 220 abc.com.cn ESMTP MDaemon 7.1.2; Sat, 18 Mar 2006 00:09:15 +0800 Sat 2006-03-18 00:09:16: [30:1] <-- EHLO hfdfgrt.net Sat 2006-03-18 00:09:16: [30:1] Performing lookup on hfdfgrt.net (looking for 222.111.212.33) Sat 2006-03-18 00:09:16: [30:1] unknown Domain Sat 2006-03-18 00:09:16: [30:1] --> 250-abc.com.cn Hello hfdfgrt.net, nice to meet you Sat 2006-03-18 00:09:16: [30:1] --> 250-ETRN Sat 2006-03-18 00:09:16: [30:1] --> 250-AUTH=LOGIN Sat 2006-03-18 00:09:16: [30:1] --> 250-AUTH LOGIN CRAM-MD5 Sat 2006-03-18 00:09:16: [30:1] --> 250-8BITMIME Sat 2006-03-18 00:09:16: [30:1] --> 250-STARTTLS Sat 2006-03-18 00:09:16: [30:1] --> 250 SIZE 10000000 Sat 2006-03-18 00:09:16: [30:1] <-- RSET Sat 2006-03-18 00:09:16: [30:1] --> 250 seset ** Sat 2006-03-18 00:09:16: [30:1] <-- MAIL FROM:<fdfers4@hfdfgrt.net> Sat 2006-03-18 00:09:16: [30:1] Performing lookup on hfdfgrt.net (looking for 222.111.212.33) Sat 2006-03-18 00:09:16: [30:1] ***************** Sat 2006-03-18 00:09:16: [30:1] --> 250 <fdfers4@hfdfgrt.net> , Sat 2006-03-18 00:09:16: [30:1] <-- RCPT TO:<qzzhang@abc.com> Sat 2006-03-18 00:09:16: [30:1] **************** Sat 2006-03-18 00:09:16: [30:1] --> 550 <qzzhang@abc.com>, Sat 2006-03-18 00:09:16: [30:1] <-- QUIT Sat 2006-03-18 00:09:16: [30:1] --> 221 see you Sat 2006-03-18 00:09:16: [30:1] SMTP finish Sat 2006-03-18 00:09:16: ---------- I want the Session result is not SMTP and not the 202.201.222.11 , like this Sat 2006-03-18 00:00:27: Session 9991; child 3 Sat 2006-03-18 00:00:27: [9991:3] receive POP [192.168.0.1 : 3486] Sat 2006-03-18 00:00:27: [9991:3] --> +OK abc.com.cn POP MDaemon 7.1.2 ready <MDAEMON-F200603180000.AA002746MD4560@abc.com.cn> Sat 2006-03-18 00:00:27: [9991:3] <-- USER peng.chen Sat 2006-03-18 00:00:27: [9991:3] --> +OK peng.chen... Sat 2006-03-18 00:00:27: [9991:3] <-- PASS ****** Sat 2006-03-18 00:00:27: [9991:3] --> +OK peng.chen@abc.com.cn Sat 2006-03-18 00:00:27: [9991:3] <-- STAT Sat 2006-03-18 00:00:27: [9991:3] --> +OK 0 0 Sat 2006-03-18 00:00:27: [9991:3] <-- QUIT Sat 2006-03-18 00:00:27: [9991:3] --> +OK peng.chen@abc.com.cn abc.com.cn POP Sat 2006-03-18 00:00:27: [9991:3] POP connect finish Sat 2006-03-18 00:00:27: ---------- Thanks |
From the sed-oneliners:
Quote:
This does what you want: Code:
sed -e '/./{H;$!d;}' -e 'x;/202.201.222.11\|SMTP/d;' file |
Quote:
|
All times are GMT -5. The time now is 06:19 PM. |