Header Ad

HackerRank Head of a Text File #2 problem solution

In this HackerRank Head of a Text File #2 problem solution In this challenge, we practice using the head command to display the first n characters of a text file.

Display the first 20 characters of an input file.

Input Format

A text file.

Output Format

Output the first 20 characters of the text file.

HackerRank Head of a Text File #2 problem solution


Problem solution.

head -c 20


Second solution.

echo "$(head -c 20)"


Third solution.

declare -i len=0
while true; do
read -d '$' line
let "len = $len + ${#line}"
#echo "$len"
if [[ $len -ge 20 ]]; then
echo "${line:0:20}"
fi
if [[ ${#line} -eq 0 ]]; then break; fi
done


Post a Comment

0 Comments