Using Raku (formerly known as Perl_6)
raku -pe 's/ ^^ D <(\d*?)> \, /20211011/;'
As @StéphaneChazelas notes in a comment to your OP, it is unclear whether "future dates" will ever appear in your first column. If not, then a simple s///
replacement suffices, which is what the Raku code above accomplishes (substituting all date digits it finds regardless of numerical < = >
comparison).
If however, you desire to update the first column to a value based on a numerical < = >
comparison, you can use the Raku code below, which executes a block containing Raku's ternary operator in the replacement half of the s///
operator:
raku -pe 'my Int $d=20211011; s/ ^^ D (\d*?) \, /D{$0 < $d ?? $d !! $0},/;'
Sample Input:
D20211011,S0519,306668,1
D20211004,S1600,306668,1
D20211009,S1604,306668,1
D20211010,S1605,306668,1
D20211006,S1610,306668,1
D20211011,S1611,306668,1
Sample Output (for either Raku code example, above):
D20211011,S0519,306668,1
D20211011,S1600,306668,1
D20211011,S1604,306668,1
D20211011,S1605,306668,1
D20211011,S1610,306668,1
D20211011,S1611,306668,1
For the second Raku code example, note that the variable $d
is Type-constrained to Int
as a further check on correctness. And as @AdminBee notes you're lucky that the first column contains ISO dates which can be compared with < = >
operators and still give the correct result.
Regarding the (simple) second line of Raku code above, you should note that there's no check on the $0
capture to insure valid dates (e.g. ensuring no 13th month nor 32nd day appears). There isn't even code to exclude incomplete dates (e.g. month/day sans year).
OTOH, adding an approprate date-validation check should be relatively easy, using Raku's built-in support for Date
and DateTime
objects (no extra modules required; example and link below).
$ echo "2020-02-29" | raku -ne '.Date.raku.say'
Date.new(2020,2,29)
$ echo "2021-02-29" | raku -ne '.Date.raku.say'
Day out of range. Is: 29, should be in 1..28
in block <unit> at -e line 1
https://docs.raku.org/language/temporal#index-entry-Date_and_time_functions
https://raku.org
DYYYYMMDD
?