Bash 是否可以将水头输出输送至sed?

Bash 是否可以将水头输出输送至sed?,bash,sed,Bash,Sed,输入文件 hello how are u some what doing fine so thats all huh thats great cool gotcha im fine 我想删除最后4行,而不重新定向到另一个文件或说就地编辑 我使用了head-n-3input.txt,但它只删除了最后两行 还想了解是否有可能将管头的输出传输到sed 像head-n-3input.txt | sed… 是的,我通过sed的选项删除了最后n行,如下所示,但无法理解命令的细微差别,因此继续使用head

输入文件

hello how are u
some what doing fine
so 
thats all
huh
thats great
cool
gotcha im fine
我想删除最后4行,而不重新定向到另一个文件或说就地编辑

我使用了
head-n-3input.txt
,但它只删除了最后两行

还想了解是否有可能将管头的输出传输到sed

head-n-3input.txt | sed…

是的,我通过sed的选项删除了最后n行,如下所示,但无法理解命令的细微差别,因此继续使用head命令的替代方案

sed -e :a -e '$d;N;2,5ba' -e 'P;D' file
编辑:不创建临时文件解决方案:

awk -i inplace -v lines=$(wc -l < Input_file) 'FNR<=(lines-4)' Input_file
第二种解决方案:使用
awk

awk -v lines=$(wc -l < Input_file) 'FNR<=(lines-4)' Input_file > temp_file && mv temp_file Input_file

awk-v lines=$(wc-lhead-n-3input.txt,但它只删除了最后两行,这似乎不正确。。对于
seq 10 | head-n-3
,您得到的输出是什么?
awk -v lines=$(wc -l < Input_file) 'FNR<=(lines-4)' Input_file > temp_file && mv temp_file Input_file