如何提取仅与该行中首次出现的特定字符匹配的每一行?

时间:2018-09-21 15:09:58

标签: regex grep

我正在处理bash shell脚本。我有一个变量,其中有很多行。在这里:如何提取与第5列中的字符5相匹配的每一行

declare -a T1 echo '$input' T=$(echo -e "$input" | grep 5) echo "$T"

输入变量内容:

:: find lo(): [5] :: Current Digital = 0.00 4
:: find lo(): [6] :: Current Digital = 0.00 4
:: find lo(): [7] :: Current Digital = 0.00 5
:: find lo(): [8] :: Current Digital = 0.00 4
:: find lo(): [5] :: Current Digital = 0.00 7
:: find lo(): [6] :: Current Digital = 0.00 7
:: find lo(): [7] :: Current Digital = 0.00 15
:: find lo(): [8] :: Current Digital = 0.00 7
:: find lo(): [5] :: Current Digital = 0.00 10
:: find lo(): [6] :: Current Digital = 0.00 10
:: find lo(): [7] :: Current Digital = 0.00 10
:: find lo(): [8] :: Current Digital = 0.00 35
:: find lo(): [5] :: Current Digital = 0.00 13
:: find lo(): [6] :: Current Digital = 0.00 25
:: find lo(): [7] :: Current Digital = 0.00 13
:: find lo(): [8] :: Current Digital = 0.00 13
:: find lo(): [5] :: Current Digital = 0.00 15
:: find lo(): [6] :: Current Digital = 0.00 50
:: find lo(): [7] :: Current Digital = 0.00 16
:: find lo(): [8] :: Current Digital = 0.00 55

预期的T1变量内容:

:: find lo(): [5] :: Current Digital = 0.00 4
:: find lo(): [5] :: Current Digital = 0.00 7
:: find lo(): [5] :: Current Digital = 0.00 10
:: find lo(): [5] :: Current Digital = 0.00 13
:: find lo(): [5] :: Current Digital = 0.00 15

当前T1变量内容:

:: find lo(): [5] :: Current Digital = 0.00 4
:: find lo(): [7] :: Current Digital = 0.00 5
:: find lo(): [5] :: Current Digital = 0.00 7
:: find lo(): [7] :: Current Digital = 0.00 15
:: find lo(): [5] :: Current Digital = 0.00 10
:: find lo(): [8] :: Current Digital = 0.00 35
:: find lo(): [5] :: Current Digital = 0.00 13
:: find lo(): [5] :: Current Digital = 0.00 15
:: find lo(): [6] :: Current Digital = 0.00 50
:: find lo(): [8] :: Current Digital = 0.00 55

1 个答案:

答案 0 :(得分:1)

只要第一个出现的5始终在括号内,此命令应为您提供预期的输出。

T=$(echo -e "$input" | grep '\[5\]'))