简体   繁体   English

用于从shell脚本解析文件中的键/值的代码

[英]Code for parsing a key/value in in file from shell script

I have a file that I need to look up a value by key using a shell script. 我有一个文件,我需要使用shell脚本按键查找值。 The file looks like: 该文件看起来像:

HereIsAKey This is the value

How can I do something like: 我该怎么做:

MyVar=Get HereIsAKey

and then MyVar should equal "This is the value". 然后MyVar应该等于“这就是价值”。 The key has no whitespace and the value should be everything following whitespace after the key. 密钥没有空格,值应该是密钥后面的空格。

如果HereIsAKey在您的文件中是唯一的,请使用grep尝试:

myVar=$(grep -Po "(?<=^HereIsAKey ).*" file)

如果您没有支持与Perl兼容的正则表达式的grep,则以下似乎有效:

VAR=$(grep "^$KEY " file | cut -d' ' -f2-)

If you only need one variable at a time, you can do something like this: 如果您一次只需要一个变量,则可以执行以下操作:

#!/bin/bash
cat file | while read key value; do
  echo $key
  echo $value
done

The problem with this solution: The variables are only valid inside the loop. 此解决方案的问题:变量仅在循环内有效。 So don't try to do $key=$value and use it after the loop. 所以不要尝试做$key=$value并在循环后使用它。

Update: Another way is I/O redirection: 更新:另一种方法是I / O重定向:

exec 3<file
while read -u3 key value; do
  eval "$key='$value'"
done
exec 3<&-
echo "$keyInFile1"
echo "$anotherKey"

If the file is unsorted, lookups will be slow: 如果文件未排序,查找将很慢:

my_var=$( awk '/^HereIsAKey/ { $1=""; print $0; exit}' value-file )

If the file is sorted, you can get a faster lookup with 如果文件已排序,您可以使用更快的查找

my_var=$( look HereIsAkey value-file | cut -d ' ' -f 2- )

I use a property file that is shared across multiple languages, I use a pair of functions: 我使用一个跨多种语言共享的属性文件,我使用了一对函数:

load_properties() {
    local aline= var= value=
    for file in config.properties; do
        [ -f $file ] || continue
        while read aline; do
            aline=${aline//\#*/}
            [[ -z $aline ]] && continue
            read var value <<<$aline
            [[ -z $var ]] && continue
            eval __property_$var=\"$value\"
            # You can remove the next line if you don't need them exported to subshells
            export __property_$var
        done <$file
    done
}

get_prop() {
    local var=$1 key=$2
    eval $var=\"\$__property_$key\"
}

load_properties reads from the config.properties file populating a set of variables __property_... for each line in the file, get_prop then allows the setting of a variable based on loaded properties. load_propertiesconfig.properties文件中读取,为文件中的每一行填充一组变量__property_...然后get_prop允许根据加载的属性设置变量。 It works for most of the cases that are needed. 它适用于大多数需要的情况。

Yes, I do realize there's an eval in there, which makes it unsafe for user input , but it works for what I needed it to do. 是的,我确实意识到那里有一个eval,这使得用户输入不安全 ,但它适用于我需要它做的事情。

get () {
    while read -r key value; do
        if [ "$key" = "$1" ]; then
            echo "$value"
            return 0
        fi
    done
    return 1
}

The two return statements aren't strictly necessary, but provide nice exit codes to indicate success or failure at finding the given key. 这两个返回语句并不是绝对必要的,但提供了很好的退出代码来指示找到给定键的成功或失败。 They can also help distinguish between "the key has a empty string for the value" and "the key was not found". 它们还可以帮助区分“键值为空字符串”和“未找到键”。

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

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