简体   繁体   English

Bash脚本:regexp从文本文件中读取数字参数

[英]Bash script: regexp reading numerical parameters from text file

Greetings! 问候!

I have a text file with parameter set as follows: 我有一个参数设置如下的文本文件:


NameOfParameter Value1 Value2 Value3 ... ... NameOfParameter值1值2值3 ... ...


I want to find needed parameter by its NameOfParameter using regexp pattern and return a selected Value to my Bash script. 我想使用regexp模式通过其NameOfParameter查找所需的参数,并将选定的Value返回到我的Bash脚本。 I tried to do this with grep, but it returns a whole line instead of Value . 我试图用grep做到这一点,但是它返回整行而不是Value

Could you help me to find as approach please? 您能帮我找到一种方法吗?

It was not clear if you want all the values together or only one specific one. 尚不清楚是要将所有值组合在一起还是只将一个特定的值组合在一起。 In either case, use the power of cut command to cut the columns you want from a file ( -f 2- will cut columns 2 and on (so everything except parameter name; -d " " will ensure that the columns are considered to be space-separated as opposed to default tab-separated) 在这两种情况下,都可以使用cut命令的功能从文件中剪切所需的列( -f 2-将剪切第2列及以后的列(因此,除参数名称外的所有内容; -d " "将确保将这些列视为空格分隔,而不是默认制表符分隔)

egrep '^NameOfParameter ' your_file | cut -f 2- -d " "

Bash: 重击:

values=($(grep '^NameofParameter '))
echo ${values[0]}    # NameofParameter 
echo ${values[1]}    # Value1
echo ${values[2]}    # Value2
# etc.

for value in ${values[@:1]}    # iterate over values, skipping NameofParameter
do
    echo "$value"
done

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

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