简体   繁体   English

你如何 pipe 并从尾部过滤文本作为脚本中变量的输入?

[英]How do you pipe and filter text from tail as input for a variable in a script?

Backstory背景故事

I am trying to create a script that updates a "device" through the devices cli, but it doesn't accept any form of command following the establishment of an ssh connection.我正在尝试创建一个脚本,通过设备 cli 更新“设备”,但在建立 ssh 连接后它不接受任何形式的命令。 for this reason i have started using screen to logging the output from the device and then attempting to filter the log for relevant info so i can pass commands back to the remote device by stuffing it into screens buffer.(kind of a ramshackled way of doing it but its all i can think of.出于这个原因,我已经开始使用屏幕从设备上记录 output,然后尝试过滤日志中的相关信息,这样我就可以通过将命令填充到屏幕缓冲区来将命令传回远程设备。(有点摇摇欲坠的做事方式它,但它所有我能想到的。

Issue I need to use some combo of grep and sed or awk to filter out one of two outputs i'm looking for respectively "SN12345678" '\w[a-zA-Z]\d{6-10}' and "finished" inside screenlog.2 I've got regex patterns for both of these but i cannot seem to get the right output and assign it to a variable问题我需要使用 grep 和 sed 或 awk 的一些组合来过滤掉我正在寻找的两个输出之一,分别是 "SN12345678" '\w[a-zA-Z]\d{6-10}' 和 "finished “在 screenlog.2 中,我有这两个的正则表达式模式,但我似乎无法获得正确的 output 并将其分配给变量

.screenrc (relevant excerpt) .screenrc(相关摘录)

screen -t script 0 ./script
screen -t local 1 bash
screen -t remote 2 bash
screen -t Shell 3 bash

./script 。/脚本

screen -p 2 -X log on #turns logging on window 2
screen -p 3 -X stuff 'tail-Fn 0 screenlog.2 | #SOMESED Function that i cant figure out'
screen -p 2 -X stuff 'ssh -o "UserKnownHostsFile /dev/null" -o "StrictHostKeyChecking=no" admin@192.168.0.1^M' && echo "Stuffed ssh login -> window 2"
sleep 2 # wait for ssh connection
screen -p 2 -X stuff admin^M && echo "stuffed pw"
sleep 4 # wait for auth
screen -p 2 -X stuff "copy sw ftp://ftpuser:admin@192.168.0.2/dev_uimage-4_4_5-26222^M" && echo "initiated flash"
screen -p 2 -X stuff "copy license ftp://ftpuser:admin@192.168.0.2/$(result of sed from screenlog.2).lic^M" && echo "uploading license"

sorry if this is a bit long winded i've been wracking my brain for the last few days trying to get this to work.抱歉,如果这有点啰嗦,过去几天我一直在绞尽脑汁,试图让它发挥作用。 Thank you for your time!感谢您的时间!

Answer回答

Regular Expression正则表达式

Looking at the example regex you provided, I'm going to assume SN can't just be hardcoded and that it could be uppercase,lowercase,digit for first character and uppercase,lowercase for the second digit, so I think you are looking for:查看您提供的示例正则表达式,我将假设SN不能只是硬编码,并且它可能是第一个字符的大写、小写、数字和第二个数字的大写、小写,所以我认为您正在寻找:

grep -Eo '[[:alnum:]][[:alpha:]][[:digit:]]{6,10}' # Works regardless of computer's local settings
# OR
egrep -o '[[:alnum:]][[:alpha:]][[:digit:]]{6,10}' # Works regardless of computer's local settings

# OR

grep -Eo '[0-9A-Za-z][A-Za-z][0-9]{6,10}'
# OR
egrep -o '[0-9A-Za-z][A-Za-z][0-9]{6,10}'

These are exact conversions of your regular expression (includes the _ as. a possibility of the first character):这些是您的正则表达式的精确转换(包括_作为第一个字符的可能性):

