readコマンドでファイルや標準入力を読み込む

ファイルを読み込む

 while read line; do
 echo $line
 done < file.txt

CSVファイルを読み込む

 $ cat 1.csv
 a,b,c
 x,y,z
 $ while IFS=, read col1 col2 col3; do echo "$col1-$col2-$col3"; done < ./1.csv
 a-b-c
 x-y-z

findコマンドの結果を標準入力を介して読み込む

 $ ls
 1.txt  2.txt  3.txt
 $ find . -print0 | while read -d $'\0' f; do echo "DEBUG: $f"; done
 DEBUG: .
 DEBUG: ./3.txt
 DEBUG: ./2.txt
 DEBUG: ./1.txt

findコマンドの"-print0"は区切り文字をヌル文字にするオプション。


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS

Last-modified: 2012-01-07 (土) 14:46:43