Basic bash concepts IV Text Processing Hackerrank

Before this post you should learn
1. Basic bash hackerrank
2. Shell beginner
3. Bash II chapter
4.Bash III chapter

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.

like

head filename

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.

head -

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.
tail -n
tail -f
tail -c
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 -20
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 .
head -22
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…
tail -11
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'

Tail command

Q4.Display the last 20 lines of an input file.

tail -20
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

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

One comment: On Basic bash concepts IV Text Processing Hackerrank

  • I’m curious to find out what blog system you are utilizing? I’m having some minor security issues with my latest website and I would like to find something more safeguarded. Do you have any recommendations?

Leave a reply:

Your email address will not be published.