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

前端 未结 6 1875
心在旅途
心在旅途 2020-11-30 06:44

I have a file that I need to look up a value by key using a shell script. The file looks like:

HereIsAKey This is the value

How can I do so

6条回答
  •  旧巷少年郎
    2020-11-30 07:32

    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- )
    

提交回复
热议问题