【发布时间】:2021-01-24 07:39:16
【问题描述】:
有一个名为 test.txt 的文件包含:
ljlkfjdslkfldjfdsajflkjf word:test1
dflkdjflkdfdjls word:test2
dlkdj word:test3
word:test4
word:NewYork
dljfldflkdflkdjf word:test7
djfkd word:young
dkjflke word:lisa
amazonwle word:NewYork
dlksldjf word:test10
现在我们只需要获取冒号后的字符串,如果结果相同,则打印输出,在本例中为“NewYork”
这是列出元素的脚本,但是当尝试推入数组并比较它时失败了,请告诉我我的错误。
#!/usr/bin/sh
input="test.txt"
cat $input | while read line; do output= $(echo $line | cut -d":" -f2); done
for (( i = 0 ; i < "${#output[@]}" ; i++ ))
{
echo ${output[i]}
}
得到的错误:
./compare.sh
./compare.sh: line 11: test1: command not found
./compare.sh: line 11: test2: command not found
./compare.sh: line 11: test3: command not found
./compare.sh: line 11: test4: command not found
./compare.sh: line 11: NewYork: command not found
./compare.sh: line 11: raghav: command not found
./compare.sh: line 11: young: command not found
./compare.sh: line 11: lisa: command not found
./compare.sh: line 11: NewYork: command not found
./compare.sh: line 11: test10: command not found
【问题讨论】:
-
输入文件如下所示:
-
cat test.txt ljlkfjdslkfldjfdsajflkjf word:test1 dflkdjflkdfdjls word:test2 dlkdj word:test3 word:test4 word:NewYork dljfldflkdflkdjf word:kathy djfkd word:young dkjflke word:lisa amazonwle word:NewYork d:
-
我无法将字符串放入数组然后比较数组元素并打印,请帮助