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
# 8  
Old 01-03-2012
Quote:
Originally Posted by rangarasan
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
you dont have Tie::IxHash module in system. so that you got error.
Please try with the above code..
Tie::IxHash - check this module in CPAN for detailed explanation.

Cheers,
RangaSmilie
This User Gave Thanks to rangarasan For This Post:
# 9  
Old 01-03-2012
Hi Ranga,
The mArr array which i have has some NULL values also. How can they be removed?

---------- Post updated at 01:12 PM ---------- Previous update was at 01:05 PM ----------

Thanks Ranga, its done.
# 10  
Old 01-03-2012
Array

Hi,

You want to use splice funtion to remove the array elements( you can able to add too).

Code:
for(my $i=0;$i<scalar(@mArr);$i++)
{
   splice(@mArr,$i,0) if ($mArr[$i] =~ /^$/ );
}

splice - remove array element any where in the position.

Cheers,
RangaSmilie
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