bash 从文件中删除所有以 # 开头的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8206280/
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
Delete all lines beginning with a # from a file
提问by Village
All of the lines with comments in a file begin with #
. How can I delete all of the lines (and only those lines) which begin with #
? Other lines containing #
, but not at the beginning of the line should be ignored.
文件中所有带有注释的行都以#
. 如何删除以 开头的所有行(仅那些行)#
?其他包含#
, 但不在行首的行应该被忽略。
回答by Raymond Hettinger
This can be done with a sed one-liner:
这可以通过sed one-liner来完成:
sed '/^#/d'
This says, "find all lines that start with # and delete them, leaving everything else."
这表示,“找到所有以 # 开头的行并删除它们,留下所有其他内容。”
回答by Keith Thompson
I'm a little surprised nobody has suggested the most obvious solution:
我有点惊讶没有人提出最明显的解决方案:
grep -v '^#' filename
This solves the problem as stated.
这解决了上述问题。
But note that a common convention is for everything from a #
to the end of a line to be treated as a comment:
但请注意,一个常见的约定是从 a#
到行尾的所有内容都被视为注释:
sed 's/#.*$//' filename
though that treats, for example, a #
character within a string literal as the beginning of a comment (which may or may not be relevant for your case) (and it leaves empty lines).
尽管例如#
将字符串文字中的字符视为注释的开头(这可能与您的情况相关,也可能不相关)(并且留下空行)。
A line starting with arbitrary whitespace followed by #
might also be treated as a comment:
以任意空格开头的行#
也可以被视为注释:
grep -v '^ *#' filename
if whitespace is only spaces, or
如果空格只是空格,或
grep -v '^[ ]#' filename
where the two spaces are actually a space followed by a literal tab character (type "control-v tab").
其中两个空格实际上是一个空格,后跟一个文字制表符(键入“control-v tab”)。
For all these commands, omit the filename
argument to read from standard input (e.g., as part of a pipe).
对于所有这些命令,省略filename
从标准输入读取的参数(例如,作为管道的一部分)。
回答by ata
The opposite of Raymond's solution:
与雷蒙德的解决方案相反:
sed -n '/^#/!p'
"don't print anything, except for lines that DON'T start with #"
“不要打印任何东西,除了不以# 开头的行”
回答by jaypal singh
You can use the following for an awk solution -
您可以将以下内容用于 awk 解决方案 -
awk '/^#/ {sub(/#.*/,"");getline;}1' inputfile
回答by rubo77
you can directly edit your file with
你可以直接编辑你的文件
sed -i '/^#/ d'
If you want also delete comment lines that start with some whitespace use
如果您还想删除以一些空格开头的注释行,请使用
sed -i '/^\s*#/ d'
Usually, you want to keep the first line of your script, if it is a sha-bang, so sed
should not delete lines starting with #!
. also it should delete lines, that just contain only a hash but no text. put it all together:
通常,您希望保留脚本的第一行,如果它是 sha-bang,则sed
不应删除以#!
. 它也应该删除只包含散列但不包含文本的行。把它们放在一起:
sed -i '/^\s*\(#[^!].*\|#$\)/d'
To be conform with all sed variants you need to add a backup extension to the -i
option:
要符合所有 sed 变体,您需要向-i
选项添加备份扩展:
sed -i.bak '/^\s*#/ d' $file
rm -Rf $file.bak
回答by Acumenus
This answer builds upon the earlier answer by Keith.
这个答案建立在Keith之前的答案之上。
egrep -v "^[[:blank:]]*#"
should filter out comment lines.
egrep -v "^[[:blank:]]*#"
应该过滤掉注释行。
egrep -v "^[[:blank:]]*(#|$)"
should filter out both comments and empty lines, as is frequently useful.
egrep -v "^[[:blank:]]*(#|$)"
应该过滤掉注释和空行,这通常很有用。
For information about [:blank:]
and other character classes, refer to https://en.wikipedia.org/wiki/Regular_expression#Character_classes.
有关[:blank:]
和其他字符类的信息,请参阅https://en.wikipedia.org/wiki/Regular_expression#Character_classes。
回答by Bruno Damas
Here is it with a loop for all files with some extension:
这是带有扩展名的所有文件的循环:
ll -ltr *.filename_extension > list.lst
for i in $(cat list.lst | awk '{ print }') # validate if it is the 8 column on ls
do
echo $i
sed -i '/^#/d' $i
done