bash 使用 sed 从 javascript 文件中删除所有 console.log

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6595429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:19:00  来源:igfitidea点击:

Using sed to remove all console.log from javascript file

regexbashsed

提问by pkyeck

I'm trying to remove all my console.log, console.diretc. from my JS file before minifying it with YUI (on osx).

在使用 YUI(在 osx 上)缩小它之前console.log,我试图console.dir从我的 JS 文件中删除所有我的等。

The regex I got for the console statements looks like this:

我为控制台语句得到的正则表达式如下所示:

console.(log|debug|info|warn|error|assert|dir|dirxml|trace|group|groupEnd|time|timeEnd|profile|profileEnd|count)\((.*)\);?

and it works if I test it with the RegExr. But it won't work with sed.

如果我用RegExr测试它,它会起作用。但它不适用于sed.

What do I have to change to get this working?

我需要改变什么才能让它工作?

sed 's/___???___//g' <$RESULT >$RESULT_STRIPPED


update


更新

After getting the first answer I tried

得到第一个答案后,我试过了

sed 's/console.log(.*)\;//g' <test.js >result.js

and this works, but when I add an OR

这有效,但是当我添加 OR 时

sed 's/console.\(log\|dir\)(.*)\;//g' <test.js >result.js

it doesn't replace the "logs":

它不会替换“日志”:

terminal screenshot

终端截图

回答by Martin

Your original expression looks fine. You just need to pass the -Eflag to sed, for extendedregular expressions:

你原来的表情看起来不错。对于扩展的正则表达式,您只需要将-E标志传递给:sed

sed -E 's/console.(log|debug|info|...|count)\((.*)\);?//g'

The difference between these types of regular expressions is explained in man re_format. To be honest I have never read that page, but instead simply tack on an -Ewhen things don't work as expected. =)

中解释了这些类型的正则表达式之间的区别man re_format。老实说,我从来没有读过那一页,而是-E在事情没有按预期工作时简单地添加。=)

回答by Zsolt Botykai

You must escape ((for grouping) and |(for oring) in sed's regex syntax. E.g.:

您必须在的正则表达式语法中转义((用于分组)和|(用于 oring)sed。例如:

sed 's/console.\(log\|debug\|info\|warn\|error\|assert\|dir\|dirxml\|trace\|group\|groupEnd\|time\|timeEnd\|profile\|profileEnd\|count\)(.*);\?//g'

UPDATEexample:

更新示例:

$ sed 's/console.\(log\|debug\|info\|warn\|error\|assert\|dir\|dirxml\|trace\|group\|groupEnd\|time\|timeEnd\|profile\|profileEnd\|count\)(.*);\?//g'
console.log # <- input line, not matches, no replacement printed on next line
console.log
console.log() # <- input line, matches, no printing

console.log(blabla); # <- input line, matches, no printing

console.log(blabla) # <- input line, matches, no printing

console.debug();  # <- input line, matches, no printing

console.debug(BAZINGA)  # <- input line, matches, no printing

DATA console.info(ditto); DATA2 # <- input line, matches, printing of expected data
DATA  DATA2

HTH

HTH

回答by windylcx

I also find the way to remove all the console.log , and i am trying to use python to do this, but i find the Regex is not work for. my writing like this:

我还找到了删除所有 console.log 的方法,我正在尝试使用 python 来执行此操作,但我发现 Regex 不适用。我的写作是这样的:

var re=/^console.log(.*);?$/;

var re=/^console.log(.*);?$/;

but it will match the following string:

但它将匹配以下字符串:

'console.log(23);alert(234dsf);'

'console.log(23);警报(234dsf);'

does it work? with the

它有效吗?与

"s/console.(log|debug|info|...|count)((.*));?//g"

"s/console.(log|de​​bug|info|...|count)((.*));?//g"

回答by rodrigocorsi

I try this:

我试试这个:

sed -E 's/console.(log|debug|info)( ?| +)\([^;]*\);//g'

See the test: Regex Tester

查看测试: Regex Tester

回答by Ecko123

Here's my implementation

这是我的实现

for i in $(find ./dir -name "*.js")
do

sed -E 's/console\.(log|warn|error|assert..timeEnd)\((.*)\);?//g' $i > ${i}.copy && mv ${i}.copy $i

done

took the sed thing from github

从 github 上拿了 sed 的东西

回答by mathewguest

Rodrigocorsi's works with nested parentheses. I added a ? after the ; because yuicompressor was omitting some semicolons.

Rodrigocorsi 的作品带有嵌套括号。我加了一个?之后 ; 因为 yuicompressor 省略了一些分号。

回答by Trinadh Koya

It is probable that the reason this is not working is that you are not 'limiting' the regex to not include a closing parenthesises ()) in the method parameters.

这可能不起作用的原因是您没有“限制”正则表达式)在方法参数中不包含右括号 ( )。

Try this regular expression:

试试这个正则表达式:

console\.(log|trace|error)\(([^)]+)\);

Remember to include the rest of your method names in the capture group.

请记住在捕获组中包含其余的方法名称。

回答by Keegan's hairstyle 82

I'd just like to add here that I was running into issues with namespaced console.logs such as window.console.log. Also Tweenmax.js has some interesting uses of console.log in some parts such as

我只想在这里补充一点,我遇到了命名空间 console.logs(例如 window.console.log)的问题。此外,Tweenmax.js 在某些部分对 console.log 有一些有趣的用途,例如

       window.console&&console.log(t)

So I used this

所以我用了这个

  sed -i.bak s/[^\&a-zA-Z0-9\.]console.log\(/\/\//g js/combined.js

The regex effectively says replace all console.logs that don't start with &, alphanumerics, and . with a '//' comment, which uglify later takes out.

正则表达式有效地表示替换所有不以&、字母数字和.开头的console.logs。带有 '//' 注释,然后 uglify 将其删除。

回答by Ben

I was feeling lazy and hoping to find a script to copy & paste. Alas there wasn't one, so for the lazy like me, here is mine. It goes in a file named something like 'minify.sh' in the same directory as the files to minify. It will overwrite the original file and it needs to be executable.

我感觉很懒惰,希望找到一个脚本来复制和粘贴。唉,没有一个,所以对于像我这样的懒惰者,这是我的。它位于与要缩小的文件相同的目录中名为“minify.sh”的文件中。它将覆盖原始文件并且它需要是可执行的。

#!/bin/bash

for f in *.js
do
    sed -Ei 's/console.(log|debug|info)\((.*)\);?//g' $f
    yui-compressor $f -o $f
done