简体   繁体   English

使用bash提取文本文件数

[英]Extract number of text file using bash

I have a variable with the following string as input: 我有一个带有以下字符串作为输入的变量:

x}#guser{padding-bottom:7px !important;text-align:right}.gbh,.gbd{border-top:1px solid #c9d7f1;font-size:1px}.gbh{height:0;position:absolute;top:24px;width:100%}@media all{.gb1{height:22px;margin-right:>12,34 M<.5em;vertical-align:top}#gbar{float:left}}a.gb1,a.gb4{text-decoration:underline !important}a.gb1,a.gb4{color:#00c !important}.gbi .gb4{color:#dd8e27 !important}.gbf .gb4{color:#900 !important} </style><style>body,td,a,p,.h{font-family:arial,sans-serif}body{margin:0;overflow-y:scroll}#gog{padding:3px 8px 0}td{line-height:.8em}.gac_m td{line-height:17px}form{margin-bottom:20px}.h{color:#36c}.q{color:#00c}.ts td{padding:0}.ts{border-collapse:collapse}em{font-weight:bold;font-style:normal}.lst{height:25px;width:496px}.gsfi,.lst{font:18px arial,sans-serif}.gsfs{font:17px arial,sans-serif}.ds{display:inline-box;display:inline-block;margin:3px 0 4px;margin-left:4px}input{font-family:inherit}a.gb1,a.gb2,a.gb3,a.gb4{color:#11c !important}body{background:#fff;color:black}a{color:#11c;text-decoration:none}a:hover,a:active{text-decoration:underline}.fl a{color:#36c}a:visited{color:#551a8b}a.gb1,a.gb4{text-decoration:underline}a.gb3:hover{text-decoration:none}#ghead a.gb2:hover{color:#fff !important}.sblc{padding-top:5px}.sblc a{display:block;margin:2px 0;margin-left:13px;font-size:11px}.lsbb{background:#eee;border:solid 1px;border-color:#ccc #999 #999 #ccc;height:30px}.lsbb{display:block}.ftl,#fll a{display:inline-block;margin:0 12px}.lsb{background:url(/images/nav_logo229.png) 0 -261px repeat-x;border:none;color:#000;cursor:pointer;height:30px;margin:0;outline:0;

Somewhere in the string we can found: 我们可以在字符串的某处找到:

>12,34 M<

Is it possible to extract the number 12,34 and put it into a variable? 是否可以提取数字12,34并将其放入变量中?

I have several of these strings, but the pattern >number M< is always the same. 我有几个这样的字符串,但是模式> number M <总是相同的。

Thank you for suggestions :) 谢谢你的建议:)

you can try reading you input variable to use grep Regex 您可以尝试阅读输入变量以使用grep Regex

echo $variable | grep -Eo "[0-9,]{1,} M"   

See comments: Use <<< avoiding the echo and assign to a variable: 查看注释:使用<<<避免echo并分配给变量:

finalVar=$(grep -Eo '[0-9,]{1,} M' <<< "$variable") 

When you have the pattern in each line, use 每行都有图案时,请使用

resultvar=$(sed -r 's/.*>([0-9]+[,]{0,1}[0-9]+ M)<.*/\1/' <<< "$variable") 

When you more more than 1 line in your input and only one match, use 如果您输入的内容多于1行且只有一项匹配,请使用

resultvar=$(sed -nr 's/.*>([0-9]+[,]{0,1}[0-9]+ M)<.*/\1/p' <<< "$variable") 

When you can have more than one match, please specify the desired result (only the first or last match, an array, a set of variables or an error message). 如果可以有多个匹配项,请指定所需的结果(仅第一个或最后一个匹配项,数组,一组变量或错误消息)。

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

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