Perl Regex: Cut and Paste

后端 未结 1 648
失恋的感觉
失恋的感觉 2021-01-25 00:33

I\'m attempting to use perl to edit a file, essentially to cut and paste content within the file. However, it appears I\'ve incorrectly constructed the regex. I\'d appreciate he

相关标签:
1条回答
  • 2021-01-25 00:58

    To match across multiple lines you need to have the whole file in a scalar

    perl -0777 -pe's/(export PATH.*?\n)(.*?)(?=\n?# THIS IS LAST)/$2$1/s' input.txt
    

    where -0777 enables "slurp" mode, and (?=...) is a positive lookahead.

    The \n? is in the lookahead to include the optional empty line that shouldn't be consumed, since that line shouldn't be moved. Then the previous pattern need be non-greedy, too. This protects a single preceding empty line, any others are matched and swapped.

    0 讨论(0)
提交回复
热议问题