You are on page 1of 2

How to print/display the first line of a file?

There are many ways to do this. However the easiest way to display the first lin
e of a file is using the [head] command.
$> head -1 file.txt
No prize in guessing that if you specify [head -2] then it would print first 2 r
ecords of the file.
Another way can be by using [sed] command. [Sed] is a very powerful text editor
which can be used for various text manipulation purposes like this.
$> sed '2,$ d' file.txt
How does the above command work? The 'd' parameter basically tells [sed] to dele
te all the records from display from line 2 to last line of the file (last line
is represented by $ symbol). Of course it does not actually delete those lines f
rom the file, it just does not display those lines in standard output screen. So
you only see the remaining line which is the 1st line.
How to print/display the last line of a file?
The easiest way is to use the [tail] command.
$> tail -1 file.txt
If you want to do it using [sed] command, here is what you should write:
$> sed -n '$ p' test
From our previous answer, we already know that '$' stands for the last line of t
he file. So '$ p' basically prints (p for print) the last line in standard outpu
t screen. '-n' switch takes [sed] to silent mode so that [sed] does not print an
ything else in the output.
How to display n-th line of a file?
The easiest way to do it will be by using [sed] I guess. Based on what we alread
y know about [sed] from our previous examples, we can quickly deduce this comman
d:
$> sed n '<n> p' file.txt
You need to replace <n> with the actual line number. So if you want to print the
4th line, the command will be
$> sed n '4 p' test
Of course you can do it by using [head] and [tail] command as well like below:
$> head -<n> file.txt | tail -1
You need to replace <n> with the actual line number. So if you want to print the
4th line, the command will be
$> head -4 file.txt | tail -1
How to remove the first line / header from a file?
We already know how [sed] can be used to delete a certain line from the output b
y using the'd' switch. So if we want to delete the first line the command should
be:
$> sed '1 d' file.txt
But the issue with the above command is, it just prints out all the lines except
the first line of the file on the standard output. It does not really change th
e file in-place. So if you want to delete the first line from the file itself, y
ou have two options.
Either you can redirect the output of the file to some other file and then renam
e it back to original file like below:
$> sed '1 d' file.txt > new_file.txt
$> mv new_file.txt file.txt
Or, you can use an inbuilt [sed] switch 'i' which changes the file in-place. See
below:
$> sed i '1 d' file.txt
How to remove the last line/ trailer from a file in Unix script?
Always remember that [sed] switch '$' refers to the last line. So using this kno
wledge we can deduce the below command:
$> sed i '$ d' file.txt.
How to remove the last n-th line from a file?
This is bit tricky. Suppose your file contains 100 lines and you want to remove
the last 5 lines. Now if you know how many lines are there in the file, then you
can simply use the above shown method and can remove all the lines from 96 to 1
00 like below:
$> sed i '96,100 d' file.txt # alternative to command [head -95 file.txt]
But not always you will know the number of lines present in the file (the file m
ay be generated dynamically, etc.) In that case there are many different ways to
solve the problem. There are some ways which are quite complex and fancy. But l
et's first do it in a way that we can understand easily and remember easily. Her
e is how it goes:
$> tt=`wc -l file.txt | cut -f1 -d' '`;sed i "`expr $tt - 4`,$tt d" test
As you can see there are two commands. The first one (before the semi-colon) cal
culates the total number of lines present in the file and stores it in a variabl
e called tt. The second command (after the semi-colon), uses the variable and work
s in the exact way as shows in the previous example.
How to check the length of any line in a file?
We already know how to print one line from a file which is this:
$> sed n '<n> p' file.txt
Where <n> is to be replaced by the actual line number that you want to print. No
w once you know it, it is easy to print out the length of this line by using [wc
] command with '-c' switch.
$> sed n '35 p' file.txt | wc c
The above command will print the length of 35th line in the file.txt.
How to get the nth word of a line in Unix?
Assuming the words in the line are separated by space, we can use the [cut] comm
and. [cut] is a very powerful and useful command and it's real easy. All you hav
e to do to get the n-th word from the line is issue the following command:
cut f<n> -d' '
'-d' switch tells [cut] about what is the delimiter (or separator) in the file,
which is space ' ' in this case. If the separator was comma, we could have writt
en -d',' then. So, suppose I want find the 4th word from the below string: A quic
k brown fox jumped over the lazy cat, we will do something like this:
$> echo A quick brown fox jumped over the lazy cat | cut f4 d' '
And it will print fox
How to remove certain lines from a file in Unix?
If you want to remove line <m> to line <n> from a given file, you can accomplish
the task in the similar method shown above. Here is an example:
$> sed i '5,7 d' file.txt
The above command will delete line 5 to line 7 from the file file.txt

You might also like