Skip to main content
added 258 characters in body
Source Link
Stéphane Chazelas
  • 584.5k
  • 96
  • 1.1k
  • 1.7k

Just do something like:

n=5
while IFS= read -ru3 line && (( n-- )); do
  printf 'Got this line: "%s"\n' "$line"
done 3< some-file

Though, here, if it's about text processing, best would probably be to use a text processing tool:

LC_ALL=C sed 's/.*/Got this line: "&"/;5q' < some-file

Or:

awk '{print "Got this line: \""$0"\""}; NR == 5 {exit}' < some-file

Or:

perl -lne 'print qq(Got this line: "$_"); last if $. == 5' < some-file

Related:

Just do something like:

n=5
while IFS= read -ru3 line && (( n-- )); do
  printf 'Got this line: "%s"\n' "$line"
done 3< some-file

Related:

Just do something like:

n=5
while IFS= read -ru3 line && (( n-- )); do
  printf 'Got this line: "%s"\n' "$line"
done 3< some-file

Though, here, if it's about text processing, best would probably be to use a text processing tool:

LC_ALL=C sed 's/.*/Got this line: "&"/;5q' < some-file

Or:

awk '{print "Got this line: \""$0"\""}; NR == 5 {exit}' < some-file

Or:

perl -lne 'print qq(Got this line: "$_"); last if $. == 5' < some-file

Related:

Source Link
Stéphane Chazelas
  • 584.5k
  • 96
  • 1.1k
  • 1.7k

Just do something like:

n=5
while IFS= read -ru3 line && (( n-- )); do
  printf 'Got this line: "%s"\n' "$line"
done 3< some-file

Related: