简体   繁体   English

使用AWK连接两个文件的某些行,然后对合并的文件进行排序

[英]using AWK to join certain lines of two files and then sort the merged file

I use the following AWK command to sort the contents after the first 24 lines of a file: 我使用以下AWK命令对文件的前24行之后的内容进行排序:

awk 'NR <= 24; NR > 24 {print $0 | "sort -k3,3 -k4,4n"}' file > newFile

Now I want to join two files first (now simply discard the first 24 lines for both files) and then sort the merged file. 现在,我想先加入两个文件(现在只需丢弃两个文件的前24行),然后对合并的文件进行排序。 Is there any way to do it without generating a temporary merged file? 有什么方法可以在不生成临时合并文件的情况下进行操作?

awk 'FNR > 24' file1 file2 | sort -k3,3 -k4,4n > newFile

FNR is the file record number (resets to 1 for the first line of each file). FNR是文件记录号(每个文件的第一行重置为1)。 If you insist on having the sort inside the awk script, you can use: 如果您坚持要在awk脚本中进行sort ,则可以使用:

awk 'FNR > 24 { print $0 | "sort -k3,3 -k4,4n" }' file1 file2 > newFile

but I prefer the shell to do my piping. 但我更喜欢用外壳来做管道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM