简体   繁体   English

功能中的命令不适用于在终端中手动键入的命令

[英]Command in function not works as manualy typed in terminal

I need to parse document for stuff like this: 我需要解析文档,例如:

#00 service.
log: servicelogin
pass: password

Don't worry, file is encrypted and separated from system :) So you see there are 'service' in first line and second, so doing it like this: 不用担心,文件已加密并与系统分开:)因此,您会看到第一行和第二行都有“服务”,因此可以这样进行:

cat /path | grep -A 3 service

Returns 4 lines as it finds service two times. 返回两次发现服务时返回4行。

However, typing in terminal this command: 但是,在终端输入以下命令:

cat /path | grep -A 3 service | grep -A 3 -m 1 "/."

Works like a charm. 奇迹般有效。

But! 但! As I need to type 'service' in the end alias isn't good enough. 由于我需要在最终别名中输入“ service”,所以还不够。 So I created a function: 所以我创建了一个函数:

function do-stuff {
    cat /path | grep -A 3 $1 | grep -A 3 -m 1 "/."
}

And it works like first one without parsing it again. 它就像第一个一样工作,而无需再次解析。

That's it. 而已。 Thank you! 谢谢!

=== edit === ===编辑===

Sure I've tried to combine it like 当然,我尝试将其合并为

cat /path | grep -A 3 -m 1 service

But it's also won't work. 但这也行不通。 Returns only one line. 仅返回一行。

=== edit === ===编辑===

I need to get only 3 lines, not 4 as it finds it again next line. 我只需要获得3行,而不是4行,因为它会在下一行再次找到它。

Your regular expression isn't specific enough. 您的正则表达式不够具体。 Perhaps something like 也许像

grep -A 3 '#.* service\.' /path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM