Basic bash concepts III Text Processing Hackerrank

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

What is cut command

The cut command in UNIX is a command line utility for cutting sections from each line of files and writing the result to output.its is used To divide a file into several parts (columns). It can be used to cut parts of a line by byte position, character and delimiter. It can also be used to cut data from file formats like CSV etc.

Command pattern
cut -f
cut -b Print only the bytes in positions listed in
cut -d
cut -c print character for range or position on every line
we will see it in different questions

Basic bash concepts III Text Processing Hackerrank

Q1.Given N lines of input, print the 3rd character from each line as a new line of output. It is guaranteed that each of the n lines of input will have a 3rd character.

Input Format

A text file containing N lines of ASCII characters.
1<=N<=100

Sample Input

Hello
World
how are you
Sample Output

l
r
w

cut -c 3

Task
Q2. Display the 2nd and 7th character from each line of text.
Sample Input

Hello
World
how are you
Sample Output

e
o
oe

cut -c 2,7

Q3.Display a range of characters starting at the 2nd position of a string and ending at the 7th position (both positions included).
Sample Input

Hello
World
how are you
Sample Output

ello
orld
ow are

cut -c 2-7

Q4.Display the first four characters from each line of text.

Sample Input

Hello
World
how are you
Sample Output

Hell
Worl
how

cut -c 1-4

Q5. Given a tab delimited file with several columns (tsv format) print the first three fields.

Sample Input

1 New York, New York[10] 8,244,910 1 New York-Northern New Jersey-Long Island, NY-NJ-PA MSA 19,015,900 1 New York-Newark-Bridgeport, NY-NJ-CT-PA CSA 22,214,083
2 Los Angeles, California 3,819,702 2 Los Angeles-Long Beach-Santa Ana, CA MSA 12,944,801 2 Los Angeles-Long Beach-Riverside, CA CSA 18,081,569
3 Chicago, Illinois 2,707,120 3 Chicago-Joliet-Naperville, IL-IN-WI MSA 9,504,753 3 Chicago-Naperville-Michigan City, IL-IN-WI CSA 9,729,825
4 Houston, Texas 2,145,146 4 Dallas-Fort Worth-Arlington, TX MSA 6,526,548 4 Washington-Baltimore-Northern Virginia, DC-MD-VA-WV CSA 8,718,083
5 Philadelphia, Pennsylvania[11] 1,536,471 5 Houston-Sugar Land-Baytown, TX MSA 6,086,538 5 Boston-Worcester-Manchester, MA-RI-NH CSA 7,601,061
Sample Output

1 New York, New York[10] 8,244,910
2 Los Angeles, California 3,819,702
3 Chicago, Illinois 2,707,120
4 Houston, Texas 2,145,146
5 Philadelphia, Pennsylvania[11] 1,536,471

cut -d$'\t' -f1-3

or

cut -f1-3 -s
cut -f-3

According to the cut manual page, TAB is the default delimiter, so you can omit the -d option. Your solution works, too, just pointing out there’s a shorter road. 😉

cut -f1-3
TAB character is the default delimiter while using the cut command.

Q6.Print the characters from thirteenth position to the end.

Sample Input

New York is a state in the Northeastern and Mid-Atlantic regions of the United States.
New York is the 27th-most extensive, the third-most populous populated of the 50 United States.
New York is bordered by New Jersey and Pennsylvania to the south.
About one third of all the battles of the Revolutionary War took place in New York.
Henry Hudson’s 1609 voyage marked the beginning of European involvement with the area.
Sample Output

a state in the Northeastern and Mid-Atlantic regions of the United States.
the 27th-most extensive, the third-most populous populated of the 50 United States.
bordered by New Jersey and Pennsylvania to the south.
ird of all the battles of the Revolutionary War took place in New York.
‘s 1609 voyage marked the beginning of European involvement with the area.

cut -c 13-
cut -b 13-

Q7.Given a sentence, identify and display its fourth word. Assume that the space (‘ ‘) is the only delimiter between words.
Sample Input

Hello
World
how are you
Sample Output

Hello
World

cut -d' ' -f4

Q8. Given a sentence, identify and display its first three words. Assume that the space (‘ ‘) is the only delimiter between words.
Sample Input

New York is a state in the Northeastern and Mid-Atlantic regions of the United States.
New York is the 27th-most extensive, the third-most populous populated of the 50 United States.
New York is bordered by New Jersey and Pennsylvania to the south.
About one third of all the battles of the Revolutionary War took place in New York.
Henry Hudson’s 1609 voyage marked the beginning of European involvement with the area.
Sample Output

New York is
New York is
New York is
About one third
Henry Hudson’s 1609

cut -d' ' -f1-3

Q9.Given a tab delimited file with several columns (tsv format) print the fields from second fields to last field.

cut -d$'\t' -f2-
cut -f2- -s
cut -f2-

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

Leave a reply:

Your email address will not be published.