首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将grep存储在变量中,并在bash中执行sed

将grep存储在变量中,并在bash中执行sed
EN

Stack Overflow用户
提问于 2022-11-22 07:42:33
回答 2查看 29关注 0票数 2

我有一个包含多个内容的文件,如:

代码语言:javascript
复制
Create initial parsimony tree by phylogenetic likelihood library (PLL)... 0.022 seconds
Perform fast likelihood tree search using LG+I+G model...
Estimate model parameters (epsilon = 5.000)
Perform nearest neighbor interchange...
Estimate model parameters (epsilon = 1.000)
1. Initial log-likelihood: -30833.549
Optimal log-likelihood: -30833.420
Proportion of invariable sites: 0.016
Gamma shape alpha: 2.035
Parameters optimization took 1 rounds (0.226 sec)
Time for fast ML tree search: 2.912 seconds
NOTE: ModelFinder requires 64 MB RAM!
ModelFinder will test up to 546 protein models (sample size: 1544) ...
 No. Model         -LnL         df  AIC          AICc         BIC
  1  LG            31317.712    31  62697.424    62698.736    62863.030
  2  LG+I          31170.012    32  62404.024    62405.422    62574.972
  3  LG+G4         30870.402    32  61804.803    61806.201    61975.751
  4  LG+I+G4       30833.404    33  61732.808    61734.294    61909.098
  5  LG+R2         30881.301    33  61828.602    61830.088    62004.892
  6  LG+R3         30831.187    35  61732.374    61734.045    61919.349
Akaike Information Criterion:           VT+F+R4
Corrected Akaike Information Criterion: VT+F+R4
Bayesian Information Criterion:         VT+F+R4
Best-fit model: VT+F+R4 chosen according to BIC
other content....

我想使用bash命令在部件后面存储元素:

代码语言:javascript
复制
Bayesian Information Criterion:

在一个名为: The_variable的变量中。

到目前为止我试过:

代码语言:javascript
复制
The_variable="$(grep 'Bayesian Information Criterion:' the_file.txt)"

这给了我所有的grep线

代码语言:javascript
复制
echo $The_variable
Bayesian Information Criterion: VT+F+R4

但我希望它只存储VT+F+R4部分。

为了隔离这部分,我尝试了:

代码语言:javascript
复制
sed 's@.*: @@g' $VAR

知道吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-11-22 07:45:46

awk中比较容易

代码语言:javascript
复制
awk -F ' *: *' '$1 == "Bayesian Information Criterion" {print $2}' file

VT+F+R4

# to save this in a variable:
myvar=$(awk -F ' *: *' '$1 == "Bayesian Information Criterion" {print $2}' file)

gnu-grep解决方案还将与\K一起工作。

代码语言:javascript
复制
grep -oP 'Bayesian Information Criterion:\h*\K.+' file

VT+F+R4

或者这个sed

代码语言:javascript
复制
sed -n 's/Bayesian Information Criterion: *//p' file

VT+F+R4
票数 4
EN

Stack Overflow用户

发布于 2022-11-22 07:45:39

您可以使用awk打印所需的内容。默认情况下,分隔符是空格,所以在您的情况下,awk '{ print $4 }'将执行这项工作。

代码语言:javascript
复制
➜ echo 'Bayesian Information Criterion: VT+F+R4' | awk '{ print $4 }'
VT+F+R4
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74529005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档