3

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!

Anthony C
  • 1,990
  • 3
  • 23
  • 40

2 Answers2

3

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
$ 
codaddict
  • 445,704
  • 82
  • 492
  • 529
2

You can use grep as:

grep -o 'TXT[^.]*\.txt' file
codaddict
  • 445,704
  • 82
  • 492
  • 529