How do I copy the first few lines of a giant file and add a line of text at the end of it, using some Linux commands?
如何复制一个大型文件的前几行并在其末尾添加一行文本,使用一些Linux命令?
3 个解决方案
#1
104
The head
command can get the first n
lines. Variations are:
head命令可以得到前n行。变化是:
head -7 file
head -n 7 file
head -7l file
which will get the first 7 lines of the file called "file"
. The command to use depends on your version of head
. Linux will work with the first one.
这将得到文件的前7行“文件”。要使用的命令取决于您的head版本。Linux将使用第一个。
To append lines to the end of the same file, use:
要将行附加到同一文件的末尾,请使用:
echo 'first line to add' >>file
echo 'second line to add' >>file
echo 'third line to add' >>file
or:
或者:
echo 'first line to add
second line to add
third line to add' >>file
to do it in one hit.
一击就搞定了。
So, tying these two ideas together, if you wanted to get the first 10 lines of the input.txt
file to output.txt
and append a line with five "="
characters, you could use something like:
把这两个想法结合起来,如果你想要得到输入的前10行。txt文件输出。txt和附加一行5个"="字符,您可以使用如下内容:
( head -10 input.txt ; echo '=====' ) > output.txt
In this case, we do both operations in a sub-shell so as to consolidate the output streams into one, which is then used to create or overwrite the output file.
在本例中,我们在子shell中执行这两个操作,以便将输出流合并为一个流,然后使用这个流创建或覆盖输出文件。
#2
15
I am assuming what you are trying to achieve is to insert a line after the first few lines of of a textfile.
我假设您要实现的是在文本文件的前几行后面插入一行。
head -n10 file.txt >> newfile.txt
echo "your line >> newfile.txt
tail -n +10 file.txt >> newfile.txt
If you don't want to rest of the lines from the file, just skip the tail part.
如果您不想从文件中删除行,就跳过尾部部分。
#3
3
First few lines: man head
.
开头几行:男人的头。
Append lines: use the >>
operator (?) in Bash:
附加行:在Bash中使用>>操作符(?):
echo 'This goes at the end of the file' >> file
#1
104
The head
command can get the first n
lines. Variations are:
head命令可以得到前n行。变化是:
head -7 file
head -n 7 file
head -7l file
which will get the first 7 lines of the file called "file"
. The command to use depends on your version of head
. Linux will work with the first one.
这将得到文件的前7行“文件”。要使用的命令取决于您的head版本。Linux将使用第一个。
To append lines to the end of the same file, use:
要将行附加到同一文件的末尾,请使用:
echo 'first line to add' >>file
echo 'second line to add' >>file
echo 'third line to add' >>file
or:
或者:
echo 'first line to add
second line to add
third line to add' >>file
to do it in one hit.
一击就搞定了。
So, tying these two ideas together, if you wanted to get the first 10 lines of the input.txt
file to output.txt
and append a line with five "="
characters, you could use something like:
把这两个想法结合起来,如果你想要得到输入的前10行。txt文件输出。txt和附加一行5个"="字符,您可以使用如下内容:
( head -10 input.txt ; echo '=====' ) > output.txt
In this case, we do both operations in a sub-shell so as to consolidate the output streams into one, which is then used to create or overwrite the output file.
在本例中,我们在子shell中执行这两个操作,以便将输出流合并为一个流,然后使用这个流创建或覆盖输出文件。
#2
15
I am assuming what you are trying to achieve is to insert a line after the first few lines of of a textfile.
我假设您要实现的是在文本文件的前几行后面插入一行。
head -n10 file.txt >> newfile.txt
echo "your line >> newfile.txt
tail -n +10 file.txt >> newfile.txt
If you don't want to rest of the lines from the file, just skip the tail part.
如果您不想从文件中删除行,就跳过尾部部分。
#3
3
First few lines: man head
.
开头几行:男人的头。
Append lines: use the >>
operator (?) in Bash:
附加行:在Bash中使用>>操作符(?):
echo 'This goes at the end of the file' >> file