LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-17-2012, 06:52 AM   #1
mmhs
Member
 
Registered: Oct 2010
Posts: 101

Rep: Reputation: 1
extract Previous line search


hi guys i have a problem

i have a file with this format

Quote:
first@xyz.com
Creation Date: Wed, 20 Jan 2009
POP3 Last Login Date: Thu, 01 Jan 1970
IMAP Last Login Date: Thu, 01 Jan 1970
WebMail Last Login Date: Tue, 10 Mar 2010


first1@xyz.com
Creation Date: Wed, 3 Jan 2009
POP3 Last Login Date: Thu, 01 Jan 1970
IMAP Last Login Date: Thu, 01 Jan 1970
WebMail Last Login Date: Tue, 12 Mar 2010


first2@xyz.com
Creation Date: Wed, 14 Aug 2009
POP3 Last Login Date: Thu, 01 Jan 1970
IMAP Last Login Date: Thu, 01 Jan 1970
WebMail Last Login Date: Tue, 10 Mar 2010


first3@xyz.com
Creation Date: Wed, 22 Aug 2009
POP3 Last Login Date: Thu, 01 Jan 1970
IMAP Last Login Date: Thu, 01 Jan 1970
WebMail Last Login Date: Tue, 10 Mar 2010


first4@xyz.com
Creation Date: Wed, 14 Aug 2009
POP3 Last Login Date: Thu, 01 Jan 1970
IMAP Last Login Date: Thu, 01 Jan 1970
WebMail Last Login Date: Wed, 14 Aug 2009

.
.
.
.
.
.
.
this file contain 3000 records

i want to extract only emails like first@xyz.com those were created on Wed, 14 Aug 2009 or their creation date is
Creation Date: Wed, 14 Aug 2009


and is there any solution to find emails their creation date is
Wed, 14 Aug 2009
and their last WebMail Last Login Date is same ??

for example in this sample we should just extract this email first4@xyz.com
 
Old 03-17-2012, 07:21 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Is this what you are looking for:
Code:
awk 'BEGIN { RS="" ; FS="\n" ; ORS="\n" ; OFS="" } {crea_date=sub(/.*, /,"",$2) ; web_date=sub(/.*, /,"",$5) ; if ( $2 == $5 ) { print $1 } }' mail.in
Or in a easy readable script form:
Code:
#!/bin/bash

awk 'BEGIN { RS="" ; FS="\n" ; ORS="\n" ; OFS="" }
{
  crea_date=sub(/.*, /,"",$2)
  web_date=sub(/.*, /,"",$5)
  if ( $2 == $5 ) { print $1 }
}
' mail.in
Hope this helps.
 
Old 03-17-2012, 07:23 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,125

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Perhaps you should try first, rather than just ask.
 
1 members found this post helpful.
Old 03-17-2012, 07:43 AM   #4
mmhs
Member
 
Registered: Oct 2010
Posts: 101

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by druuna View Post
Hi,

Is this what you are looking for:
Code:
awk 'BEGIN { RS="" ; FS="\n" ; ORS="\n" ; OFS="" } {crea_date=sub(/.*, /,"",$2) ; web_date=sub(/.*, /,"",$5) ; if ( $2 == $5 ) { print $1 } }' mail.in
Or in a easy readable script form:
Code:
#!/bin/bash

awk 'BEGIN { RS="" ; FS="\n" ; ORS="\n" ; OFS="" }
{
  crea_date=sub(/.*, /,"",$2)
  web_date=sub(/.*, /,"",$5)
  if ( $2 == $5 ) { print $1 }
}
' mail.in
Hope this helps.
thx man for your help but it didn't work
 
Old 03-17-2012, 07:50 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

First: WHAT did not work, WHAT is the error you are seeing (if any). Please provide input we can work with!

As in your other thread, it does work on my side.

Is the input file a normal text file?

EDIT: The code I posted can be shortened a bit, the crea_date= and web_date= parts aren't needed. This would do:
Code:
awk 'BEGIN { RS="" ; FS="\n" ; ORS="\n" ; OFS="" } {sub(/.*, /,"",$2) ; sub(/.*, /,"",$5) ; if ( $2 == $5 ) { print $1 } }' mail.in

Last edited by druuna; 03-17-2012 at 07:56 AM.
 
1 members found this post helpful.
Old 03-17-2012, 08:00 AM   #6
mmhs
Member
 
Registered: Oct 2010
Posts: 101

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by druuna View Post
Hi,

First: WHAT did not work, WHAT is the error you are seeing (if any). Please provide input we can work with!

As in your other thread, it does work on my side.

Is the input file a normal text file?
thx man for your help no it didn't give any errors

i think it didn't find match records

the file is in a file new.txt

and it's exactly like this

PHP Code:
first1@xyz.com
    Creation Date
Wed14 Aug 2009 15:08:01
    POP3 Last Login Date
Thu01 Jan 1970 03:30:00
    IMAP Last Login Date
Thu01 Jan 1970 03:30:00
    WebMail Last Login Date
Tue13 Mar 2012 15:23:20
first2
@xyz.com
    Creation Date
Thu19 Jan 2012 21:24:26
    POP3 Last Login Date
Thu01 Jan 1970 03:30:00
    IMAP Last Login Date
Thu01 Jan 1970 03:30:00
    WebMail Last Login Date
Fri16 Mar 2012 07:34:53
first3
@xyz.com
    Creation Date
Wed14 Aug 2009 18:19:41
    POP3 Last Login Date
Thu01 Jan 1970 03:30:00
    IMAP Last Login Date
Thu01 Jan 1970 03:30:00
    WebMail Last Login Date
Sat17 Mar 2012 09:52:26
first4
@xyz.com
    Creation Date
Tue17 Jan 2012 14:29:31
    POP3 Last Login Date
Thu01 Jan 1970 03:30:00
    IMAP Last Login Date
Thu01 Jan 1970 03:30:00
    WebMail Last Login Date
Wed07 Mar 2012 00:00:03
first5
@xyz.com
    Creation Date
Tue17 Jan 2012 18:09:03
    POP3 Last Login Date
Thu01 Jan 1970 03:30:00
    IMAP Last Login Date
Thu01 Jan 1970 03:30:00
    WebMail Last Login Date
Sat17 Mar 2012 10:05:03 
i need emails those have Creation Date on "Wed, 14 Aug 2009" and have same WebMail Last Login Date POP3 Last Login Date on Thu, 01 Jan 1970

or email those were created on Wed, 14 Aug 2009 with WebMail Last Login Date: Thu, 01 Jan 1970

thx in advance man

Last edited by mmhs; 03-17-2012 at 09:18 AM.
 
Old 03-17-2012, 08:07 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Nice code, druuna! The problem is that there is a trailing blank space after the date in the last line, so that the expression $2 == $5 is not true anymore.

@mmhs: as syg00 pointed out, you should show some effort and try to solve the issue by yourself. Post the code you've written so far and ask help when you're at a stumbling point. It is not clear if you have some experience in awk programming or you simply leech the suggested code and expect others modify it until you reach your result. That's not a good attitude.

Last edited by colucix; 03-17-2012 at 08:09 AM.
 
1 members found this post helpful.
Old 03-17-2012, 08:08 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Well......

You need to be precise when giving information we need to work with. The example you posted in your first post doesn't come close to what you posted in post #6

The solution I posted in post #2 will not work on the file posted in post #6
 
2 members found this post helpful.
Old 03-17-2012, 08:13 AM   #9
mmhs
Member
 
Registered: Oct 2010
Posts: 101

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by colucix View Post
Nice code, druuna! The problem is that there is a trailing blank space after the date in the last line, so that the expression $2 == $5 is not true anymore.

@mmhs: as syg00 pointed out, you should show some effort and try to solve the issue by yourself. Post the code you've written so far and ask help when you're at a stumbling point. It is not clear if you have some experience in awk programming or you simply leech the suggested code and expect others modify it until you reach your result. That's not a good attitude.
i tried many times i exctarct many informations like find the line number of those person have creation date but none of them give my last result i tried many times
 
Old 03-17-2012, 08:23 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by mmhs View Post
i tried many times i exctarct many informations like find the line number of those person have creation date but none of them give my last result i tried many times
I believe it. Therefore, my advice is to post what you've tried, so that we can understand your level of knowledge and help you to correct your mistakes. You'll learn more this way, than trying a new and maybe completely different solution, won't you?

