重击-从文件读取行时打印第一和第二列 [英] Bash - While read line from file print first and second column
本文介绍了重击-从文件读取行时打印第一和第二列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下的测试文件。 (但实际文件有1000多个行,并且有很多列)
I have a test file like below. (but the actual file has 1000+ lines and many columns)
apple,2
mango,5
coconut,10
我要按以下方式打印此文件。
I want to print this file as below.
I have apple and the count is 2
I have mango and the count is 5
I have coconut and the count is 10
我在读取行时尝试了
和 awk -F',''{print $ 1}'
,但是我没有得到实际输出。
有人可以帮我吗?
I tried while read line
with awk -F ',' '{print $1}'
, but im not getting the actual output.
Can someone help me on this?
推荐答案
您可以使用 awk
像这样:
awk -F, '{print "I have", $1, "and the count is", $2}' file
I have apple and the count is 2
I have mango and the count is 5
I have coconut and the count is 10
尽管建议使用 awk
,但如果您正在寻找bash循环,则使用:
Though awk
is recommended but if you are looking for a bash loop then use:
while IFS=, read -r f c; do
echo "I have $f and the count is $c"
done < file
这篇关于重击-从文件读取行时打印第一和第二列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文