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" ...
- 03-01-2011 #1Just 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
- 03-02-2011 #2Just Joined!
- Join Date
- Jul 2008
- Posts
- 81
Yes but what do you want to do after comparing those two fields and finding them equal. Or finding them not equal.
- 03-02-2011 #3Linux Newbie
- Join Date
- Nov 2008
- Location
- Tokyo, Japan
- Posts
- 243
Try a "perl" script.
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.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
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.But this is a bit less efficient.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 )Last edited by ramin.honary; 03-02-2011 at 02:32 AM.
- 03-02-2011 #4Linux Newbie
- Join Date
- Dec 2010
- Posts
- 146
Alternatively, as a hint you can use the cut command -
cut -d \ -f2,3
- 03-02-2011 #5Linux Newbie
- Join Date
- Nov 2008
- Location
- Tokyo, Japan
- Posts
- 243
- 03-02-2011 #6Just 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!
- 03-02-2011 #7
Use the FS variable in the initialisation section to change the separator: Field Separators - The GNU Awk User's Guide .
- 03-02-2011 #8Just Joined!
- Join Date
- Jan 2010
- Posts
- 27
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.


Reply With Quote
