简体   繁体   中英

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

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

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

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 eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM