第一个X线后整理文件的更优雅的方法是什么? [英] More elegant method of sorting a file after the first X lines?

查看:173
本文介绍了第一个X线后整理文件的更优雅的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了很多今天在Stack Overflow上寻找一个解决方案,并已发现了大约跳过X布线后整理了很多问题,但没有真正坚实通用的答案,所以我把做在一起我自己粗制滥造的方法所以:

I've been doing a lot of searching on Stack Overflow today for a solution for this and have found many questions about sorting after skipping X lines, but no really solid generic answers, so I threw together my own slipshod method of doing so:

head -n 15 FILE.EXT > temp.txt
tail -n+16 FILE.EXT | sort >> temp.txt
mv temp.txt FILE.EXT

这将整理文件(把你对排序的选项中选择),而preserving它的第15行的顺序。这显然​​是相当不雅,有三个文件引用和两个不同的值进入。理想情况下,我想拿出那个少一点麻烦,如果可能的命令,因为这好像没有太大的支持pretty的共同愿望。

This will sort the file (take your pick of the options for sort), while preserving the order of the first 15 lines of it. This is obviously fairly inelegant, with three file references and two different values to enter. Ideally I'd like to come up with a command that is less cumbersome, if possible, because this seems like a pretty common desire with not much support.


  • 有没有人有一个简单的解决方案比我?

  • 这有什么错我做了什么?潜在的问题?

  • 这个问题借自己更强烈使用脚本,但我的命令仍然可能略高于创建和执行一个脚本一次性更快。

我不甚至接近一个bash的专家,所以我希望有一些bash的福,可以使一个快速的单行。有没有办法在一个单一的命令来创建和引用变量,这样用户只需要投入类似的名称和行号?

I'm not even close to a bash expert, so I'm hoping there is some bash-fu that can make this a quick one-liner. Is there a way to create and reference variables in a single command so that a user only needs to put in something like the name and line number?

推荐答案

这一班轮'产生的输出:

This 'one-liner' generates the output:

awk 'NR <= 15 { print; next } { print | "sort" }'

覆盖原文件干净更硬,一般涉及到的东西写入到一个临时文件并重新命名它这是完成时。

Overwriting the original file cleanly is harder, and generally involves something that writes to a temporary file and renames it when that's complete.

由于 sputnick <一个href=\"http://stackoverflow.com/questions/27789885/more-elegant-method-of-sorting-a-file-after-the-first-x-lines/27790010#comment43990011_27790010\">points出,如果​​你有GNU AWK ,您可以使用 -i 选项就地覆盖

As sputnick points out, if you have GNU awk, you can use the -i option to overwrite in-place:

gawk -i 'NR <= 15 { print; next } { print | "sort" }' FILE.EXT

(和 GAWK 通常也安装为 AWK

如果你没有GNU AWK ,那么我有一个脚本从脚本<$衍生嗷嗷 C $ C>覆盖;派克的UNIX编程环境的做到了这一点。

If you don't have GNU awk, then I have a script ow derived from a script overwrite from Kernighan & Pike The UNIX Programming Environment that does just that.

用法:

ow FILE.EXT awk 'NR <= 15 { print; next } { print | "sort" }' FILE.EXT

code:

:   "@(#)$Id: ow.sh,v 1.6 2005/06/30 18:14:08 jleffler Exp $"
#
#   Overwrite file
#   From: The UNIX Programming Environment by Kernighan and Pike
#   Amended: remove PATH setting; handle file names with blanks.

case $# in
0|1)    
    echo "Usage: $0 file command [arguments]" 1>&2
    exit 1;;
esac

file="$1"
shift
new=${TMPDIR:-/tmp}/ovrwr.$$.1
old=${TMPDIR:-/tmp}/ovrwr.$$.2

trap "rm -f '$new' '$old' ; exit 1" 0 1 2 15

if "$@" >"$new"
then
    cp "$file" "$old"
    trap "" 1 2 15
    cp "$new" "$file"
    rm -f "$new" "$old"
    trap 0
    exit 0
else
    echo "$0: $1 failed - $file unchanged" 1>&2
    rm -f "$new" "$old"
    trap 0
    exit 1
fi

这是旧code;我没有修改它了将近十年了,但我已经用它了不少。作为<一个href=\"http://stackoverflow.com/questions/27789885/more-elegant-method-of-sorting-a-file-after-the-first-x-lines/27790010#comment43990001_27790010\">noted由查尔斯·达菲,它可以与一些现代化的,如果你有可能面临开始破折号文件名做(因为那些可能被误认为是命令行选项 CP MV ),它应该有一个shebang行等

It's old code; I haven't modified it for almost a decade now, but I have used it quite a lot. As noted by Charles Duffy, it could do with some modernization if you're likely to face file names starting with dashes (because those could be mistaken for command-line options to cp or mv), and it should have a shebang line, etc.

这也显示捕获的信号(虽然现在,我通常陷阱 0 1 2 3 13 15 ',相当于'退出HUP INT QUIT PIPE TERM )和$ p $命名的临时文件pventing随意干扰(使用 $ ,而不是 mktemp的&MDASH;就像我说的,它是旧的code)

It also shows trapping signals (though nowadays, I usually trap '0 1 2 3 13 15', equivalent to 'EXIT HUP INT QUIT PIPE TERM') and naming temporary files for preventing casual interference (using $$ rather than mktemp — like I said, it is old code).

这篇关于第一个X线后整理文件的更优雅的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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