简体   繁体   中英

perl command line one liner to do find/replace, but *not* print lines that don't match?

I want to do a regex find & replace on a file on the command line with complicated regex, so I need PCRE's & am using perl -pe "s/foo/bar" (rather than sed ), that prints out all lines after applying the s/// , but it also prints lines that don't match.

Is there a perl command line one-liner which will not print lines that don't match? I know of perl -pe s/foo/bar/ if /foo/ , but then I need to duplicate the regex. Is it possible without repeating myself?

You can use -n flag (which, unlike -p , does not automatically print all lines) and print only lines which match the regex:

perl -ne 's/foo/bar/ && print'

Or, equivalently:

perl -ne 'print if s/foo/bar/'

The answer from @Dada shows the most concise method to solve the OP's exact problem.
Below are a few other equivalent, but longer, methods to do the same. Their advantage is that these one-liners can be more easily expanded to execute some other additional code (if needed) in addition to simply printing the lines.

perl -ne 'if ( s{foo}{bar} ) { print; }'
# Same:
perl -ne 'next LINE unless s{foo}{bar}; print;'

These can be expanded to:

perl -ne 'if ( s{foo}{bar} ) { some(); other(); code(); print; }'
# Same:
perl -ne 'next LINE unless s{foo}{bar}; some(); other(); code(); print;'

SEE ALSO:

perlrun: command line switches
What should every Perl hacker know about perl -ne?

To see these command line switches in a variety of use cases, including some great Perl one-liners, see:
What is your latest useful Perl one-liner (or a pipe involving Perl)?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM