Questions tagged [head]

Use for questions on the head command which outputs the first x number of bytes or lines of a file

The head command outputs the first few bytes or lines from a file. If your question also deals combining this command with other text tools, also use the the tag.

122 questions
189
votes
8 answers

cat line X to line Y on a huge file

Say I have a huge text file (>2GB) and I just want to cat the lines X to Y (e.g. 57890000 to 57890010). From what I understand I can do this by piping head into tail or viceversa, i.e. head -A /path/to/file | tail -B or alternatively tail -C…
Amelio Vazquez-Reina
  • 40,169
  • 77
  • 197
  • 294
72
votes
10 answers

How to obtain inverse behavior for `tail` and `head`?

Is there a way to head/tail a document and get the reverse output; because you don't know how many lines there are in a document? I.e. I just want to get everything but the first 2 lines of foo.txt to append to another document.
chrisjlee
  • 8,283
  • 16
  • 49
  • 54
30
votes
4 answers

How to get lines 10 to 100 from a 200 line file into a new file

I have file with 200 lines. I need to extract lines from 10 to 100 and put them into a new file. How do you do this in unix/Linux? What are the possible commands you could use?
sunil
  • 301
  • 1
  • 3
  • 3
26
votes
9 answers

Command to display first few and last few lines of a file

I have a file with many rows, and each row has a timestamp at the starting, like [Thread-3] (21/09/12 06:17:38:672) logged message from code..... So, I frequently check 2 things from this log file. First few rows, that has the global conditions…
mtk
  • 26,802
  • 35
  • 91
  • 130
22
votes
2 answers

How to do `head` and `tail` on null-delimited input in bash?

find command can output names of files as a null-delimited strings (if -print0 is provided), and xargs can consume them with -0 option turned on. But in between, it's hard to manipulate that collection of files - sort command has -z switch, that…
Rogach
  • 6,150
  • 11
  • 38
  • 41
22
votes
3 answers

Negative arguments to head / tail

Variants of this question have certainly been asked several times in different places, but I am trying to remove the last M lines from a file without luck. The second most voted answer in this question recommends doing the following to get rid of…
Amelio Vazquez-Reina
  • 40,169
  • 77
  • 197
  • 294
19
votes
3 answers

How can I work with binary in bash, to copy bytes verbatim without any conversion?

I am ambitiously trying to translate a c++ code into bash for a myriad of reasons. This code reads and manipulates a file type specific to my sub-field that is written and structured completely in binary. My first binary-related task is to copy the…
neurocoder
  • 191
  • 1
  • 1
  • 6
15
votes
3 answers

head eats extra characters

The following shell command was expected to print only odd lines of the input stream: echo -e "aaa\nbbb\nccc\nddd\n" | (while true; do head -n 1; head -n 1 >/dev/null; done) But instead it just prints the first line: aaa. The same doesn't happen…
anton_rh
  • 399
  • 2
  • 9
14
votes
1 answer

Can `head` read/consume more input lines than it outputs?

Given the following 3 scripts: printf 'a\nb\nc\n' > file && { head -n 1; cat; } < file printf 'a\nb\nc\n' | { head -n 1; cat; } { head -n 1; cat; } < <(printf 'a\nb\nc\n') I'd expect the output from each to be: a b c but for some of those, on…
Ed Morton
  • 28,789
  • 5
  • 20
  • 47
14
votes
1 answer

Alternative to 'head' that doesn't exit?

I need a utility that will print the first n lines, but then continue to run, sucking up the rest of the lines, but not printing them. I use it to not overwhelm the terminal with the output of a process that needs to continue to run (it writes…
IttayD
  • 381
  • 2
  • 9
14
votes
2 answers

Command to remove the first N number of lines in input

Background I am running an SSH server and have this user that I want to delete. I cannot delete this user because he is currently running a few processes that I need to kill first. This is the pipeline I am using currently using to find out all the…
wefwefa3
  • 1,345
  • 5
  • 15
  • 27
12
votes
2 answers

Why does `cat`ing a file into itself erase it?

Possible Duplicate: IO redirection and the head command I just wanted to remove all but the first line of a file. I did this: head -1 foo.txt ... and verified that I saw only the first line. Then I did: head -1 foo.txt > foo.txt But instead of…
Nathan Long
  • 1,613
  • 1
  • 13
  • 27
12
votes
3 answers

What's the POSIX way to read an exact number of bytes from a file?

Just hit this problem, and learned a lot from the chosen answer: Create random data with dd and get "partial read warning". Is the data after the warning now really random? Unfortunately the suggested solution head -c is not portable. For folks who…
Low Powah
  • 223
  • 1
  • 2
  • 8
12
votes
3 answers

Compare the heads of two files in bash

I can do diff filea fileb to see the difference between files. I can also do head -1 filea to see the first line of filea or fileb. How can I combine these commands to show the difference between the first line of filea and the first line of fileb?
Martijn Burger
  • 225
  • 2
  • 8
11
votes
5 answers

Using head and tail to grab different sets of lines and saving into same file

So this is for homework, but I will not be asking the specific homework question. I need to use head and tail to grab different sets of line from one file. So like lines 6-11 and lines 19-24 and save them both to another file. I know I can do this…
user2709291
  • 111
  • 1
  • 1
  • 3
1
2 3
8 9