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
- Use the -b flag to provide a range (Ex: 5-8)
#!/bin/bash
while read n; do
echo $n | cut -b 1-4
done

💬 Comment Section 💬