david
david

Reputation: 13

how to use sed regular expression to replace a line

I want to replace a log command in a file and output to another file

my log command can be like

log(xxx);
log ( xxx);

so I use the following

cat input.txt | sed -e '/\s*log\s*\(.*\)/d' > output.txt

however, it also replace the the line with "logical".

What should I change to make it work.

Thanks

Upvotes: 1

Views: 274

Answers (3)

potong
potong

Reputation: 58371

This "belt-n-braces" solution may work:

# sed '/\<log\>\s*([^)]*)/d' <<!
> a
> b
> logical(123)
> log(123)
> log ( 123)
> d
> e
> !
a
b
logical(123)
d
e

Upvotes: 1

tripleee
tripleee

Reputation: 189317

If you just want ti remove lines with "log" alone, you can use grep.

grep -vw log input.txt >output.txt

Or if you want to include only log as a single word (and not e.g. slog) followed by optional whitespace before a pair of parentheses with anything between them, you can use egrep:

egrep -v '\<log[ \t]*\(.*\)' input.txt >output.txt

Upvotes: 0

Beta
Beta

Reputation: 99094

Don't escape the parentheses; you want them to be literal. (Also, as far as I know sed does not support regex character class shorthand.)

sed -e '/[ \t]*log[ \t]*(.*)/d' input.txt > output.txt

Upvotes: 0

Related Questions