Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

Specifying file to process to Perl one-liner

I was given a Perl one-liner. It has the following form:

perl -pe'...'

How do I specify the file to process to the program?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Documentation on how to launch perl is found in the perlrun man page.

perl -pe'...' -i~ file [file [...]]   # Modifies named file(s) in place with backup.
perl -pe'...' -i file [file [...]]    # Modifies named file(s) in place without backup.
perl -pe'...' file.in >file.out       # Reads from named file(s), outputs to STDOUT.
perl -pe'...' <file.in >file.out      # Reads from STDIN, outputs to STDOUT.

If the file's name could start with a -, you can use --.

perl -pe'...' [-i[~]] -- "$file" [...]

If you wanted to modify multiple files, you could use any of the following:

find ... -exec               perl -pe'...' -i~ {} +   # GNU find required
find ...         | xargs -r  perl -pe'...' -i~        # Doesn't support newlines in names
find ... -print0 | xargs -r0 perl -pe'...' -i~

In all of the above, square brackets ([]) denote something optional. They should not appear in the actual command. On the other hand, the {} in the -exec clause should appear as-is.


Note: Some one-liners use -n and explicit prints instead of -p. All of the above applies to these as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...