Bash-从文件读取行时打印第一和第二列

Bhu*_*esh 0 linux bash shell do-while

我有一个如下的测试文件。(但实际文件有1000多行和许多列)

apple,2
mango,5
coconut,10
Run Code Online (Sandbox Code Playgroud)

我要按以下方式打印此文件。

I have apple and the count is 2
I have mango and the count is 5
I have coconut and the count is 10
Run Code Online (Sandbox Code Playgroud)

我尝试while read line使用awk -F ',' '{print $1}',但即时通讯未获得实际输出。有人可以帮我吗?

anu*_*ava 5

您可以这样使用awk:

awk -F, '{print "I have", $1, "and the count is", $2}' file
Run Code Online (Sandbox Code Playgroud)

I have apple and the count is 2
I have mango and the count is 5
I have coconut and the count is 10
Run Code Online (Sandbox Code Playgroud)

虽然awk推荐,但如果您正在寻找bash循环,请使用:

while IFS=, read -r f c; do
    echo "I have $f and the count is $c"
done < file
Run Code Online (Sandbox Code Playgroud)