次のようにファイルの行を繰り返すことができます。
while read l; do echo $l; done < file
最初の5行だけを繰り返す方法はありますか?
ベストアンサー1
以下を実行してください。
n=5
while IFS= read -ru3 line && (( n-- )); do
printf 'Got this line: "%s"\n' "$line"
done 3< some-file
ただし、テキスト処理が関連している場合は、テキスト処理ツールを使用する方が良いです。
LC_ALL=C sed 's/.*/Got this line: "&"/;5q' < some-file
または:
awk '{print "Got this line: \""$0"\""}; NR == 5 {exit}' < some-file
または:
perl -lne 'print qq(Got this line: "$_"); last if $. == 5' < some-file
関連: