我有两个名为A和B的变量用于文件主题和分数如下
A=contents of file 'subject' which contains "English Hindi Telugu"
B=contents of file 'Scores' which contains "60 60 10"
我想用各自的标记标记主题,即
english ==> 60 hindi ==> 60 telugu ===>10
我实现如下,但它显示了奇怪的结果:
English ==> 60 English ==> 60 English ==> 10 Hindi ==> 60
Hindi ==> 60 Hindi ==> 10 Telugu ==> 60 Telugu ==> 60 Telugu ==> 10
我希望结果如下英文==> 60印地语==> 60泰卢固语==> 10
#!/bin/ksh
A=`cat subject`
B=`cat Scores`
for sub in $A
do
for score in $B
do
echo " $sub ==> $score "
done
done
答案 0 :(得分:2)
如果文件中的单词出现在单独的行中,例如
,这会更容易一些$ cat subject
English
Hindi
Telugu
$ cat Scores
60
60
10
然后使用一些漂亮的Unix哲学:
$ paste subject Scores | sed 's/\t/ ==> /'
English ==> 60
Hindi ==> 60
Telugu ==> 10
paste
实用程序负责打开多个文件并逐行同步读取。
要转换原始文件,请使用以下内容:
$ printf '%s\n' $(cat subject)
English
Hindi
Telugu
答案 1 :(得分:1)
我不确定您的实际用例是什么,但您可以定义两个文件描述符并一起阅读并使用printf
#!/bin/bash
while IFS= read -r subjectVal <&4 && IFS= read -r scoreVal <&3; do
printf "%s%s\t" "$subjectVal"" ==> ""$scoreVal" # To have them all in a single-line
# printf "%s%s\n" "$subjectVal"" ==> ""$scoreVal" # To print them in new-lines
done 4<subject 3<scores
printf "\n"
以./script.sh
运行脚本会产生如下输出: -
English ==> 60 Hindi ==> 60 Telugu ==> 10