小编lak*_*jay的帖子

如何在 bash 中从文本文件创建字典?

我想从一个文本文件在 bash 中创建一个字典,如下所示:

H96400275|A
H96400276|B
H96400265|C
H96400286|D
Run Code Online (Sandbox Code Playgroud)

基本上我想要一个来自这个文件的字典file.txt

KEYS        VALUES
H96400275 = A
H96400276 = B
H96400265 = C
H96400286 = D
Run Code Online (Sandbox Code Playgroud)

我创建了以下脚本:

#!/bin/bash
declare -a dictionary

while read line; do 

  key=$(echo $line | cut -d "|" -f1)
  data=$(echo $line | cut -d "|" -f2)
  dictionary[$key]="$data"
done < file.txt


echo ${dictionary[H96400275]}
Run Code Online (Sandbox Code Playgroud)

但是,这不会打印A,而是打印D。你能帮忙吗?

bash shell dictionary

2
推荐指数
2
解决办法
8438
查看次数

标签 统计

bash ×1

dictionary ×1

shell ×1