Questions tagged [unix-head]

The Unix `head` command displays the leading lines of a file.

head is a unix command which displays the first lines of a file. It's counterpart which displays the last lines is tail ().

Example: head -n 10 file will output first 10 lines of file.

55 questions
185
votes
9 answers

What's the opposite of head? I want all but the first N lines of a file

Given a text file of unknown length, how can I read, for example all but the first 2 lines of the file? I know tail will give me the last N lines, but I don't know what N is ahead of time. So for a file AAAA BBBB CCCC DDDD EEEE I want…
Nicholas M T Elliott
  • 3,643
  • 2
  • 19
  • 15
60
votes
2 answers

How do I limit (or truncate) text file by number of lines?

I would like to use a terminal/shell to truncate or otherwise limit a text file to a certain number of lines. I have a whole directory of text files, for each of which only the first ~50k lines are useful. How do I delete all lines over 50000?
sjmurphy
  • 686
  • 1
  • 5
  • 6
23
votes
6 answers

PowerShell equivalent for "head -n-3"?

I've been able to track down basic head/tail functionality: head -10 myfile <==> cat myfile | select -first 10 tail -10 myfile <==> cat myfile | select -last 10 But if I want to list all lines except the last three or all lines except the first…
likso
  • 3,370
  • 3
  • 17
  • 18
22
votes
4 answers

How do I use Head and Tail to print specific lines of a file

I want to say output lines 5 - 10 of a file, as arguments passed in. How could I use head and tail to do this? where firstline = $2 and lastline = $3 and filename = $1. Running it should look like this: ./lines.sh filename firstline lastline
user2232423
19
votes
6 answers

Using combination of "head" and "tail" to display middle line of the file in Unix

If I have a file name myownfile.txt which contains 3 lines of text. foo hello world bar I want to display the line in the middle which is hello world by using head and tail command only.
Ali
  • 9,997
  • 20
  • 70
  • 105
19
votes
2 answers

How do I get java to exit when piped to head

I have a java process which prints out a lot of text. Sometimes I just want to see a bit of the text. With normal programs I can just do: $ myprog | head I'll just see 10 lines of output from myprog and it will exit immediately. But with java, if I…
onlynone
  • 7,602
  • 3
  • 31
  • 50
14
votes
4 answers

How to get the last lines of a file except the first 20?

Say I have a file with any number of lines, say, 125. I want to get all the lines except the first n, say, 20. So, I want lines 21–125. Is there a way to do this with with tail/head, or some other tool?
kch
  • 77,385
  • 46
  • 136
  • 148
13
votes
4 answers

find results piped to zcat and then to head

I'm trying to search for a certain string in a lot of gziped csv files, the string is located at the first row and my thought was to get the first row of each file by combining find, zcat and head. But I can't get them to work together. $find .…
furedde
  • 133
  • 1
  • 1
  • 5
7
votes
6 answers

Efficient way to get n middle lines from a very big file

I have a big file around 60GB. I need to get n middle lines of the file. I am using a command with head and tail like tail -m file |head -n >output.txt where m,n are numbers The general structure of the file is like below with set of records…
Mahesh
  • 99
  • 1
  • 5
6
votes
2 answers

Why doesn't "yes | head" hang?

Why doesn't yes | head hang? I thought the system collects all of the result from yes and then pipes it to head, and because yes is an infinite loop, the system hangs. But, it can actually stop and show 10 lines of y. How does the system manage to…
Antares
  • 213
  • 1
  • 8
6
votes
5 answers

head and grep simultaneously

Is there a unix one liner to do this? head -n 3 test.txt > out_dir/test.head.txt grep hello test.txt > out_dir/test.tmp.txt cat out_dir/test.head.txt out_dir/test.tmp.txt > out_dir/test.hello.txt rm out_dir/test.head.txt out_dir/test.tmp.txt I.e.,…
Dnaiel
  • 7,622
  • 23
  • 67
  • 126
5
votes
3 answers

select the second line to last line of a file

How can I select the lines from the second line to the line before the last line of a file by using head and tail in unix? For example if my file has 15 lines I want to select lines from 2 to 14.
femchi
  • 1,185
  • 8
  • 20
  • 37
4
votes
1 answer

Unix head/tail a percentage of lines to an output file

I'm using unix to read through an excel file, sort by one column, and then I want a percentage (lets say the top 10%) of the sorted lines exported to a new .xls file. I have the code below which works fine but I need to do a wc -l prior to this line…
user3470496
  • 141
  • 7
  • 33
4
votes
2 answers

How can I use 'head' in a bash script with a variable?

I've been trying to create a script that can read a certain line off of a file given some variables I've created. SCRIPTNUM=$(tail -1 leet.txt) LINE=$(echo $SCRIPTNUM | python leetSolver.py) PART1=$(head "-$LINE" leet.txt) FLAG=$(printf "$PART1" |…
user3892426
  • 43
  • 1
  • 1
  • 3
3
votes
2 answers

'while head -n 1' curiosities

Some coding experiments, (made while attempting to find a shorter answer to a coding question), led to a few interesting surprises: seq 2 | while head -n 1 ; do : ; done Output (hit Control-C or it'll waste CPU cycles forever): 1 ^C The same, but…
agc
  • 7,973
  • 2
  • 29
  • 50
1
2 3 4