简体   繁体   English

Bash:从字符串中提取(百分比)可变长度数

[英]Bash: extract (percent) number of variable length from a string

I want to write a little progress bar using a bash script. 我想使用bash脚本编写一些进度条。

To generate the progress bar I have to extract the progress from a log file. 要生成进度条,我必须从日志文件中提取进度。

The content of such a file (here run.log) looks like this: 这样的文件(在此处为run.log)的内容如下所示:

Time to finish 2d 15h, 42.5% completed, time steps left 231856 完成2d 15h的时间,完成42.5%,剩余时间步长231856

I'm now intersted to isolate the 42.5%. 我现在很感兴趣将42.5%的人隔离。 The problem is now that the length of this digit is variable as well as the position of the number (eg 'time to finish' might content only one number like 23h or 59min). 现在的问题是,该数字的长度和数字的位置都是可变的(例如,“完成时间”可能只包含一个数字,例如23h或59min)。

I tried it over the position via 我通过尝试在位置上

echo "$(tail -1 run.log | awk '{print $6}'| sed -e 's/[%]//g')"

which fails for short 'Time to finish' as well as via the %-sign 短暂的“完成时间”以及通过%符号失败

echo "$(tail -1 run.log | egrep -o '[0-9][0-9].[0-9]%')"

Here is works only for digits >= 10%. 仅对> = 10%的数字有效。

Any solution for a more variable nuumber extraction? 对于更多可变数字提取有什么解决方案吗?

====================================================== ================================================== ====

Update: Here is now the full script for the progress bar: 更新:现在是进度条的完整脚本:

#!/bin/bash

# extract % complete from run.log
perc="$(tail -1 run.log | grep -o '[^ ]*%')"

# convert perc to int
pint="${perc/.*}"

# number of # to plot
nums="$(echo "$pint /2" | bc)"

# output
echo -e ""
echo -e "   completed: $perc"
echo -ne "   "
for i in $(seq $nums); do echo -n '#'; done
echo -e ""
echo -e "  |----.----|----.----|----.----|----.----|----.----|"
echo -e "  0%       20%       40%       60%       80%       100%"
echo -e ""
tail -1 run.log
echo -e ""

Thanks for your help, guys! 谢谢你们的帮助!

based on your example 根据你的例子

grep -o '[^ ]*%'

should give what you want. 应该给你想要的。

You can extract % from below command: 您可以从以下命令中提取%:

tail -n 1 run.log | grep -o -P '[0-9]*(\.[0-9]*)?(?=%)'

Explanation: 说明:

grep options:
-o : Print only matching string.
-P : Use perl style regex

regex parts:
[0-9]* : Match any number, repeated any number of times.
(\.[0-9]*)? : Match decimal point, followed by any number of digits. 
              ? at the end of it => optional. (this is to take care of numbers without fraction part.)
(?=%)  :The regex before this must be followed by a % sign. (search for "positive look-ahead" for more details.)

You should be able to isolate the progress after the first comma (,) in your file. 您应该能够隔离文件中第一个comma (,)之后的进度。 ie.you want the characters between , and % ie.you希望之间的字符,并且%

There are many ways to achieve your goal. 有很多方法可以实现您的目标。 I would prefer using cut several times as it is easy to read. 我更喜欢使用cut多次,因为它易于阅读。

cut -f1 -d'%' | cut -f2 -d',' | cut -f2 -d' '

After first cut : 第一次切割后

 Time to finish 2d 15h, 42.5

After second (note space): 秒之后(注意空格):

 42.5

And the last one just to get rid of space, the final result: 而最后一个只是为了摆脱空间,最终的结果是:

42.5

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

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