首先是模式BASH排序行

首先是模式BASH排序行

本文介绍了首先是模式BASH排序行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印路由器配置和排序只是开始模式加密ISAKMP密钥6行。重要的是,我要离开了,在同一个地方行,因此所有之前这行后,应该留在同一个地方(不排序)。

I'd like to print router configuration and sort only lines starting with pattern "crypto isakmp key 6". Important thing is that I want to leave that lines in same place so all before and after this lines should stay in same place ( not sorted ).

例CFG文件:

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

然后我想打印SORTED线开始加密ISAKMP密钥6。

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

我已经被很多操作,包括获取第一个和最后一个位置加密ISAKMP密钥6,用的 / 的命令,管理这一点,但它是相当复杂的,我不知道是否有在AWK / SED选项也许其它的Linux工具来管理它指定行。请解释一下你的命令的步骤一样。

I've managed this by many operations including getting 1st 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

问候,
迈克

Greetings,Mike

推荐答案

不完全明白你的排序是什么意思,但是这将加密线按字母顺序排序,离开别人,因为他们是

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

必需GNU AWK的ASORT功能。

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

这篇关于首先是模式BASH排序行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 09:09