Perl oneliner match repeating itself
I'm trying to read a specific section of a line out of a file with Perl. The file in question is of the following syntax.
# Sets $USER1$ $USER1$=/usr/.... # Sets $USER2$ #$USER2$=/usr/...
My oneliner is simple,
perl -ne 'm/^\$USER1\$\s*=\s*(\S*?)\s*$/m; print "$1";' /my/file
For some reason I'm getting the extraction for $1 repeated several times over, apparently once for every line in the file after my match occurs. What am I missing here?
You are executing print for every line of the file because print gets called for every line, whether the regex matches or not. Replace the first ; with an &&.