Cut #8 – HackerRank Solution

In this post, we will solve Cut #8 HackerRank Solution. This problem (Cut #8) is a part of Linux Shell series.

Task

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

Input Format

A text file with lines of ASCII text only. Each line has exactly one sentence.

Constraints

  • 1 <= N <= 100
  • (N is the number of lines of text in the input file)

Output Format

The output should contain N lines. For each input 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

Solution – Cut #8 – HackerRank Solution

#The output should contain N lines. For each input sentence, identify and display its first three words. Assume that the space (' ') is the only delimiter between words. 
cut -d " " -f 1-3

Note: This problem (Cut #8) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *