LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   extract Previous line search (https://www.linuxquestions.org/questions/linux-newbie-8/extract-previous-line-search-934927/)

mmhs 03-17-2012 06:52 AM

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

druuna 03-17-2012 07:21 AM

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.

syg00 03-17-2012 07:23 AM

Perhaps you should try first, rather than just ask.

mmhs 03-17-2012 07:43 AM

Quote:

Originally Posted by druuna (Post 4629080)
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 :(

druuna 03-17-2012 07:50 AM

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

mmhs 03-17-2012 08:00 AM

Quote:

Originally Posted by druuna (Post 4629098)
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

colucix 03-17-2012 08:07 AM

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.

druuna 03-17-2012 08:08 AM

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

mmhs 03-17-2012 08:13 AM

Quote:

Originally Posted by colucix (Post 4629106)
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

colucix 03-17-2012 08:23 AM

Quote:

Originally Posted by mmhs (Post 4629110)
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).

mmhs 03-17-2012 09:15 AM

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 :(

grail 03-17-2012 12:42 PM

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".

mmhs 03-17-2012 01:14 PM

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

druuna 03-17-2012 02:31 PM

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 :)

mmhs 03-17-2012 02:38 PM

Quote:

Originally Posted by druuna (Post 4629287)
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 :)


All times are GMT -5. The time now is 08:03 AM.