Regular expression for one pattern several times in one line

The sample I'm looking for is:

TXT.*\.txt

This pattern can occur several times on any given line. I would like to either extract each instance of the template, or, conversely, remove the text that surrounds each instance using sed (or something, really).

Thank!

+3
source share
2 answers

You can use grep like:

grep -o 'TXT[^.]*\.txt' file
+2
source

You can use Perl like:

$ 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
$ 
+3
source

Source: https://habr.com/ru/post/1778216/


All Articles