LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   I have a question about awk or sed (https://www.linuxquestions.org/questions/programming-9/i-have-a-question-about-awk-or-sed-427570/)

sqp1982 03-23-2006 02:47 AM

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

sqp1982 03-23-2006 02:51 AM

I want use awk or sed or grep to fix it
thanks

kshkid 03-23-2006 05:04 AM

what have you tried so far !!! ??? :tisk:

here it is,
Code:

# !/usr/bin/ksh

awk '/receive/ { print NR }' data | while read line
do
if [ ! `awk '{ if( NR == '$(($line - 1))' ) print }' data` = '202.201.222.11' ]
then
awk '{ if(NR == '$(($line - 2))' || NR == '$(($line - 1))' || NR == '$line') print }'
data
echo ""
fi
done

exit 0


muha 03-23-2006 07:26 AM

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 '^--$'
OR
sed -n '/^$/,/202.201.222.11/p' file |grep -B 3 -A 1 'receive'|grep -v '^--$'

explanation:
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.

kshkid 03-23-2006 07:51 AM

Quote:

grep 'receive' -B 3 -A 1
what grep are you using? (distro ? )


syntactically it should throw unable to open error
and it is !!! :)

muha 03-23-2006 07:59 AM

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 '^--$'
I did not mean that "grep 'receive' -B 3 -A 1" can be used without input, it recieves the input from the pipe ..

kshkid 03-23-2006 08:12 AM

Quote:

I did not mean that "grep 'receive' -B 3 -A 1" can be used without input, it recieves the input from the pipe
Using: grep on SunOS 5.9
I didnt mean it that way.

with
Code:

grep 'receive' -B 3 -A
it would search for the pattern 'receive' in files -B 3 -A

Thanks for the reply !!!

sqp1982 03-23-2006 09:27 PM

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

muha 03-24-2006 07:35 AM

From the sed-oneliners:
Quote:

# print paragraph if it contains AAA (blank lines separate paragraphs)
# HHsed v1.5 must insert a 'G;' after 'x;' in the next 3 scripts below
sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;'
Using \| as an OR-statement we get 202.201.222.11\|SMTP; d for delete.
This does what you want:
Code:

sed -e '/./{H;$!d;}' -e 'x;/202.201.222.11\|SMTP/d;' file
HTH ;)

sqp1982 03-28-2006 05:37 AM

Quote:

Originally Posted by muha
From the sed-oneliners:

Using \| as an OR-statement we get 202.201.222.11\|SMTP; d for delete.
This does what you want:
Code:

sed -e '/./{H;$!d;}' -e 'x;/202.201.222.11\|SMTP/d;' file
HTH ;)

Thank you very much :)


All times are GMT -5. The time now is 06:19 PM.