Unix 从数据文件中删除注释。有什么区别?
假设您希望使用以下两种方法之一从数据文件中删除注释:Unix 从数据文件中删除注释。有什么区别?,unix,sed,grep,command,Unix,Sed,Grep,Command,假设您希望使用以下两种方法之一从数据文件中删除注释: cat file.dat | sed -e "s/\#.*//" cat file.dat | grep -v "#" 这些单独的方法是如何工作的,它们之间的区别是什么?一个人是否也可以将干净的数据写入新文件,同时避免任何可能的警告或错误消息最终出现在该数据文件中?如果是这样的话,你会怎么做 这些单独的方法是如何工作的,有什么区别 他们之间 是的,虽然sed和grep是两个不同的命令,但它们的工作原理相同。您的sed命令只是将所有具有#的行
cat file.dat | sed -e "s/\#.*//"
cat file.dat | grep -v "#"
这些单独的方法是如何工作的,它们之间的区别是什么?一个人是否也可以将干净的数据写入新文件,同时避免任何可能的警告或错误消息最终出现在该数据文件中?如果是这样的话,你会怎么做
这些单独的方法是如何工作的,有什么区别
他们之间
是的,虽然sed
和grep
是两个不同的命令,但它们的工作原理相同。您的sed
命令只是将所有具有#
的行替换为NULL。另一方面,grep
将跳过或忽略那些将跳过其中包含
的行
您可以通过手册页获得有关这些的更多信息,如下所示:
man grep
:
-o, --only-matching
Print only the matched (non-empty) parts of a matching line,
with each such part on a separate output line.
man sed
:
替换可能
包含特殊字符&表示匹配的模式空间部分,特殊转义符\1
通过\9到
请参阅regexp中相应的匹配子表达式
一个人是否也可以将干净的数据写入 新文件,同时避免任何可能的警告或错误消息 在那个数据文件里结束 是的,我们可以通过在这两个命令中使用
2>/dev/null
来重新引导错误
如果是这样的话,你会怎么做
您可以像2>/dev/null 1>输出文件那样尝试
对sed
命令的解释:现在也添加对sed
命令的解释。这仅用于理解目的,无需使用cat
然后再使用sed
您可以使用sed-e“s/\\\\\\\.*/”Input\u文件
sed -e " ##Initiating sed command here with adding the script to the commands to be executed
s/ ##using s for substitution of regexp following it.
\#.* ##telling sed to match a line if it has # till everything here.
//" ##If match found for above regexp then substitute it with NULL.
这些单独的方法是如何工作的,有什么区别
他们之间
是的,虽然sed
和grep
是两个不同的命令,但它们的工作原理相同。您的sed
命令只是将所有具有#
的行替换为NULL。另一方面,grep
将跳过或忽略那些将跳过其中包含
的行
您可以通过手册页获得有关这些的更多信息,如下所示:
man grep
:
-o, --only-matching
Print only the matched (non-empty) parts of a matching line,
with each such part on a separate output line.
man sed
:
替换可能
包含特殊字符&表示匹配的模式空间部分,特殊转义符\1
通过\9到
请参阅regexp中相应的匹配子表达式
一个人是否也可以将干净的数据写入
新文件,同时避免任何可能的警告或错误消息
在那个数据文件里结束
是的,我们可以通过在这两个命令中使用2>/dev/null
来重新引导错误
如果是这样的话,你会怎么做
您可以像2>/dev/null 1>输出文件那样尝试
对sed
命令的解释:现在也添加对sed
命令的解释。这仅用于理解目的,无需使用cat
然后再使用sed
您可以使用sed-e“s/\\\\\\\.*/”Input\u文件
sed -e " ##Initiating sed command here with adding the script to the commands to be executed
s/ ##using s for substitution of regexp following it.
\#.* ##telling sed to match a line if it has # till everything here.
//" ##If match found for above regexp then substitute it with NULL.
grep-v
将丢失上面有#
的所有行,例如:
$ cat file
first
# second
thi # rd
所以
将删除所有带有#
的行,这是不好的。相反,你应该:
$ grep -o "^[^#]*" file
first
thi
就像sed
命令那样,但这样您就不会得到空行<代码>人工grep
:
-o, --only-matching
Print only the matched (non-empty) parts of a matching line,
with each such part on a separate output line.
grep-v
将丢失上面有#
的所有行,例如:
$ cat file
first
# second
thi # rd
所以
将删除所有带有#
的行,这是不好的。相反,你应该:
$ grep -o "^[^#]*" file
first
thi
就像sed
命令那样,但这样您就不会得到空行<代码>人工grep
:
-o, --only-matching
Print only the matched (non-empty) parts of a matching line,
with each such part on a separate output line.
请详细说明sed命令是如何从“s/\\\\.*/”?@B.Ing中推断出这一点的。现在请检查我的编辑以获得解决方案,我已经为我的命令添加了解释。请检查此URL一次,如果有人提供帮助,您应该怎么做?请详细说明sed命令如何从“s/\\\\.*//”中推断此URL?@B.Ing,请立即检查我的编辑以获得解决方案,我现在已为我的命令添加了解释。也检查一下这个URL一旦有人帮了你,你应该怎么做