eran
eran

Reputation: 6921

How to replace whitespaces without the new line

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.

Upvotes: 2

Views: 99

Answers (1)

ysth
ysth

Reputation: 98388

[^\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/;

Upvotes: 3

Related Questions