Hey there people on the internet. Here's my solution for Cut-#4 Linux shell challenge from HackerRank. You can find answers to other Linux shell challenges via this link => https://blog.shasec.rocks/post/hackerrank-bash-challs. So let's get started. 

Challenge

Display the first four characters from each line of text.

> Input Format

A text file containing N lines of ASCII characters.  

(N is the number of lines of text in the input file)

> Constraints

1 <= N <= 100

> Output Format

The output should contain N lines. Each line should contain just the first four characters of the corresponding input line.

> Sample Input

Hello
World
how are you

> Sample Output

Hell
Worl
how 

Solution

#!/bin/bash

while read n; do
 echo $n | cut -b 1-4 
done
Blog Image
Figure 1.0: Solution passed all HackerRank tests cases

If you like content like this, please consider buying me a coffee.

Thank you for your support :)


👇 Share this post 👇


💬 Comment Section 💬