Sunday, May 19, 2024
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 304  / 2 Years ago, thu, january 27, 2022, 12:55:58

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?


More From » command-line

 Answers
1

Either



grep -P -o 'Data.+?dddd-dd-dd'


or



perl -pe 's/^.+(Data.+?dddd-dd-dd).+$/$1/'


will do. They both print the minimal string that starts with Data and ends in what looks like a date (YYYY-MM-DD).



echo "fvvDataFolders/DDB/DDB2018-02-21oM]" > input.txt
echo "fbbDataFolders/DDB/DDB2018-02-22oM]" >> input.txt
grep -P -o 'Data.+?dddd-dd-dd' input.txt

# output:
DataFolders/DDB/DDB2018-02-21
DataFolders/DDB/DDB2018-02-22

perl -pe 's/^.+(Data.+?dddd-dd-dd).+$/$1/' input.txt

# output:
DataFolders/DDB/DDB2018-02-21
DataFolders/DDB/DDB2018-02-22

[#8976] Saturday, January 29, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
variark

Total Points: 82
Total Questions: 114
Total Answers: 122

Location: Sweden
Member since Mon, May 8, 2023
1 Year ago
;