Skip to main content
Merge Stéphane’s comment.
Source Link
Stephen Kitt
  • 481.7k
  • 60
  • 1.2k
  • 1.4k

head seems like the best tool for the job:

head -n "$N" input.txt > input.log

With sed, you need to specify a command; based on your approach, that would be the p command and the -n option so it doesn’t print the pattern space by default:

sed -n "1,${N}p" input.txt > input.log

but

sed "$N q" input.txt > input.log

would be more efficient, only reading the first N lines and stopping (which is also what head does) instead of reading the complete file (which is what sed "1,${N}p" does).

These approaches only produce the same output for N greater than or equal to 1; if it’s less than 1, the behaviours will differ.

head seems like the best tool for the job:

head -n "$N" input.txt > input.log

With sed, you need the p command and the -n option so it doesn’t print the pattern space by default:

sed -n "1,${N}p" input.txt > input.log

head seems like the best tool for the job:

head -n "$N" input.txt > input.log

With sed, you need to specify a command; based on your approach, that would be the p command and the -n option so it doesn’t print the pattern space by default:

sed -n "1,${N}p" input.txt > input.log

but

sed "$N q" input.txt > input.log

would be more efficient, only reading the first N lines and stopping (which is also what head does) instead of reading the complete file (which is what sed "1,${N}p" does).

These approaches only produce the same output for N greater than or equal to 1; if it’s less than 1, the behaviours will differ.

Source Link
Stephen Kitt
  • 481.7k
  • 60
  • 1.2k
  • 1.4k

head seems like the best tool for the job:

head -n "$N" input.txt > input.log

With sed, you need the p command and the -n option so it doesn’t print the pattern space by default:

sed -n "1,${N}p" input.txt > input.log