从值有新行的文件中读取键值对

时间:2017-09-08 06:15:57

标签: bash

我有一个文件,其中包含file.txt中的数据

key1=this
is
the
value.

我需要以相同的格式获取变量($ var)中key1的值。 例如。

echo "$var"
this
is
the
value.

我尝试了以下

bash-4.1$ sed '/^\#/d' ./file.txt | grep "key1"  | sed 's/^.*=//'
this

对此提出任何建议。我在key1之前和之后都有密钥

1 个答案:

答案 0 :(得分:0)

在bash中,分配给变量的值在引用时可以是多行。所以你的文件内容应该如下

<input id="tab1" type="radio" name="tabs" checked>
<label for="tab1" class="tb1">home</label>

<input id="tab2" type="radio" name="tabs">
<label for="tab2" class="tb2">about</label>

<input id="tab3" type="radio" name="tabs">
<label for="tab3" class="tb3">FAQ</label>

<input id="tab4" type="radio" name="tabs">
<label for="tab4" class="tb4">contact</label>


<div class="wr">
<section id="content1">
  <p>home</p>
</section>

<section id="content2">
  <p>about</p>
</section>

<section id="content3">
  <p>FAQ</p>
</section>

<section id="content4">
  <p>contact</p>
</section>
</div>

请注意,我们现在是双引号

现在我们可以做一个简单的echo命令并获取格式化的值

key1="this
is
the
value."
相关问题