using awk in perl with split command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using awk in perl with split command
# 1  
Old 01-03-2012
using awk in perl with split command

Hi,
I have an array with following data. First field shows the owner and second is unique name. Now i have to pic the latest value with respect to the date in case of duplicate.
like "def" is from two owners "rahul/vineet", now i want the latest from the two and the owner name also for all the fields

ARRAY:
rahul_abc_Linux_Nov_02
rahul_def_Linux_Nov_02
vineet_cde_Linux_Dec_05
vineet_def_Linux_Dec_01
vineet_kbc_Linux_Dec_06

expected result
ARRAY:
rahul_abc_Linux_Nov_02
vineet_cde_Linux_Dec_05
vineet_def_Linux_Dec_01
vineet_kbc_Linux_Dec_06
# 2  
Old 01-03-2012
Hi,

Try this below code,,

Code:
#! /usr/local/bin/perl
use strict;
use Tie::IxHash;
my @mArr = qw/rahul_abc_Linux_Nov_02 rahul_def_Linux_Nov_02 vineet_cde_Linux_Dec_05 vineet_def_Linux_Dec_01 vineet_kbc_Linux_Dec_
06/;
my %mMonth=("Jan"=>"1","Feb"=>"2","Mar"=>"3","Apr"=>"4","May"=>"5","Jun"=>"6","Jul"=>"7","Aug"=>"8","Sep"=>"9","Oct"=>"10","Nov"=
>"11","Dec"=>"12");
my %mHash = ();
tie(%mHash,"Tie::IxHash");
foreach my $mEle ( @mArr )
{
   my @tArr = split(/\_/,$mEle);
   my $mKey1 = $tArr[1];
   if ( defined $mHash{$mKey1} )
   {
      my $val = $mHash{$mKey1};
      my @Arr = split(/\_/,$val);
      my $mExistMonth = $Arr[3];
      my $mNewMonth = $tArr[3];
      if ( $mMonth{$mExistMonth} < $mMonth{$mNewMonth} )
      {
         $mHash{$mKey1}="$mEle";
      }
      elsif ( ( $mMonth{$mExistMonth} == $mMonth{$mNewMonth} ) && $tArr[4] > $Arr[4] )
      {
         $mHash{$mKey1}="$mEle";
      }
   }
   else
   {
      $mHash{$mKey1}="$mEle";
   }
}
print "The Output is\n################\n\n";

foreach my $mKey1 ( keys %mHash )
{
   print "$mHash{$mKey1}\n";
}


Cheers,
RangaSmilie
# 3  
Old 01-03-2012
I think i am using an older version. it gives this error. any other way?
"Can't locate Tie/IxHash.pm "
# 4  
Old 01-03-2012
Another way:
Code:
# awk -F\_ '{a[$2""$3]=b++" "$0}END{for (i in a) {print a[i]}}' infile|sort|awk '{print $2}'
rahul_abc_Linux_Nov_02
vineet_cde_Linux_Dec_05
vineet_def_Linux_Dec_01
vineet_kbc_Linux_Dec_06

# 5  
Old 01-03-2012
hi Klashxx, I want it in perl.
# 6  
Old 01-03-2012
Your array is oderer by date ?
# 7  
Old 01-03-2012
No, we have to extract the date also from the string.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk command to split pipe delimited file

Hello, I need to split a pipe de-limited file based on the COLUMN 7 value . If the column value changes I need to split the file Source File Payment|ID|DATE|TIME|CONTROLNUMBER|NUMBER|NAME|INDICATOR 42156974|1137937|10/1/2018|104440|4232|2054391|CARE|1... (9 Replies)
Discussion started by: rosebud123
9 Replies

2. Shell Programming and Scripting

awk split and awk calculation in the same command

I am trying to run the awk below. My question is when I split the input, then run anotherawk to perform a calculation using that splitas the input there are no issues. When I try to combine them the output is not correct, is the split not working or did I do it wrong? Thank you :). input ... (8 Replies)
Discussion started by: cmccabe
8 Replies

3. Shell Programming and Scripting

awk split command to get the desired result

Dear all, I am using the awk 'split' command to get the particular value. FILE=InputFile_009_0.txt Temp=$(echo $FILE | awk '{split($FILE, a, "e_"); print a}') I would like to have the Temp take the value as : _009_0 ... (4 Replies)
Discussion started by: emily
4 Replies

4. Shell Programming and Scripting

Want to split awk command

Hi, There is an awk command in script and it is running successfully. I want to split that command in 2 lines. I have tried using '\' but its not working.. Please suggest me the solution. (11 Replies)
Discussion started by: Sanket Dalvi
11 Replies

5. Shell Programming and Scripting

Split a file using awk command.

awk 'FNR == 1 { c = 1 } { print > (f c) } !FNR%n { close(f c); ++c }' n=$files_per_stream f=$input_path/filename_ $input_file $input_file with some records are splitted into files named filename_1,filename_2...etc according to $files_per_stream. Plz help me know how and if anyone has... (7 Replies)
Discussion started by: guptam
7 Replies

6. Shell Programming and Scripting

Awk command to split file name

Hi I have few files with format access.2Nov-12:15AM. These files will be generated daily . I need to write a script so that if today's date is less than 10 then it has to zip the file and rename it to acess.02Nov-12:15AM.gz .please help me in this . Also please help me in splitting the file... (10 Replies)
Discussion started by: mskalyani9
10 Replies

7. Shell Programming and Scripting

Split a file based on pattern in awk, grep, sed or perl

Hi All, Can someone please help me write a script for the following requirement in awk, grep, sed or perl. Buuuu xxx bbb Kmmmm rrr ssss uuuu Kwwww zzzz ccc Roooowwww eeee Bxxxx jjjj dddd Kuuuu eeeee nnnn Rpppp cccc vvvv cccc Rhhhhhhyyyy tttt Lhhhh rrrrrssssss Bffff mmmm iiiii Ktttt... (5 Replies)
Discussion started by: kumarn
5 Replies

8. UNIX for Advanced & Expert Users

Split Command in Perl

Hi, I have to split a line of the form 1232423#asdf#124324#54534#dcfg#wert#rrftt#4567 into an array in perl. I am using @fields; @fields=split('#',$line); if($fields eq "1") But this is not working. By using the syntax, the statements in "if" are never executed. Please help.... (9 Replies)
Discussion started by: rochitsharma
9 Replies

9. Shell Programming and Scripting

Perl Split Command usage

Hi, I am trying to use the split commad to seperate string reading from file. but it dosent give me a correct result. can some body tell me what is the wrong in following scritp. #!/usr/bin/perl -w #use CGI qw(:standard); ... (2 Replies)
Discussion started by: maheshsri
2 Replies

10. Shell Programming and Scripting

awk command to split in to 2 files

Hi, I have a problem in grepping a file for 2 strings and writing them to 2 appropriate files. I need to use the awk command and read the file only once and write to the appropriate file. My file is very huge in size and it is taking a long time using cat command and grep command. Can anyone... (3 Replies)
Discussion started by: m_subra_mani
3 Replies
Login or Register to Ask a Question