gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:26:18 24 4
gpt4 key购买 nike

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

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 命令存储该部分之后的元素:

Bayesian Information Criterion:

在名为:The_variable 的变量内。

到目前为止我尝试过:

The_variable="$(grep 'Bayesian Information Criterion:' the_file.txt)"

这给了我所有的 grep 行

echo $The_variable
Bayesian Information Criterion: VT+F+R4

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

为了隔离我尝试过的部分:

sed 's@.*: @@g' $VAR

有什么想法吗?

最佳答案

使用 awk 更容易:

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:

grep -oP 'Bayesian Information Criterion:\h*\K.+' file

VT+F+R4

或者这个sed:

sed -n 's/Bayesian Information Criterion: *//p' file

VT+F+R4

关于bash - 将 grep 存储在变量中并在 bash 中执行 sed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74529005/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com