有没有办法忽略头文件行在UNIX排序? [英] Is there a way to ignore header lines in a UNIX sort?

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

问题描述

我有一个固定宽度字段文件,我试图使用UNIX(Cygwin,在我的情况下)排序实用程序排序。



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



有一种方法来告诉排序要么通过未排序的前两行,要么指定将冒号行排在顶部的排序 - 其余行总是以6位数字开始(这实际上是关键I'

示例:

 : 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


解决方案

  -n 2< file> &&& tail -n +3< file> |排序)>新文件

括号创建一个子shell,包装stdout,以便管道或重定向它来自一个命令。


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).

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.

Example:

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

should sort to:

: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

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天全站免登陆