What is Head command
head makes it easy to output the first part of files by default, it prints the first 10 lines of each FILE.
it will display first 10 lines of file
head -20 filename
it will display first 20 lines of file
head -n 15 file1 file2
it will displays only the first 15 lines of both files.
head -c 24 file1
it will display only the first twentyfour bytes (characters) of file1. Newlines count as a single character, so if head prints out a newline, it will count it as a byte.
head -n 3K file1
it will display the first 3,000 lines of file1.
head -c 6M file1
it will display the first six megabytes.
If a dash is specified for the filename, head reads from standard input rather than a regular file.
What is tail command
tail command does something like its name. It displays the last few lines of a file. It is mostly used for viewing log file updates as these updates are appended to the log files.
By default print last 10 lines.
are pattern for it. we will understand them later by examples below.
Basic bash concepts IV Text Processing Hackerrank
Q1. Display the first 20 lines of an input file.
head -n 20
Q2. Display the first 20 characters of an input file.
head -c 20
Q3.Display the lines (from line number 12 to 22, both inclusive) of a given text file.
head -n22 | tail -n11
head -22 | tail -11
Let’s look at each component 1 by one | piping .
This will grab the first 22 lines of the input.
Now let’s pipe the output (the 22 lines) from the head command to the tail…
You will get the 11 lines from the bottom of the head command. So you get lines 12-22. The head command gives you the upper limit while the tail command gives you the lower limit.
this can be done using sed also. sed -n ‘12,22p’
awk ‘NR>=12 && NR<=22'
Q4.Display the last 20 lines of an input file.
tail -n 20
Q5.Display the last 20 characters of an input file.
tail -c 20
All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme