operation between two files (awk or sed )

operation between two files (awk or sed )

Post by M.H » Fri, 06 Feb 2004 04:58:59



Hi,

I have two files, say  file_A  and file_B

# cat file_A
[30/Jan/2004:17:43:11 user1
[30/Jan/2004:17:58:37 user2
...
..

# cat file_B
1.1.1.1 - - [30/Jan/2004:17:43:11 "dom.com/logon_main.jsp"
1.1.1.1 - - [30/Jan/2004:17:43:11 "dom.com/logon.do"
6.6.6.6 - - [30/Jan/2004:17:43:11 "dom.com/something1.jsp"
2.2.2.2 - - [30/Jan/2004:17:50:37 "dom.com/something1.jsp"
3.3.3.3 - - [30/Jan/2004:17:51:41 "dom.com/path/something2.jsp"
4.4.4.4 - - [30/Jan/2004:17:52:41 "dom.com/path/something2.jsp"
5.5.5.5 - - [30/Jan/2004:17:58:37 "dom.com/"logon_main.jsp"
5.5.5.5 - - [30/Jan/2004:17:58:37 "dom.com/logon.do"
...
..

I want to have an output from the Two precedent files like this :

1.1.1.1 - user1 [30/Jan/2004:17:43:11 "dom.com/logon_main.jsp"
1.1.1.1 - user1 [30/Jan/2004:17:43:11 "dom.com/logon.do"
6.6.6.6 - - [30/Jan/2004:17:43:11 "dom.com/something1.jsp"
2.2.2.2 - - [30/Jan/2004:17:50:37 "dom.com/something1.jsp"
3.3.3.3 - - [30/Jan/2004:17:51:41 "dom.com/path/something2.jsp"
4.4.4.4 - - [30/Jan/2004:17:52:41 "dom.com/path/something2.jsp"
5.5.5.5 - user2 [30/Jan/2004:17:58:37 "dom.com/"logon_main.jsp"
5.5.5.5 - user2 [30/Jan/2004:17:58:37 "dom.com/logon.do"
...
..

In other words, what I want is whenever the dates and times in the two
files (file_A and file_B) are the same (1st field in file_A is equal to
the 4th field in file_B) AND that the 5th field in file_B contains
logon_main.jsp OR logon.do, I have to REPLACE the 3rd field in the
file_B (-) with the 2nd field in the file_A (the user) .

How to acheive this ?

Please help.

Best Regards.

haed98

 
 
 

operation between two files (awk or sed )

Post by Darren Dunha » Fri, 06 Feb 2004 06:17:13



> Hi,
> [...] what I want is whenever the dates and times in the two
> files (file_A and file_B) are the same (1st field in file_A is equal to
> the 4th field in file_B) AND that the 5th field in file_B contains
> logon_main.jsp OR logon.do, I have to REPLACE the 3rd field in the
> file_B (-) with the 2nd field in the file_A (the user) .
> How to acheive this ?

Here's an attempt.  There's a lot more you'd want to add to it (error
checking, passing in the filenames on the command line, dealing with
blank lines or bad format lines...)

#!/usr/bin/perl -w
use strict;

my $user_file = "filea";
my $log_file = "fileb";
my %user_by_date;

open (my $user, $user_file) or die;
while (<$user>)
{
  my ($date, $user) = split;
  $user_by_date{$date} = $user;

Quote:}

open (my $log, $log_file) or die;
while (<$log>)
{

  if (exists $user_by_date{$info[3]} and
      $info[4] =~ /logon_main\.jsp|logon\.do/)
  { $info[2] = $user_by_date{$info[3]}; }

Quote:}

--

Unix System Administrator                    Taos - The SysAdmin Company
Got some Dr Pepper?                           San Francisco, CA bay area
         < This line left intentionally blank to confuse you. >

 
 
 

operation between two files (awk or sed )

Post by M.H » Sat, 07 Feb 2004 19:11:11


Hi Darren

Thanks a lot.

haed98



>>Hi,

>>[...] what I want is whenever the dates and times in the two
>>files (file_A and file_B) are the same (1st field in file_A is equal to
>>the 4th field in file_B) AND that the 5th field in file_B contains
>>logon_main.jsp OR logon.do, I have to REPLACE the 3rd field in the
>>file_B (-) with the 2nd field in the file_A (the user) .

>>How to acheive this ?

> Here's an attempt.  There's a lot more you'd want to add to it (error
> checking, passing in the filenames on the command line, dealing with
> blank lines or bad format lines...)

> #!/usr/bin/perl -w
> use strict;

> my $user_file = "filea";
> my $log_file = "fileb";
> my %user_by_date;

> open (my $user, $user_file) or die;
> while (<$user>)
> {
>   my ($date, $user) = split;
>   $user_by_date{$date} = $user;
> }

> open (my $log, $log_file) or die;
> while (<$log>)
> {

>   if (exists $user_by_date{$info[3]} and
>       $info[4] =~ /logon_main\.jsp|logon\.do/)
>   { $info[2] = $user_by_date{$info[3]}; }

> }

 
 
 

1. operation between two files (awk or sed )

Hi,

I have two files, say  file_A  and file_B

# cat file_A
[30/Jan/2004:17:43:11 user1
[30/Jan/2004:17:58:37 user2
...
..

# cat file_B
1.1.1.1 - - [30/Jan/2004:17:43:11 "dom.com/logon_main.jsp"
1.1.1.1 - - [30/Jan/2004:17:43:11 "dom.com/logon.do"
6.6.6.6 - - [30/Jan/2004:17:43:11 "dom.com/something1.jsp"
2.2.2.2 - - [30/Jan/2004:17:50:37 "dom.com/something1.jsp"
3.3.3.3 - - [30/Jan/2004:17:51:41 "dom.com/path/something2.jsp"
4.4.4.4 - - [30/Jan/2004:17:52:41 "dom.com/path/something2.jsp"
5.5.5.5 - - [30/Jan/2004:17:58:37 "dom.com/"logon_main.jsp"
5.5.5.5 - - [30/Jan/2004:17:58:37 "dom.com/logon.do"
...
..

I want to have an output from the Two precedent files like this :

1.1.1.1 - user1 [30/Jan/2004:17:43:11 "dom.com/logon_main.jsp"
1.1.1.1 - user1 [30/Jan/2004:17:43:11 "dom.com/logon.do"
6.6.6.6 - - [30/Jan/2004:17:43:11 "dom.com/something1.jsp"
2.2.2.2 - - [30/Jan/2004:17:50:37 "dom.com/something1.jsp"
3.3.3.3 - - [30/Jan/2004:17:51:41 "dom.com/path/something2.jsp"
4.4.4.4 - - [30/Jan/2004:17:52:41 "dom.com/path/something2.jsp"
5.5.5.5 - user2 [30/Jan/2004:17:58:37 "dom.com/"logon_main.jsp"
5.5.5.5 - user2 [30/Jan/2004:17:58:37 "dom.com/logon.do"
...
..

In other words, what I want is whenever the dates and times in the two
files (file_A and file_B) are the same (1st field in file_A is equal to
the 4th field in file_B) AND that the 5th field in file_B contains
logon_main.jsp OR logon.do, I have to REPLACE the 3rd field in the
file_B (-) with the 2nd field in the file_A (the user) .

How to acheive this ?

Please help.

Best Regards.

haed98

2. Newbie question about 1GB drive

3. help: simple sed operation on multiple files

4. HP plotter to IBM Xstation

5. sed/awk - generating Makefiles (Re: awk processing of Makefile macros)

6. Redhat 6.2 TFTP

7. sed/awk : need just the first column in a multi-column file

8. Looking for Internet C/S specialist

9. sed or awk - end of file

10. Grep/awk/sed for multiple/groups of lines in a file???

11. grep, awk, or sed for an unwieldy file

12. Using awk And/Or sed To Edit Fourth Field Of File