1 linux bash regex sed command-line
尝试使用 sed 从代码中删除所有注释行:
1)/* ... */和/* \n \n \n */
尝试使用这种结构来隐藏显示里面的内容
sed -n '/^\/\*/,/\*\//!p'
Run Code Online (Sandbox Code Playgroud)
但它似乎隐藏了不同行之间的内容并省略了内联/* .... */
我的意思是它在这里工作:
/********
This readme is intented ...
......
....
....
************/
Run Code Online (Sandbox Code Playgroud)
但它在这里不起作用:
/* Just a small bug */
Run Code Online (Sandbox Code Playgroud)
它采用第一个找到的内容并在下一行中/*继续进一步搜索:*/
/* Just a small bug */
code
code
code
/*****
To sum up this shows us...
...
...
...
...
******/
Run Code Online (Sandbox Code Playgroud)
所以“/*只是一个小错误*/”下的所有代码都被隐藏了:(我非常想念这个:
code
code
code
Run Code Online (Sandbox Code Playgroud)
2) // 内联:前面不包括 http:// 和 https://,后面包括 if
我正在尝试删除包含以下内容的字符串和部分字符串//:
sed 's/\/\/.*//'
Run Code Online (Sandbox Code Playgroud)
//仅当位于行开头时,此实现才会成功:
sed 's/^\/\/.*//'
Run Code Online (Sandbox Code Playgroud)
但最终它会删除内联链接,例如http://和https://
code
code https://www.sample.com/abc // include this URL
code https://www.sample.com/abc // exclude this URL but leave alow https://anothersample.com/xyz
code
Run Code Online (Sandbox Code Playgroud)
尝试搜索 sed 模式,搜索http://和https://,忽略它们,然后内联搜索//并删除它之后的所有内容(如果它们位于之后,则忽略 http/https //),但没有运气:(
也许有人有一个好主意,那就太棒了,无论如何,谢谢!
小智 5
我创建了这个小文本文件
/* one line comment */
some
multiple
code
here
/*****
multiple
line
comment
*****/
some code http://somelink
some code // some one line comment
Run Code Online (Sandbox Code Playgroud)
对于这个小测试文件,此命令适用于删除您提到的所有注释
cat comments.txt | sed -n '/^\/\*.*\*\//!p' | sed -n '/ \/\/.*/!p' | sed 's|/\*|\n&|g;s|*/|&\n|g' | sed '/\/\*/,/*\//d'
Run Code Online (Sandbox Code Playgroud)
该命令正在执行以下操作:
sed -n '/^\/\*.*\*\//!p':删除所有一行注释,例如/* one line comment */测试文件中的注释
sed -n '/ \/\/.*/!p':删除所有一行注释,如// some one line comment,但保留http://链接。由于这部分中有空白,因此可以完成此操作/ \/\/。我想,你可以改变这个/\s\/\/
sed 's|/\*|\n&|g;s|*/|&\n|g' | sed '/\/\*/,/*\//d':像示例中一样删除所有多条注释行,但保留代码。