I have strings:
fvvDataFolders/DDB/DDB2018-02-21oM]
fbbDataFolders/DDB/DDB2018-02-22oM]
I want to strip everything that starts with Data and ends in what looks like a date:
DataFolders/DDB/DDB2018-02-21
DataFolders/DDC/DDB2018-02-22
How I can do it?
You can use the command
grep
in this way:-o
,--only-matching
- show only the part of a line matching PATTERN.-P
,--perl-regexp
- PATTERN is a Perl regular expression; or in this case could be used also the option-E
,--extended-regexp
- PATTERN is an extended regular expression (ERE).'Data.*[0-9]{4}-[0-9]{2}-[0-9]{2}'
matches to your requirements. It begin with the stringData
, followed by unknown number*
of any characters.
, and ends with the date format:4 digits from 0 to 9
dash2 digits from 0 to 9
dash2 digits from 0 to 9
.Here is also a
sed
solution:> output-file
or use the option-i.bak
to make the changes in their places and create a backup file.-r
,--regexp-extended
- use extended regular expressions in the script.s
means substitute:/<string-or-regexp>/<replacement>/
.^.*
will match to the beginning^
of the line, followed by unknown number of any characters..*$
will match to the end$
of the line, precede by unknown number of any characters.(...)
, will be treated as the variable \1. So the whole line^.*$
will be substituted by the part that matces to what is in the brackets.Either
or
will do. They both print the minimal string that starts with
Data
and ends in what looks like a date (YYYY-MM-DD).