Regarding the code suggested by druuna, you could try to modify it to match exactly your requirement, given that in your first example there is an extra blank space at the end of the last line that breaks the algorithm (as I already pointed out in post #7).
 
1 members found this post helpful.
Old 03-17-2012, 09:15 AM   #11
mmhs
Member
 
Registered: Oct 2010
Posts: 101

Original Poster
Rep: Reputation: 1
for first example i did that with druuna helpful command

Quote:
awk 'BEGIN { RS="" ; FS="\n" ; ORS="\n" ; OFS="" } {sub(/.*, /,"",$2) ; sub(/.*, /,"",$3) ;sub(/.*, /,"",$5) ; if ( $2 == "14 Aug 2009" && $3 == "Thu, 01 Jan 1970" && $5 == "Thu, 01 Jan 1970" ) { print $1 } }' new.txt
but for my main file it didnt work

my another problem is im not very familiar with awk i read this tutorial
http://www.grymoire.com/Unix/Awk.html
but i didn't understand how can i fix it

Last edited by mmhs; 03-17-2012 at 01:19 PM.
 
Old 03-17-2012, 12:42 PM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well the first issue will be that the solution for how to break up the file:
Code:
BEGIN { RS="" ; FS="\n" ; ORS="\n" ; OFS="" }
The above was provided to you based on the first set of data you sent but as that does not have the same format as the second set and possibly the 'main' file I am guessing it will not
work at all.

The following code:
Code:
sub(/.*, /,"",$3)
This is a regex saying to remove everything up until you encounter a comma and a space. If we assume that the previous step did actually work then it would mean the following:
Code:
POP3 Last Login Date: Thu, 01 Jan 1970 03:30:00

# now looks like
01 Jan 1970 03:30:00
As you can clearly see, this will never equal:
Code:
Thu, 01 Jan 1970
a) It has a comma in it
b) It does not have the time code

Hence ... it is not equal

Finally, you have no variable in awk called '$', so when you say '$$' it will throw an error. I believe you are trying to say a boolean 'and' which would be '&&'

To finish, you will need to provide correctly formatted data and word your requirement better as I was not able to follow the previous statement:
Quote:
i need emails those have Creation Date on "Wed, 14 Aug 2009" and have same WebMail Last Login Date POP3 Last Login Date on Thu, 01 Jan 1970

or email those were created on Wed, 14 Aug 2009 with WebMail Last Login Date: Thu, 01 Jan 1970
The only part I can follow here is that the creation date should contain "Wed, 14 Aug 2009".
 
1 members found this post helpful.
Old 03-17-2012, 01:14 PM   #13
mmhs
Member
 
Registered: Oct 2010
Posts: 101

Original Poster
Rep: Reputation: 1
thx man i solved with great help of one of my friend

solution is

Quote:
awk '/@/{m=$0;p=0}/Creation Date: Wed, 14 Aug 2009/{p++}/POP3 Last Login Date: Thu, 01 Jan 1970/{p++}/WebMail Last Login Date: Thu, 01 Jan 1970/{p++}p==3{print m}' file
it was interesting i want to learn awk fully thx for your help here is one of the best linux forum with a good group of expret moderators thx thx thx
 
Old 03-17-2012, 02:31 PM   #14
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Glad to read you have a solution that works, but....

The posted solution does not work when using the sets of sample data you posted in this thread. The reason: There is no record that meets all the criteria. The second set (post #6) misses an entry like this:
Code:
first6@xyz.com
    Creation Date: Wed, 14 Aug 2009 15:08:01
    POP3 Last Login Date: Thu, 01 Jan 1970 03:30:00
    IMAP Last Login Date: Thu, 01 Jan 1970 03:30:00
    WebMail Last Login Date: Thu, 01 Jan 1970 15:23:20
Posting this so others that stumble upon this thread won't get too confused
 
1 members found this post helpful.
Old 03-17-2012, 02:38 PM   #15
mmhs
Member
 
Registered: Oct 2010
Posts: 101

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by druuna View Post
Hi,

Glad to read you have a solution that works, but....

The posted solution does not work when using the sets of sample data you posted in this thread. The reason: There is no record that meets all the criteria. The second set (post #6) misses an entry like this:
Code:
first6@xyz.com
    Creation Date: Wed, 14 Aug 2009 15:08:01
    POP3 Last Login Date: Thu, 01 Jan 1970 03:30:00
    IMAP Last Login Date: Thu, 01 Jan 1970 03:30:00
    WebMail Last Login Date: Thu, 01 Jan 1970 15:23:20
Posting this so others that stumble upon this thread won't get too confused
thx thx thx druuna yes you right the sets of sample which i posted didn't have a record likeyour sample but i said in my first post it was just 5 records of 3000 records anyway thx for your great help man

Last edited by mmhs; 03-17-2012 at 02:39 PM.
 
  


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
Perl script to replace a line after finding a previous line Mark1986 Programming 1 02-28-2011 05:09 PM
Help scripting to find line and print previous line to out jamieofansa Programming 4 05-21-2010 01:30 PM
how to search and delete previous line as well mallesh1985 Programming 5 12-03-2009 03:41 PM
Attempting to append a line of text to the end of the previous line market_garden Linux - General 4 12-11-2008 11:37 AM
Next - Previous Page Buttons / Search this Thread Button JZL240I-U LQ Suggestions & Feedback 5 11-09-2005 07:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:51 PM.

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