Bash脚本用于存储来自不同变量的文件中的单词[关闭]


-1

我需要在不同的变量中存储单词表单文件。就像我有一个test.txt包含以下内容的文件:

uname : null empty amarjeet sharma
ver   : null 1.2 empty 1.3 1.4
txn   : null 123 124 empty 125

我们需要的是将冒号前的单词存储在一个变量中,并保留在每行的数组中。

示例:uname存储在var中,ramaining存储在value[]数组中

var = uname
value[] ={null, empty, amarjeet, sharma}
var1 = ver
value1[] = {null, 1.2, empty, 1.3, 1.4}
etc

您的文件只包含三行?
赛勒斯2015年

@Cyrus没先生!!!!! 它可以根据需要包含许多行和许多列。
Abhijatya Singh 2015年

@AbhijatyaSingh你要求的是可能的,但鉴于bash的能力,它是非常尴尬的。您可能希望更多地了解您正在做的事情,以便人们可以建议更合适的数据设计或更合适的语言。
约翰1024年

@ John1024先生!!!! 我根据其他团队给出的测试文件输入生成了xml文件。
Abhijatya Singh 2015年

Answers:


1

尝试这个:

declare -A hash  # create associative array
declare -a array # create array

# first part: read file to associative array hash
c=0
while IFS=" :" read hash[$c,0] hash[$c,1]; do
  ((c++))
done < filename

# second part: print hash and array
for ((i=0;i<$c;i++)); do
  # create array from hash with part right from :
  array=(${hash[$i,1]})
  echo "${hash[$i,0]} ->  ${array[@]}"
done

输出:

uname  - > null空amarjeet sharma
ver  - > null 1.2空1.3 1.4
txn  - > null 123 124空125
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.