sykatch
sykatch

Reputation: 301

Choosing lines with GAWK

I have a file with data:

 /homes/XXXX/YYYYY/file1:20150324
 /homes/XXXX/YYYYY/file2:20150327
 /homes/XXXX/YYYYY/file3:20150320
 /homes/XXXX/YYYYY/file3:20150327
 /homes/XXXX/YYYYY/file4:20150328

First field is path, : as a separator and second field is a date. What I want to do is to select only those lines with date lower then some value.

gawk -v var="$SOMEVALUE" '{FS = ":"; if($2<=var) print;}'

This is what I have. It works fine, prints lines with lower value than I want to, but it everytime prints also the first line, no matter the value. Could you give me some advice what to do? thanks

Upvotes: 0

Views: 44

Answers (2)

Bernie Reiter
Bernie Reiter

Reputation: 116

@JuniorCompressor I didn't realise that. Will save me trouble in future scripts. Thanks :-)

@sykatch You could also statically define the field separator within your gawk script like:

    gawk -v var="20150323" 'BEGIN {FS = ":"}; {if($2<=var) print;}' datefile.txt

but the solution suggested by JuniorCompressor is more flexible.

Upvotes: 0

JuniorCompressor
JuniorCompressor

Reputation: 20025

What about:

gawk -F: -v var="$SOMEVALUE" 'var>=$2'

The error is that you had to change the field separator after you read the first row.

Upvotes: 1

Related Questions