for i in cat file
和 while read line的區別
#!/bin/bash
#Auth: andy
#Date: 20191114
#Describe: 統計
word_file="./cui"
###會把每行的單詞做爲新的每一行的輸出
#for i in `cat ./cui`;do
# l=`echo $i|wc -L`
# #echo ${l}
# if [ ${l} -gt 6 ];then
# echo $i
# fi
#done
##單詞還是在本行裏面
a=0
while read line
do
a=$[a+1]
echo "第${a} 行內容"
for i in $line;
do
l=`echo $i|wc -L`
if [ ${l} -gt 6 ];then
echo -n $i
fi
done
echo
done < ./cui
while讀取是按照shell read 分割符
而 IFS 是一種 set 變量,當 shell 處理"命令替換"和"參數替換"時,shell 根據 IFS 的值,默認是 space, tab, newline 來拆解讀入的變量,然後對特殊字符進行處理,最後重新組合賦值給該變量。
直接輸出IFS是看不到的,把它轉化爲二進制就可以看到了,"040"是空格,"011"是Tab,"012"是換行符"\n" 。最後一個 012 是因爲 echo 默認是會換行的。
更改IFS
$ cat 1.txt
1,a
2,b
3,c
4,d
$ cat test.sh
採用逗號分割