Can a command line utility save sub-strings conditionally in different files? I have a file (file.txt
) with several lines like the following.
1/1_ABCD4.txt:20020711
1/1_ABCD10.txt:20020731
2/2_ABCD2.txt:20071103
2/2_ABCD5.txt:20071107
3/3_ABCD1.txt:20090225
3/3_ABCD3.txt:20090230
My goal is to save 20020711
together with 20020731
in file 1
, 20071103
with 20071107
in file 2
, and 20090225
with 20090230
in file 3
?
I could extract the desired sub-strings after :
with the following command, but would lose the reference digit by doing so:
$ grep -oP 'txt\:\K[A-Z0-9-]+' 'path/to/file.txt'
20020711
20020731
20071103
20071107
20090225
20090230
Is it possible to build three separate files with the first digit before /
as target reference while using command line? The destination might be the same directory like the original text file.
- File:
20020711
20020731
- File:
20071103
20071107
- File:
20090225
20090230
Thank you.