2014-07-27 46 views
0

我有一個逐行讀取文件的腳本。 每一行具有由空格隔開,就像許多列: 1A 1B 1C 1D 2A 2B 2C 2D 3A 3B 3C 3D雖然讀線奇怪的輸出

欲作用在每一行的第一列。 所以我有這個腳本:

#!/bin/sh 
file_name=myfile.txt 

while read line 
do 
ve=`cut -d " " -f1` 
echo "This is $ve" 
done < $file_name 

但輸出是:的

This is 1a 
2a 
3a 

代替

This is 1a 
This is 2a 
This is 3a 

回答

1

你的cut一審吃掉了所有的輸入。

你可能是指

ve=`echo "$line" | cut -d " " -f1` 

我建議你報你的變量也很好:

#!/bin/sh 
file_name=myfile.txt 
while read line; do 
    ve=`echo "$line" | cut -d ' ' -f1` 
    echo "This is $ve" 
done < "$file_name" 
+1

越好,不要使用'cut'可言:'而閱讀已經休息; do'。 – chepner