user1139359
user1139359

Reputation: 169

perl regex command line - how to get matches instead replacing

I am new with perl. And I would like to get the matches, instead replacing the text.

I there anyway to accomplish that using one line command?

Sample text I am using: text.txt

sample1 other sample2 other

Command I am using:

perl -0777 -pe 's/sample\d/changed/gs' text.txt

I would like to have the following output (using one line command):

sample1
sample2

I am using strawberry perl in Windows PowerShell.

Upvotes: 1

Views: 999

Answers (1)

Andrey
Andrey

Reputation: 1818

This should work:

perl -ne "while(/(sample\d)/g){print \"$1\n\";}" text.txt

This looks better I think:

perl -lne "while(/(sample\d)/g){print $1;}" text.txt

Upvotes: 1

Related Questions