移动行以跟随文件中的另一行
我得到一个文件,其中有这样的行:
check=('78905905f5a4ed82160c327f3fd34cba')
我希望能够移动这一行以遵循如下所示的行:
files=('somefile.txt')
数组,但有时可以跨越多行,例如:
files=('somefile.txt'
'file2.png'
'another.txt'
'andanother...')
text
in between
check=('78905905f5a4ed82160c327f3fd34cba'
'5277a9164001a4276837b59dade26af2'
'3f8b60b6fbb993c18442b62ea661aa6b')
数组/line 始终以 ) 结尾,并且其间的任何文本都不会包含右括号。
我得到了一些建议,awk 可以做到这一点:
awk '/files/{
f=0
print $0
for(i=1;i<=d;i++){ print a[i] }
g=0
delete a # remove array after found
next
}
/check/{ f=1; g=1 }
f{ a[++d]=$0 }
!g' file
但这只会跨越一行。有人告诉我扩大搜索范围:
awk '/source/ && /\)$/{
f=0
print $0
for(i=1;i<=d;i++){ print a[i] }
g=0
delete a # remove array after found
next
}
/md5sum/ && /\)$/{ f=1; g=1 }
f{ a[++d]=$0 }
!g'
只是学习 awk,所以我很感激这方面的帮助。或者如果有其他工具可以做到这一点,我想听听。有人告诉我“ed”了这些类型的功能。
I got a file that has a line in the file like this:
check=('78905905f5a4ed82160c327f3fd34cba')
I'd like to be able to move this line to follow a line that looks like this:
files=('somefile.txt')
The array though at times that can span multiple lines, for example:
files=('somefile.txt'
'file2.png'
'another.txt'
'andanother...')
text
in between
check=('78905905f5a4ed82160c327f3fd34cba'
'5277a9164001a4276837b59dade26af2'
'3f8b60b6fbb993c18442b62ea661aa6b')
The array/line always ends in a ) and no text in between will contain a closed parenthesis.
I got some advice that awk can do this:
awk '/files/{
f=0
print $0
for(i=1;i<=d;i++){ print a[i] }
g=0
delete a # remove array after found
next
}
/check/{ f=1; g=1 }
f{ a[++d]=$0 }
!g' file
This will only span one line though. I was told to expand the search:
awk '/source/ && /\)$/{
f=0
print $0
for(i=1;i<=d;i++){ print a[i] }
g=0
delete a # remove array after found
next
}
/md5sum/ && /\)$/{ f=1; g=1 }
f{ a[++d]=$0 }
!g'
Just learning awk so I'd appreciate help with this. Or if there is another tool that can do this, I'd like to hear about it. Someone told me that 'ed' these types of capabilities.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先回答你的最后一个问题,是的,
awk
是典型的 Unix 工具,其他候选工具是非常强大的Perl
, < code>Python,或者..我最喜欢的..红宝石
。awk
的优点之一是它始终存在;它是基础系统的一部分。解决此类问题的另一种方法是使用控制ed(1)
或ex(1)
的编辑器脚本。好的,针对修改后的问题的新程序。该程序将根据需要向上或向下移动“检查”行,以便它们跟随“文件”行。
To answer your last question first, yes,
awk
is the typical Unix tool for this, other candidates are the incredibly powerfulPerl
,Python
, or .. my favorite ..Ruby
. One advantage ofawk
is that it's always there; it's part of the base system. Another way to solve this kind of problem is with an editor script that controlsed(1)
orex(1)
.Ok, new program for the revised question. This program will move the "check" lines either up or down as necessary so that they follow the "files" lines.
我打算用 Awk 来做这件事,但看起来你不会真正从中得到任何聪明的东西,它只是相同的逻辑,但伴随着一些 Awk 的痛苦,所以我用 Perl 做了它: )
和输出:)
ta da ?! :D
I looked in to doing this with Awk, but it looked like you wouldn't really get anything clever out of it, it would just be the same logic, but with some Awk pain to go with it, so I did it in Perl :)
And the output :)
ta da ?! :D
以下是如何使用 sed 执行此操作:
这假设“files=...”之后没有右括号,如果有,则需要更高的精度:
编辑:
在 bash 中工作?好吧,试试这个:
这似乎有效,但我不清楚为什么是这个变体,而不是其他一些明显的变体。这种特殊字符的舞蹈始终是正则表达式的问题。
Here's how to do it with sed:
This assumes that there are no right parentheses after the "files=..." If there are then you'll need more precision:
EDIT:
Working in bash? All right, try this:
This seems to work, but it's not clear to me why this variant and not a few other obvious ones. This dance-of-the-special-characters is always a problem with regexs.
@todd,在为您提供 awk 解决方案后,我似乎让您陷入了困境,不是吗? ? :)。
这是另一种方法,这次不使用标志方法。有一些未解决的问题(提示:再次检查模式 p、q 和输出),我将其留给您来整理。
注意:gensub 是 gawk 特有的,所以如果你有 gawk,那么
输出就可以了
@todd, I seem to have left you in the lurch after providing you the awk solution haven't i. ? :).
here's another method, this time not using method of flags. there are some loose ends (hint: check the patterns p,q and output again) that i leave it to you to tidy up.
NB: gensub is specific to gawk so if you have gawk, then that's alright
output
这可能对你有用:
This might work for you: