Find the answer to your Linux question:
Results 1 to 8 of 8
Dear Pros, I'm a newbie in scripting. Actually i need help to build a script which can monitor the "DateTimeHourMinute" in a data file. I wanted to compare the "DateTimeHourMinute" ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2011
    Posts
    1

    Need Script to compare 2 columns


    Dear Pros,
    I'm a newbie in scripting. Actually i need help to build a script which can monitor the "DateTimeHourMinute" in a data file.
    I wanted to compare the "DateTimeHourMinute" from the 2nd column against "DateTimeHourMinute" on the 3rd column which located in the same line.

    Below is the data sample :-

    thanks in advance.

    02 14320110104113306255MYFCPOF01L3700326 20110104113306C0000000000193
    02 14320110104113320974MYFCPOF01L3700326 20110104113320C0000000000193
    02 14320110104113331617MYFCPOF01L3700326 20110104113331C0000000000193
    02 14320110104113346171MYFCPOF01L3700326 20110104113346C0000000000193
    02 14320110104113946489MYFCPOF01L3700326 20110104113946C0000000000193
    02 14320110104114050245MYFCPOF01L3700326 20110104114050C0000000000221
    02 14320110104114108914MYFCPOF01L3700326 20110104114108C0000000000221

  2. #2
    Just Joined!
    Join Date
    Jul 2008
    Posts
    93
    Yes but what do you want to do after comparing those two fields and finding them equal. Or finding them not equal.

  3. #3
    Linux User
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    260
    Try a "perl" script.
    Code:
    #!/usr/bin/perl
    
    while ( $x = <> ) {
      $x =~ /^......(............)\S*\s(............)$/ ;
      if ( $1 eq $2 ) {
         #do something if the two dates are equal
      }
      else {
        #do something if the dates are not equal
      }
    }
    
    #now in the command line execute:
    # ./this-perl-script.pl  input-file.data
    I'm a bit rusty with perl, but I think that's how it would work. I haven't tested it though. Notice the line "$x =~ /...." -- this is a regular expression match. The dots will match exactly one character, so I assume that every line in the file has exactly the same number of characters. In perl, when you match regular expressions, the parenthesis denotes where to keep a portion of a matched string and store each match in the variables $1, $2, $3, ... and so on. So in this case, since there are two sets of parenthesis in the regular expression, it will match the two sections of the input.

    You need to test your regular expression to make sure it matches the data you want to match, otherwise it won't work.

    If you can't use perl, you can achieve the same effect with "sed", and piping the output of "sed" to a bash script.
    Code:
    sed -e 's/^......\(............\)[^[:space:]]*[[:space:]]\(............\).*$/\1 \2/' input-file.data | ( \
    while read x y
    do if [ "$x" == "$y" ]
          then echo "match" $x #here is the code to execute on matching
          else echo "no match" #here is the code to execute on no matching
          fi
    done
    )
    But this is a bit less efficient.
    Last edited by ramin.honary; 03-02-2011 at 02:32 AM.

  4. #4
    Linux Newbie
    Join Date
    Dec 2010
    Posts
    146
    Alternatively, as a hint you can use the cut command -

    cut -d \ -f2,3

  5. #5
    Linux User
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    260
    Quote Originally Posted by dE_logics View Post
    Alternatively, as a hint you can use the cut command -

    cut -d \ -f2,3
    "awk" does exactly the same thing as "cut":
    Code:
    awk '{ print $2 " " $3 }' input-file.data
    The problem is, it only separates out columns on spaces or tabs. "Sed" and "perl" gives you more control with regular expressions. You can scissor out the date string exactly.

  6. #6
    Just Joined!
    Join Date
    Oct 2010
    Posts
    6

    extension on awk

    I like awk and have extended the idea a bit:
    1. save your input in a file 'datefile'
    2. execute:
    awk '{if (substr($2,4,12)==substr($3,0,12)){print "equal ", $1 $2 $3} }' datefile

    I think it is obvious to edit further to your needs. Good luck and have fun!

  7. #7
    Linux Newbie unlimitedscolobb's Avatar
    Join Date
    Jan 2008
    Posts
    120
    Quote Originally Posted by ramin.honary View Post
    The problem is, it only separates out columns on spaces or tabs.
    Use the FS variable in the initialisation section to change the separator: Field Separators - The GNU Awk User's Guide .

  8. #8
    Just Joined!
    Join Date
    Jan 2010
    Posts
    40
    Quote Originally Posted by unlimitedscolobb View Post
    Use the FS variable in the initialisation section to change the separator: [...]
    Yes, FS can be even a regular expression in Awk:
    Code:
       Fields
           As each input record is read, gawk splits  the  record
           into fields, using the value of the FS variable as the
           field separator.  If FS is a single character,  fields
           are  separated  by  that character.  If FS is the null
           string, then each individual character becomes a sepa‐
           rate  field.   Otherwise,  FS is expected to be a full
           regular expression.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •