Showing posts with label Linux tail. Show all posts
Showing posts with label Linux tail. Show all posts

Friday, 2 August 2019

Linux - head & tail

Linux head and tail commands practical examples and explanation

Similar to cat command, Head and tail commands deal with displaying the content of a file. But they don’t display the entire content of it, instead show a certain number of lines of the file from head & tail part.
In this section, we will see some of the examples for head command and tail command individually.

Let’s start with head command,
Syntax:
head filename
head -n12 file-name

By default, the head command will show top 10 lines of a file.

Example-1:
Show top lines of a file:
We have a file abclearn_lab4 and to look at its top 10 lines.
[rreddy@abclearn abclearn_dir1]$ head abclearn_lab4.txt
this line should replace the existing one
this is the first line in new file
Hi
this information saves in a new file
Hey, this goes into a file
This is another test line
Testing
Success one
0121313121
New line content

To see top 4 lines of a file,
[rreddy@abclearn abclearn_dir1]$ head -n 4 abclearn_lab4.txt
this line should replace the existing one
this is the first line in new file
Hi
this information saves in a new file

Between -n and 4 you can keep the space or remove it. It works in both ways.
head -n4 abclearn_lab4.txt

Practical usage:
The tail command will show the number of lines from the bottom of the file.
Syntax:
tail filename
tail -n number-of-lines file-name

Example-1:
Show bottom lines of a file:
Similar to head command it shows bottom 10 lines of a file by default.
[rreddy@abclearn abclearn_dir1]$ tail abclearn_lab4.txt
How are you
closing this file here
Test line
Test line
Test line
Test line
Test line
Test line
Test line

If we want to see only last 3 lines of a file abclearn_lab4.txt
[rreddy@abclearn abclearn_dir1]$ tail -n 3 abclearn_lab4.txt
How are you

closing this file here.
Note: 
head and tail commands will display even empty lines as part of the output.

Exampe-2:
To see continuous updates of a file:
The tail command has a wonderful feature to show, continuous updates of a file to the bottom of the page.
We can use “-f” option with tail command,
Syntax:
tail -f file-name

[rreddy@abclearn abclearn_dir1]$ tail -f errorfile


Practical usage:
Assume we have a log file and want to observe the content updated dynamically.

Let’s take a file, which is having some 90 lines in it. And I want to display lines from line number 20 to 35. How can we do it?
We can use the combination of head and tail commands to display the output.
[rreddy@abclearn abclearn_dir1]$ head -n35 filename | tail -n15

Wednesday, 31 July 2019

3 WAYS TO GET THE NTH LINE OF A FILE IN LINUX

The need to get/print a particular line of a file on the Linux shell is a common task. Luckily there are various ways to do this. Below are three great ways to get the nth line of a file in Linux.

1. head / tail

Simply using the combination of the head and tail commands is probably the easiest approach. Below is an example using head and tail to read the 25th line of sample_data_1.txt:

2. sed

There are a couple of nice ways to do this with sed. The first is with the p (print) command, and the other is with the d (delete) command. The n option with the print command is used to only print lines explicitly indicated by the command. For example, sed will output the 25th line of sample_data_1.txt with each of the commands below:

3. awk

awk has a built in variable NR that keeps track of file/stream row numbers. awk syntax and idioms can be hard to read, so below are three different ways to print line 25 of sample_data_1.txt file using awk.