使用awk和/或sed按字母顺序对文件中的行进行排序 [英] Sorting lines in a file alphabetically using awk and/or sed

查看:209
本文介绍了使用awk和/或sed按字母顺序对文件中的行进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有几百行格式的文件:

I have a file with several hundred lines formatted as so:

#blah
RewriteCond %{HTTP_HOST} www.blah.com [NC]
RewriteRule ^/xyz(|/)$ http://www.blah.com/404.html [R=301,L,NC]

#xblah
RewriteCond %{HTTP_HOST} www.blah.com [NC]
RewriteRule ^/hkf(|/)$ http://www.blah.com/404.html [R=301,L,NC]

#ablah
RewriteCond %{HTTP_HOST} www.blah.com [NC]
RewriteRule ^/abc/.*(|/)$ http://www.blah.com/404.html [R=301,L,NC]

我想在awk/sed中创建一个脚本,以使用每组文本第三行中的第二个参数对该文件按字母顺序排序.对于此文件,可以是"abc","hkf"或"xyz"-它们是在此apache重定向文件中创建的重定向.

I would like to create a script in awk/sed to alphabetize this file using the second argument in the third line of each group of text. In the case of this file it's the "abc", "hkf" or "xyz" which could be anything - they're the redirects being created in this apache redirects file.

我认为我想做的是:

  1. 将每三行的组连接成一行,并在每行之间使用定界符
  2. 使用-k3,3排序
  3. 然后用空白行重新组装3行构造
  4. 写入文件

我的预期输出如下:

#ablah
RewriteCond %{HTTP_HOST} www.blah.com [NC]
RewriteRule ^/abc/.*(|/)$ http://www.blah.com/404.html [R=301,L,NC]

#xblah
RewriteCond %{HTTP_HOST} www.blah.com [NC]
RewriteRule ^/hkf(|/)$ http://www.blah.com/404.html [R=301,L,NC]

#blah
RewriteCond %{HTTP_HOST} www.blah.com [NC]
RewriteRule ^/xyz(|/)$ http://www.blah.com/404.html [R=301,L,NC]

这有意义吗?有一个更好的方法吗?

Does this make sense? Is there a better way to do this?

p.s.我的目的是使脚本具有可移植性,以便可以在此结构的多个文件中使用该脚本.在提出解决问题的代码建议时,请尽可能地将其拼写出来,以便像我这样的新手开始理解如何有效地解决此问题并能够扩展最终结果.

p.s. my intent is to make the script portable so it can be used on several files of this structure. When suggesting code to solve the problem, please spell it out as best possible for a rank newb like me to start to understand how to tackle this problem efficiently and be able to extend the end result.

任何人和所有帮助都将不胜感激.

Any and all help greatly appreciated.

推荐答案

您可以在Gnu Awk中完成整个操作:

You can do the whole operation in Gnu Awk:

awk -f sort.awk input.txt

其中sort.awk

BEGIN {
    RS=""
}
{
    match($0,/RewriteRule \^\/(.*)\(\|/,a)
    key[NR]=a[1] "\t" NR
    block[NR]=$0
}

END {
    asort(key)
    for (i=1; i<=NR; i++) {
        split(key[i],a,"\t")
        print block[a[2]]
        printf "\n"
    }
}

产生:

#ablah
RewriteCond %{HTTP_HOST} www.blah.com [NC]
RewriteRule ^/abc/.*(|/)$ http://www.blah.com/404.html [R=301,L,NC]

#xblah
RewriteCond %{HTTP_HOST} www.blah.com [NC]
RewriteRule ^/hkf(|/)$ http://www.blah.com/404.html [R=301,L,NC]

#blah
RewriteCond %{HTTP_HOST} www.blah.com [NC]
RewriteRule ^/xyz(|/)$ http://www.blah.com/404.html [R=301,L,NC]

这篇关于使用awk和/或sed按字母顺序对文件中的行进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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