使用awk或sed对以pattern开头的行进行排序 [英] sort rows starting with pattern using awk or sed

查看:171
本文介绍了使用awk或sed对以pattern开头的行进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印路由器配置并仅对以模式crypto isakmp key 6开头的行进行排序.

I'd like to print router configuration and sort only lines starting with pattern crypto isakmp key 6.

重要的是,我想将该行放在同一位置,以便这些行之前和之后的所有行都应保持在相同的位置和顺序(未排序).

The important thing is that I want to leave that lines in same place so all lines before and after these lines should stay in same place and order (not sorted).

示例输入文件:

123 345
678 901
bla bla bla
ble ble ble
crypto isakmp key 6 kokofeofepokpfowkfpwjeiofjwiojefiow address 123.456.789.012
crypto isakmp key 6 ofjwiojefiow352okdwofkwkfi9i42kpfsej09f09j4 address 123.456.789.012
crypto isakmp key 6 9i42kpfsej09f09j4ofjwiojefiow352okdwofkwkfi address 123.456.789.012
crypto isakmp key 6 9j4ofjwiojefiow352okdwofkwkfi9i42kpfsej09f0 address 123.456.789.012
ccc ddd eee
fff ggg hhh iii
123 456

所以首先我想打印不变(行的随机数):

So first I'd like to print unchanged ( random count of rows ):

123 345
678 901
bla bla bla
ble ble ble

然后我要打印从crypto isakmp key 6开始的SORTED行.

Then I want to print SORTED lines starting with crypto isakmp key 6.

最后,我想保留其余文件的原样(也是行的随机计数):

At the end I'd like to print rest of file unchanged (also random count of rows):

ccc ddd eee
fff ggg hhh iii
123 456

我已经通过许多操作来管理此问题,包括获取crypto isakmp key 6的第一个和最后一个位置以及使用tail/head命令,但是这非常复杂,我想知道AWK/SED中是否有其他Linux工具的选项管理指定的行.请分步说明您的命令的作用.

I've managed this by many operations including getting first and last position of crypto isakmp key 6 and using tail / head commands but it's quite complicated and I wonder if there is option in AWK/SED maybe other linux tool to manage it for specified lines. Please explain what your command does in steps.

预期的输出(加密排序的其余部分保持不变):

Expected output (crypto sorted rest intact):

123 345
678 901
bla bla bla
ble ble ble
crypto isakmp key 6 9i42kpfsej09f09j4ofjwiojefiow352okdwofkwkfi address 123.456.789.012
crypto isakmp key 6 9j4ofjwiojefiow352okdwofkwkfi9i42kpfsej09f0 address 123.456.789.012
crypto isakmp key 6 kokofeofepokpfowkfpwjeiofjwiojefiow address 123.456.789.012
crypto isakmp key 6 ofjwiojefiow352okdwofkwkfi9i42kpfsej09f09j4 address 123.456.789.012
ccc ddd eee
fff ggg hhh iii
123 456

推荐答案

不完全理解排序的含义,但这会按字母顺序对加密行进行排序,而其余的保持不变

Don't fully understand what you mean by sorted but this will sort the crypto lines alphabetically and leave the others as they are

sort函数需要GNU awk.

Required GNU awk for the asort function.

awk 'y=/crypto isakmp key 6/{x=1;a[NR]=$0}
     x&&!y{x=asort(a);for(i=1;i<=x;i++)print a[i];x=0};!x' file

123 345
678 901
bla bla bla
ble ble ble
crypto isakmp key 6 9i42kpfsej09f09j4ofjwiojefiow352okdwofkwkfi address 123.456.789.012
crypto isakmp key 6 9j4ofjwiojefiow352okdwofkwkfi9i42kpfsej09f0 address 123.456.789.012
crypto isakmp key 6 kokofeofepokpfowkfpwjeiofjwiojefiow address 123.456.789.012
crypto isakmp key 6 ofjwiojefiow352okdwofkwkfi9i42kpfsej09f09j4 address 123.456.789.012
ccc ddd eee
fff ggg hhh iii
123 456

说明

y=/crypto isakmp key 6/
 #variable y is set to 1 if the line contains this regex, 0 if not
{
 #The following code block within the brackets is executed if y is non zero
x=1
 #Set x to 1(i.e true),done every match because it is less hassle and has no negative 
 #side effects 
a[NR]=$0
 #Create array element in array a with a key of NR(line number,doesn't actually matter what 
 #it is though just has to be unique each line) and a value of $0(the line)
}
 #End that block
x&&!y
 #If x(set in the previous block to 1) is set and y isn't (meaning we have encountered a 
 #crypto line but the one we are currently on isn't a crypto line) then
{
 #Open block like before
x=asort(a)
 #Sort the array a, and set x to the number of elements
for(i=1;i<=x;i++)
 #for each element
print a[i]
 #Print the element , note the loop ends here as we have not enclosed in brackets
x=0
 #Set x to 0(false)
}
 #End block
!x
 #Default action for awk is to print the line if an command returns true, so will print any 
 #line where x is not set or is 0 i.e not crypto lines. We could have also used y'


具有有意义的名称

awk 'InBlock=/crypto isakmp key 6/{Stored=1;Lines[NR]=$0}
     Stored&&!InBlock{
         Count=asort(Lines)
         for(i=1;i<=Count;i++)print Lines[i]
         Stored=0
     }
     !InBlock' file

这篇关于使用awk或sed对以pattern开头的行进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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