我要处理的一个任务是,我必须读取文本文件,并将其中的每个单词作为输入&重要的是,我必须使用while或任何其他循环(不使用awk命令)来完成它
我在while循环中尝试过它,它正在读取文件,但我无法找出下一步。
详情如下:
内容文件(源文件)

[root@localhost ~]# cat content.txt
Rantndeep,old spice,100,20
D-mart,toothbrush,30,20
more,sack,300,10

所需输出
[root@localhost ~]# sh parser.sh
Today I went to Rantndeep Store bought old spice For Rs. 100 And paid 20 Rs.as a parking charges
Today I went to D-mart Store bought toothbrush For Rs. 30 And paid 20 Rs.as a parking charges
Today I went to more Store bought sack For Rs. 300 And paid 10 Rs.as a parking charges

我的剧本
[root@localhost ~]# cat p.sh
#/bin/bash
cat content.txt | while read a
do
     echo $a
done

这只是打印上面提到的文件的内容,我想通过使用任何循环编写脚本,这样我就可以将输出作为
[root@localhost ~]# sh parser.sh
Today I went to Rantndeep Store bought old spice For Rs. 100 And paid 20 Rs.as a parking charges
Today I went to D-mart Store bought toothbrush For Rs. 30 And paid 20 Rs.as a parking charges
Today I went to more Store bought sack For Rs. 300 And paid 10 Rs.as a parking charges

最佳答案

你差不多做到了。注意,可以使用read设置多个变量。试用

IFS=, # Because you separate the items using comma instead of space
while read w1 w2 w3 w4
do
  echo "first word: $w1  second word: $w2  last word: $w4"
done < content.txt

你会在每次迭代中看到,w1。。。w4包含content.txt中相应行的4个字段

关于linux - 有什么方法可以读取文本文件并将其中的单词用作输入?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56250881/

10-14 16:11
查看更多