2

I have a text file where each line may end with some fixed TAG surrounded by white spaces, e.g

some text TAG

I'm writing something like:

while (<FILE>) {
  s/\s*TAG\s*$//;
  print;
}

The problem is that this remove the new line from the end, is there anyway to tell Perl not to replace the new line? The only thing I thought of is to write

s/\s*TAG[ \t]*$//;

Is there better way?

[Not sure if this is relevant, but OS is Linux]

thanks.

2
  • It seems that it would be easier just to put the newline back. Jun 17, 2012 at 9:12
  • If this will be in a one-liner, adding the -l option will solve your problem.
    – TLP
    Jun 17, 2012 at 11:30

1 Answer 1

3

[^\S\n] will match whitespace that's not a newline, so:

s/\s*TAG[^\S\n]*$//;

Or you could just do:

s/\s*TAG\s*$/\n/;

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.