Mimic ack --output on steroid
Closed this issue · 2 comments
Another idea here and sorry if It's already possible with rak but what I love about ack if the fact that you can create a custom output with pattern captured by regex.
For example here you can reformat the datetime format from ISO to European :
ack '(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})' --ouput='$3/$2/$1 $4:$5:$6' /path/file
Even more cool would be to combine this with --unique and --stats to get in return all unique value of a matched pattern with the number of hits for each different pattern. There is probably a lot of this pre-baked in Rak but haven't figure it out
In that regex, you're leaving the time part untouched, right? In that case:
$ rak '{ .subst(/ (\d\d\d\d) "-" (\d\d) "-" (\d\d) )> \s \d\d ":" \d\d ":" \d\d /, { "$2-$1-$0" } ) }'
I prefer to use the \d\d\d\d
way of specifying in these cases, as it is actually shorter for the 2 char case, and it is visually more clear what happens.
The .subst
method returns a copy of the string with the possible substitution, or it returns the invocant. With multiple dates on the same line, maybe a :g
would need to be added.
There is no need for special formatters: if the needle returns True
, the line is selected. If it returns False
, Nil
or Empty
, the line is not selected. If the needle returns anything else, then that will be used instead of the line.
In the case of .subst not matching, that will be the original, so effectively the same as the True
case.
If you want to make the change permanent in the files, then add --modify-files
, possibly with a --dryrun
and a --verbose
first if you're not 100% sure of what it will do.
I think this issue can be closed, please do so if you agree :-)