Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 头和格雷普同时出现_Bash_Unix_Grep_Unix Head - Fatal编程技术网

Bash 头和格雷普同时出现

Bash 头和格雷普同时出现,bash,unix,grep,unix-head,Bash,Unix,Grep,Unix Head,是否有unix单行程序来执行此操作 head -n 3 test.txt > out_dir/test.head.txt grep hello test.txt > out_dir/test.tmp.txt cat out_dir/test.head.txt out_dir/test.tmp.txt > out_dir/test.hello.txt rm out_dir/test.head.txt out_dir/test.tmp.txt 也就是说,我想同时从给定的文件中获取

是否有unix单行程序来执行此操作

head -n 3 test.txt > out_dir/test.head.txt
grep hello test.txt > out_dir/test.tmp.txt
cat out_dir/test.head.txt out_dir/test.tmp.txt > out_dir/test.hello.txt
rm out_dir/test.head.txt out_dir/test.tmp.txt
也就是说,我想同时从给定的文件中获取标题和一些grep行。

您可以说:

{ head -n 3 test.txt ; grep hello test.txt ; } > out_dir/test.hello.txt
使用awk:

awk 'NR<=3 || /hello/' test.txt > out_dir/test.hello.txt

awk'NR尝试使用
sed

sed -n '1,3p; /hello/p' test.txt > out_dir/test.hello.txt
该解决方案是最好的,但为了完整性,我将添加一个sed解决方案:

$ sed -n test.txt -e '1,3p' -e '4,$s/hello/hello/p' test.txt > $output_file
-n
说明除非指定,否则不要打印一行。
-e
是命令
'1,3p
打印前三行
4,$s/hello/hello/p
查找包含单词
hello
的所有行,并将其替换回
hello
。最后的
p
打印出替换操作的所有行


应该有一种方法可以使用
4,$g/HELLO/p
,但我无法让它工作。很长一段时间以来,我都没有真正弄糟sed。

当然,我会去
awk
,但这里有一个
ed
解决方案,用于前
vi
怀旧:

ed test.txt <<%
4,$ v/hello/d
w test.hello.txt
%

ed test.txt每当你发现自己在做从头部到grep再到sed再到grep再到尾部的多重剪切时,你应该总是想到
awk
。对于基本的文件操作来说,Awk并不难学,它可以从容地处理多个任务。@DavidW:很好的一点,Awk可以在大多数情况下在一个命令中替换所有这些多管道命令。如果
hello
出现在前三行怎么办?你将把标题线加倍。@DavidW。啊哈!说得好。我看你已经提供了解决方案。devnull的解决方案在这种情况下也会遇到麻烦,因为您正在使用一个不可移植的
sed
选项,并从原始文件中删除行,而这不是所要求的。@jlliagre
-i
是一个标准的Unix映射,但还没有成为Linux上的GNU实用程序。我对此感到惊讶,因为GNU通常在选项方面领先。
-e
在GNU和Unix平台以及
-n
上都有。您可以删除
-i
并将其传输到另一个文件,该文件将删除不兼容的问题。
awk
解决方案更好,但这是为了完整性。我将修改我的答案。
sed
可移植选项只有
-n
-e
-f
-i
是一个Gnuism,不知道为什么要写相反的代码。它也在BSD版本的
sed
中。这包括OS X。man.cx(假定为手册页的GNU版本)没有显示
-i
选项。但是,linux.man.de确实可以。我没有访问Solaris的权限,SunOS/Solaris网页的最新版本是1998年的。我已经从我的答案中删除了
-I
。(遗留)BSD
sed
没有
-I
。这个选项是在2001-09-25年由Paolo Bonzini在GNU源代码中首次引入的。它后来被其他实现采用,但从2013年起,根据其手册网页,它仍然不在最新的Solaris中:。请在您的帖子中提及
ed
是标准编辑器<代码>;)
@gniourf\u gniourf无需提及,每个人都知道这是唯一的一个
;)
使用您的命令序列,如果前三行中有一行包含
hello
,则它是重复的。我猜那是个虫子?否则,建议的解决方案在这方面是不等效的。@user2719058很好,您是对的,前3行不包含grep字符。