grep -Eo '[[:alnum:]_][[:alpha:]][[:digit:]]{6,10}' # Works regardless of computer's local settings
# OR
grep -Eo '[0-9A-Za-z_][A-Za-z][0-9]{6,10}'
# OR (non-extended regular expressions)
grep -o '[[:alnum:]_][[:alpha:]][[:digit:]]\{6,10\}'
grep -o '[0-9A-Za-z_][A-Za-z][0-9]\{6,10\}'

Reuse the Match重复使用比赛

I don't know how you would assign the output to a variable, but I would just write it to a file and delete the file afterwards (assuming the "script" and "Shell" windows have the same pwd [present working directory]):我不知道你如何将 output 分配给一个变量,但我只是将它写入一个文件然后删除该文件(假设“脚本”和“Shell”windows 具有相同的密码 [当前工作目录]) :

. . .
screen -p 3 -X stuff 'tail -Fn1 screenlog.2 | grep -Eo "[[:alnum:]][[:alpha:]][[:digit:]]{6,10}" >> SerialNumberOrID^M'
. . .
screen -p 2 -X stuff "copy license ftp://ftpuser:admin@192.168.0.2/$(cat SerialNumberOrID).lic^M" && echo "uploading license"
rm -f SerialNumberOrID

Explanation解释

Regular Expression正则表达式

I'm fairly confident that grep , sed , and awk (and most POSIX compliant utilities) don't support \w and \d .我相当有信心grepsedawk (以及大多数符合 POSIX 的实用程序)不支持\w\d Those are Perl-like flags .这些是类似 Perl 的标志 You can pass -E to grep and sed to make them use extended regular expressions (will save you from having to do as much escaping).您可以将-E传递给grepsed以让它们使用扩展的正则表达式(这将使您不必进行尽可能多的转义)。

Command Changes命令更改

Writing the match to a file seemed like the best way to reuse it.将匹配项写入文件似乎是重用它的最佳方式。 Using >> ensures that we append to the file, so that grep will only write the matching expression to the file and won't overwrite it with an empty file.使用>>确保我们将 append 写入文件,这样grep只会将匹配的表达式写入文件,而不会用空文件覆盖它。 This is why it's necessary to delete the file at the end of your script (so that it won't mess up next run and also so you don't have unnecessary files laying around).这就是为什么有必要删除脚本末尾的文件(这样下次运行时它不会搞砸,也不会留下不必要的文件)。 In the license upload command, we use cat to output the contents of the file in-line.在许可证上传命令中,我们使用cat将 output 文件的内容内联。 I also changed the tail command to tail -Fn1 because I'm pretty sure you need to at least have 1 for it to feed a line into grep.我还将 tail 命令更改为tail -Fn1 ,因为我很确定您至少需要 1 才能将一行输入 grep。

Resources资源

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

相关问题 如果javascript被禁用,你如何过滤(和替换)手机#输入你想要(XXX)XXX-XXXX完全,在PHP或正则表达式 - How do you filter (and replace) phone# input if javascript disabled and you want (XXX)XXX-XXXX exactly, in PHP or Regex 如何从文本字符串中删除“并将其替换为”? - How do you remove ’ from a text string and replace it with '? 你如何实现一个好的亵渎过滤器? - How do you implement a good profanity filter? 如何从文件或脚本中的管道中选择多行? - How to select multiple lines from a file or from pipe in a script? 如何使用自定义工具提示文本为无效输入模式而不是“您必须使用此格式:[空白]” - How do I use a custom Tooltip Text for Invalid Input Pattern instead of “You must use this format: [blank]” 如何从bash脚本中的文件中提取文本 - How do I extract text from a file in a bash script 您如何在bash脚本中使用正则表达式 - How do you use regex in a bash script Perl-将输入管道读取为单个变量 - Perl - Read input pipe as single variable 如何使用 python 从一行中剪切文本并将 append 剪切到文本文件中的另一行? - How do you cut text from a line and append it to another line in text file using python? 您如何匹配捕获组/变量的值? - How do you match the value of a capturegroup/variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM