在bash外壳中使用tail

问题描述 投票:0回答:1

我让我的文件f1包含x1,x2,x3,x4,每行都包含一个新行,如下图所示。我想读取给定的参数n。如果n = i,我想从位置i将f1的行写入文件f2中。

例如,如果n = 2 f2将包含x2,x3,x4。

如果n = 3,则f2将包含x3,x4。

以下我的代码无法正常运行。有谁知道如何解决只使用tail command

代码:

t = $n-1
tail -n -$t  $F1 >> F2.txt  

F1_content

linux bash shell tail
1个回答
0
投票

您使用的是正确的选项n | --lines

从尾巴man page

-n,--lines = [+] NUM输出最后NUM行,而不是最后10行;或使用-n+ NUM从第NUM行开始输出

因此,基于此,您应该为选项提供以+开头的有效值,例如:

n=3
t=$((n-1)) # arithmetic calc -> 2
tail --lines=+$t $F1 >> F2.txt  

tail --lines=+$t $F1的输出为例如:

x2
x3
x4
© www.soinside.com 2019 - 2024. All rights reserved.