0

场景是我有一个文件并包含一个字符串“日期和时间是 2012-12-07 17:11:50”

我已经搜索并找到了一个命令

grep 'the date and time is' 2012-12-07.txt | cut -d\   -f5

它只显示第 5 个单词,我需要第 5 个和第 6 个的组合,所以我尝试了

grep 'the date and time is' 2012-12-07.txt | cut -d\   -f5 -f6 

但它的错误。

现在,如何用一个命令 grep 第 5 个和第 6 个单词

我只需要像这样的输出2012-12-07 17:11:50

4

3 回答 3

3

你应该可以使用

$ grep 'the date and time is' 2012-12-07.txt | cut -d' ' -f6-7

检查手册页-f以获取选项参数的语法。

于 2012-12-07T12:09:35.310 回答
0

这听起来像是一个工作awk,这可能比构建一个由多个进程组成的管道要快一点:

pax> echo 'hello
           the date and time is 2012-12-07 17:11:50
           goodbye' | awk '/the date and time is/ {print $6" "$7}'
2012-12-07 17:11:50

它将搜索和修改结合在一个命令中。

请记住,与您的解决方案一样,如果您的搜索字符串之前有内容,则此解决方案将无济于事,但awk也可以根据您的需求的复杂性来执行此操作,例如:

pax> echo 'hello
           Today (Friday), the date and time is 2012-12-07 17:11:50
           goodbye' | awk '/the date and time is/ {
                               sub (".*is","",$0);
                               print $1" "$2
                               }'
2012-12-07 17:11:50
于 2012-12-07T12:15:59.667 回答
0

我猜它不是第 5 和第 6 而是第 6 和第 7

grep 'the date and time is' 2012-12-07.txt |awk '{print $6,$7}'
于 2012-12-07T12:13:34.000 回答