gpt4 book ai didi

linux - 使用 awk 或 sed 对以模式开头的行进行排序

转载 作者:太空狗 更新时间:2023-10-29 12:01:34 25 4
gpt4 key购买 nike

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

重要的是我想将这些行留在同一个地方,所以这些行前后的所有行都应该保持在同一个地方和顺序(不排序)。

示例输入文件:

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

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

123 345
678 901
bla bla bla
ble ble ble

然后我想打印以 crypto isakmp key 6 开头的 SORTED 行。

最后我想打印其余文件不变(也是随机行数):

ccc ddd eee
fff ggg hhh iii
123 456

我已经通过许多操作来管理它,包括获取 crypto isakmp key 6 的第一个和最后一个位置以及使用 tail/head 命令但是它非常复杂,我想知道 AWK/SED 中是否有选项或者其他 linux 工具可以为指定的行管理它。请按步骤解释您的命令的作用。

预期输出(加密排序的其余部分完好无损):

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

最佳答案

不完全理解排序的意思,但这会按字母顺序对加密行进行排序,而其他行保持原样

asort 函数需要 GNU awk。

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

关于linux - 使用 awk 或 sed 对以模式开头的行进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33914124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com