I want to put certain lines in to two different files in a shell script. How should I put the syntax for this.
我想在shell脚本中将某些行放入两个不同的文件中。我该怎么做这个的语法。
Example:
A for loop prints 6 lines, and I want that the first two lines should be appended to 1st file, and the last 4 lines should be appended to the other file.
for循环打印6行,我希望前两行应该附加到第一个文件,最后4行应该附加到另一个文件。
2 个解决方案
#1
0
A for loop prints 6 lines, and I want that the first two lines should be appended to 1st file, and the last 4 lines should be appended to the other file.
for循环打印6行,我希望前两行应该附加到第一个文件,最后4行应该附加到另一个文件。
There is no way. One option would be to redirect everything to a file and then copy the desired sections of the log to other files.
没有办法。一种选择是将所有内容重定向到文件,然后将日志的所需部分复制到其他文件。
for i in {1..6}; do
echo $i > log
done
head -4 log >> logfile1 # Appends the first four lines to logfile1
tail -2 log >> logfile2 # Appends the last two lines to logfile2
#2
0
Answer
If you're using BASH you can use tee
to send the same input to both head -n2
and tail -n4
at the same time using a combination of process substitution and a pipe:
如果你正在使用BASH,你可以使用tee使用进程替换和管道的组合同时向head -n2和tail -n4发送相同的输入:
$ for i in {1..6}; do echo $i; done | tee >(head -n2 >first2.txt) | tail -n4 >last4.txt
$ cat first2.txt
1
2
$ cat last4.txt
3
4
5
6
Explanation
By default tee
takes its STDIN and copies it to file(s) specified as arguments in addition to its STDOUT. Since process substitution returns a /dev/fd
path to a file descriptor (echo >(true)
to see an example) tee
is able to write to that path like any other regular file.
默认情况下,除了STDOUT之外,tee还会将其STDIN复制到指定为参数的文件中。由于进程替换将/ dev / fd路径返回到文件描述符(echo>(true)以查看示例),因此tee能够像任何其他常规文件一样写入该路径。
Here's what the tee
command looks like after substitution:
以下是替换后tee命令的样子:
tee /dev/fd/xx | tail -n4 >last4.txt
Or more visually:
或者更直观:
tee | tail -n4 >last4.txt
:
/dev/fd/xx
:
:..>(head -n2 >first2.txt)
So the output gets copied both to the head
process (Whose output is redirected to first2.txt
) and out STDIN which is piped to the tail
process:
因此输出被复制到head进程(其输出重定向到first2.txt)和输出到尾进程的STDIN:
Note that process substitution is a BASH-ism, so if you're using a different shell or concerned about POSIX compliance it might not be available.
请注意,进程替换是BASH-ism,因此如果您使用的是其他shell或者关注POSIX兼容性,则可能无法使用它。
#1
0
A for loop prints 6 lines, and I want that the first two lines should be appended to 1st file, and the last 4 lines should be appended to the other file.
for循环打印6行,我希望前两行应该附加到第一个文件,最后4行应该附加到另一个文件。
There is no way. One option would be to redirect everything to a file and then copy the desired sections of the log to other files.
没有办法。一种选择是将所有内容重定向到文件,然后将日志的所需部分复制到其他文件。
for i in {1..6}; do
echo $i > log
done
head -4 log >> logfile1 # Appends the first four lines to logfile1
tail -2 log >> logfile2 # Appends the last two lines to logfile2
#2
0
Answer
If you're using BASH you can use tee
to send the same input to both head -n2
and tail -n4
at the same time using a combination of process substitution and a pipe:
如果你正在使用BASH,你可以使用tee使用进程替换和管道的组合同时向head -n2和tail -n4发送相同的输入:
$ for i in {1..6}; do echo $i; done | tee >(head -n2 >first2.txt) | tail -n4 >last4.txt
$ cat first2.txt
1
2
$ cat last4.txt
3
4
5
6
Explanation
By default tee
takes its STDIN and copies it to file(s) specified as arguments in addition to its STDOUT. Since process substitution returns a /dev/fd
path to a file descriptor (echo >(true)
to see an example) tee
is able to write to that path like any other regular file.
默认情况下,除了STDOUT之外,tee还会将其STDIN复制到指定为参数的文件中。由于进程替换将/ dev / fd路径返回到文件描述符(echo>(true)以查看示例),因此tee能够像任何其他常规文件一样写入该路径。
Here's what the tee
command looks like after substitution:
以下是替换后tee命令的样子:
tee /dev/fd/xx | tail -n4 >last4.txt
Or more visually:
或者更直观:
tee | tail -n4 >last4.txt
:
/dev/fd/xx
:
:..>(head -n2 >first2.txt)
So the output gets copied both to the head
process (Whose output is redirected to first2.txt
) and out STDIN which is piped to the tail
process:
因此输出被复制到head进程(其输出重定向到first2.txt)和输出到尾进程的STDIN:
Note that process substitution is a BASH-ism, so if you're using a different shell or concerned about POSIX compliance it might not be available.
请注意,进程替换是BASH-ism,因此如果您使用的是其他shell或者关注POSIX兼容性,则可能无法使用它。