Perl regex to act on a file from the command line

后端 未结 3 1621
一生所求
一生所求 2020-12-18 03:52

In a file, say xyz.txt i want to replace the pattern of any number followed by a dot example:1.,2.,10.,11. etc.. with a whitespace. How to compose a perl command on the comm

相关标签:
3条回答
  • 2020-12-18 04:37

    This HAS to be a Perl oneliner?

    perl -i -pe  's/\d+\./ /g' <fileName>
    

    The Perl command line options: -i is used to specify what happens to the input file. If you don't give it a file extension, the original file is lost and is replaced by the Perl munged output. For example, if I had this:

    perl -i.bak -pe  's/\d+\./ /g' <fileName>
    

    The original file would be stored with a .bak suffix and <fileName> itself would contain your output.

    The -p means to enclose your Perl program in a print loop that looks SOMEWHAT like this:

    while ($_ = <>) {
        <Your Perl one liner>
        print "$_";
    }
    

    This is a somewhat simplified explanation what's going on. You can see the actual perl loop by doing a perldoc perlrun from the command line. The main idea is that it allows you to act on each line of a file just like sed or awk.

    The -e simply contains your Perl command.

    You can also do file redirection too:

    perl -pe  's/\d+\./ /g' < xyz.txt > xyz.txt.out
    
    0 讨论(0)
  • 2020-12-18 04:46

    Both

    perl -ipe "s/\d+\./ /g" xyz.txt
    

    and

    perl -pie
    

    cannot execute on my system.

    I use the following order:

    perl -i -pe
    
    0 讨论(0)
  • 2020-12-18 04:49

    Answer (not tested):

    perl -ipe "s/\d+\./ /g" xyz.txt
    
    0 讨论(0)
提交回复
热议问题