I am trying to print line 2 to 7th line using head command. But, I get the below errors. Can you guide me on the syntax.
$ head -n 2,7 patterns.txt
head: invalid number of lines: �2,7'
$
$
$ head -n 2 7 patterns.txt
head: cannot open '7' for reading: No such file or directory
==> patterns.txt <==
hello world
hello asia
$
$ head -n 2-7 patterns.txt
head: invalid number of lines: �2-7'
Your solution didn't work for me. Maybe bcoz the head and tail commands I use is GNU.
$ cat patterns.txt
hello world
hello asia
hello europe
hello africa
hello america
hello antartica
hello australia
some more hellos
$
$ head -7 patterns.txt
hello world
hello asia
hello europe
hello africa
hello america
hello antartica
hello australia
$
$
$
$ head -7 patterns.txt | tail +2
tail: cannot open '+2' for reading: No such file or directory
$
$
$ head -7 patterns.txt | tail 2
tail: cannot open '2' for reading: No such file or directory
$
It principally reads: if tail +2 doesn't work for you (it does on my computer), try tail -n+2 . Generally: check the man page for your tool & version to find out what it provides and what it misses.
I would write tail -n +2 (with space). I think this is the "most canonical" form. On very old Unixes, the first variants of head and tail did not provide the -n switch. Then, for some time, I found it supported on several platforms, but marked as obsolete, and -n was favoured. Hence, even if the old form would still work, I would prefer using an explicit -n, simply for compatibility.