0

My question sounds trivial, but I google many page still can't not find an answer.

I am on Windows. I have a text file. If I open it with Notepad++, it looks like this

enter image description here

I want to try several things

  1. delete all carriage return and line feed

    perl -i.bak -pe "s/\r\n//g" a.txt

surprisingly, there is nothing changed. What is wrong? But according to the doc, I am pretty sure \r is CR and \n is LF

  1. What I actually want to do is match across line. for example ^function.*\r\n! will match just like Notepad++ will does

enter image description here

If we want to indent the ! line if its previous line is started with "function", a naive thought would be (actually it works is notepad++)

perl -i.bak -pe "s/^(function.*\r\n)!/$1\t!/g" a.txt

But it didn't work. How to do it correctly?

9
  • please try this: perl -i.bak -pe "s/^(.*\r\n)!/$1\t!/sg" a.txt or else perl -i.bak -pe "s/\s*$//sg" a.txt Commented Jul 25, 2016 at 7:18
  • stackoverflow.com/questions/3166976/… Commented Jul 25, 2016 at 7:21
  • @ssr1012, That's of no use. It's about merging all lines into one, and it's for a different OS. Commented Jul 25, 2016 at 7:22
  • @ikegami It is just an example to demonstrate feature I want . Anyway, merging lines is very useful : ) Commented Jul 25, 2016 at 7:25
  • 1
    Try perl -i.bak -pe "s/\\R+//g" a.txt Commented Jul 25, 2016 at 7:30

2 Answers 2

3

By default, on Windows, CR+LF gets transformed to LF on read, and LF gets transformed to CR+LF on write. This makes lines look like they're LF-terminated regardless of the OS.


If sounds like you want to add a leading tab to lines starting with !.

perl -i.bak -pe"s/^!/\t!/" a.txt
   -or-
perl -i.bak -pe"s/^(?=!)/\t/" a.txt

You might also be trying to avoid doing it on the first line.

perl -i.bak -pe"s/^!/\t!/ if $. > 1" a.txt
   -or-
perl -i.bak -pe"s/^(?=!)/\t/ if $. > 1" a.txt
Sign up to request clarification or add additional context in comments.

2 Comments

ikegami, I am very sorry for my post which is not clear enough and may already wasted you a lot of time. Well, as I said in the post, I insisted to do it across line for a reason. What I actually want to do is indent the ! line according to the previous line, for example only indent ! line when its previous line is start with "function". My post actually has the same goal as my previous post "stackoverflow.com/questions/38452288/…". If you have any better idea using perl, feel free to add an answer, just neglect notepad++ tag.
The current solution doesn't do it just for the lines with a '!' that have a 'function' on the previous line.
2
  1. perl -i.bak -pe "s/\n//" a.txt

I.e. just change \r\n to \n for the \r\n is automatically converted to \n on Windows as it was explained by ikegami.


  1. perl -i.bak -0777 -pe "s/^(function.*?\n)!/\1\t!/gm" a.txt

The main point here is that you need to read the entire file contents into a single string in order to do cross-line matches. -0777 parameter instructs Perl to do so (alternatively you may set $/ to an empty string from within Perl script).

11 Comments

Hi, Dmitry Egorov. Currently, your answer works. And m seems not needed here. However, why things got so complicated, I just switch to perl because sed is so awkward in regex, while perl is also awkward in something. : )
@user15964, yes, you're right. Corrected the answer.
I would also like to thank @LukStorms for the simpler regex
-0777 can be used instead of BEGIN{undef $/;}
@ikegami Hi, ikegami. You deleted your answer? That is a good reference too.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.