有没有办法忽略 UNIX 排序中的标题行? [英] Is there a way to ignore header lines in a UNIX sort?

查看:33
本文介绍了有没有办法忽略 UNIX 排序中的标题行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个固定宽度的字段文件,我正在尝试使用 UNIX(在我的情况下为 Cygwin)排序实用程序对其进行排序.

I have a fixed-width-field file which I'm trying to sort using the UNIX (Cygwin, in my case) sort utility.

问题是文件顶部有一个两行的标题,它被排序到文件底部(因为每个标题行都以冒号开头).

The problem is there is a two-line header at the top of the file which is being sorted to the bottom of the file (as each header line begins with a colon).

有没有办法告诉 sort 要么通过未排序的前两行"或指定将冒号行排序到顶部的排序 - 其余行始终以 6 位数字开头(实际上是我正在排序的关键)如果有帮助.

Is there a way to tell sort either "pass the first two lines across unsorted" or to specify an ordering which sorts the colon lines to the top - the remaining lines are always start with a 6-digit numeric (which is actually the key I'm sorting on) if that helps.

示例:

:0:12345
:1:6:2:3:8:4:2
010005TSTDOG_FOOD01
500123TSTMY_RADAR00
222334NOTALINEOUT01
477821USASHUTTLES21
325611LVEANOTHERS00

应该排序为:

:0:12345
:1:6:2:3:8:4:2
010005TSTDOG_FOOD01
222334NOTALINEOUT01
325611LVEANOTHERS00
477821USASHUTTLES21
500123TSTMY_RADAR00

推荐答案

(head -n 2 <file> && tail -n +3 <file> | sort) > newfile

括号创建了一个子shell,包装了标准输出,这样你就可以通过管道或重定向它,就好像它来自单个命令一样.

The parentheses create a subshell, wrapping up the stdout so you can pipe it or redirect it as if it had come from a single command.

这篇关于有没有办法忽略 UNIX 排序中的标题行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