LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-23-2006, 02:47 AM   #1
sqp1982
LQ Newbie
 
Registered: Jan 2006
Posts: 5

Rep: Reputation: 0
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
 
Old 03-23-2006, 02:51 AM   #2
sqp1982
LQ Newbie
 
Registered: Jan 2006
Posts: 5

Original Poster
Rep: Reputation: 0
I want use awk or sed or grep to fix it
thanks
 
Old 03-23-2006, 05:04 AM   #3
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
what have you tried so far !!! ???

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
 
Old 03-23-2006, 07:26 AM   #4
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 38
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.

Last edited by muha; 03-23-2006 at 08:18 AM.
 
Old 03-23-2006, 07:51 AM   #5
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Quote:
grep 'receive' -B 3 -A 1
what grep are you using? (distro ? )


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

Last edited by kshkid; 03-23-2006 at 07:53 AM.
 
Old 03-23-2006, 07:59 AM   #6
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 38
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
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 ..
 
Old 03-23-2006, 08:12 AM   #7
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
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 !!!
 
Old 03-23-2006, 09:27 PM   #8
sqp1982
LQ Newbie
 
Registered: Jan 2006
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
Old 03-24-2006, 07:35 AM   #9
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 38
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
 
Old 03-28-2006, 05:37 AM   #10
sqp1982
LQ Newbie
 
Registered: Jan 2006
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Difficult sed/awk question 3saul Linux - Software 2 03-04-2006 02:49 AM
Simple question about sed or awk setianusa Programming 2 09-16-2005 03:57 PM
Simple bash/awk/sed scripting question R00ts Programming 4 04-16-2005 02:55 AM
awk/sed help pantera Programming 1 05-13-2004 11:59 PM
sed or awk question - replace caps with small letters computera Linux - General 1 12-30-2003 04:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration