Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for The Same Pattern Multiple Times in One Line

Tags:

regex

sed

The pattern I'm looking for is this:

TXT.*\.txt

That pattern can occur multiple times in any given line. I would like to either extract each instance of the pattern out or alternatively delete the text that surrounds each instance using sed (or anything, really).

Thanks!

like image 657
Anthony C Avatar asked Nov 29 '25 08:11

Anthony C


1 Answers

You can use Perl as:

$ cat file
foo TXT1.txt bar TXT2.txt baz
foo TXT3.txt bar TXT4.txt baz

$ perl -ne 'print "$1\n" while(/(TXT.*?\.txt)/g)' file
TXT1.txt
TXT2.txt
TXT3.txt
TXT4.txt
$ 
like image 138
codaddict Avatar answered Nov 30 '25 23:11

codaddict



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!