如何使用一些Linux命令复制一个巨大文件的前几行,并在其末尾添加一行文本?
发布于 2009-08-25 02:09:14
head
命令可以获取第一个n
行。变体包括:
head -7 file
head -n 7 file
head -7l file
它将获得名为"file"
的文件的前7行。要使用的命令取决于您的head
版本。Linux将与第一个一起工作。
要将行附加到同一文件的末尾,请使用:
echo 'first line to add' >>file
echo 'second line to add' >>file
echo 'third line to add' >>file
或者:
echo 'first line to add
second line to add
third line to add' >>file
一气呵成。
因此,将这两个想法结合在一起,如果您想将input.txt
文件的前10行放到output.txt
中,并在一行后面附加5个"="
字符,您可以使用如下代码:
( head -10 input.txt ; echo '=====' ) > output.txt
在这种情况下,我们在一个子shell中执行这两个操作,以便将输出流合并为一个流,然后用于创建或覆盖输出文件。
发布于 2009-08-25 02:12:34
我假设您正在尝试实现的是在文本文件的前几行之后插入一行。
head -n10 file.txt >> newfile.txt
echo "your line >> newfile.txt
tail -n +10 file.txt >> newfile.txt
如果您不想从文件中剩余的行,只需跳过尾部部分。
发布于 2009-08-25 02:05:13
前几行:man head
。
追加行:使用>>
运算符(?)在Bash中:
echo 'This goes at the end of the file' >> file
https://stackoverflow.com/questions/1325701
复制相似问题