Header Ad

HackerRank Cut #3 problem solution

In this HackerRank Cut #3 problem-solution Using cut, multiple items can be retrieved with the same command. For instance, to return multiple columns from a line, there are two basic needs:

  1. Multiple columns that are not contiguous
  2. Multiple columns that are contiguous

The following commands split a string on dots, then return columns 1 and 3, then columns 1 through 3. The -d flag specifies the delimiter and the -f flag specifies the column(s) to return. Note that the delimiter is added back in to the results.

echo 100.200.300.400 |cut -d '.' -f 1,3

100.300

echo 100.200.300.400 |cut -d '.' -f 1-3

100.200.300

Read the man pages for concise documentation.

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

HackerRank Cut #3 problem-solution


Problem solution.

#!/bin/bash
while read line; do
  if [[ $line == "" ]]; then
    break;
  else
    echo $line | cut -c2-7
  fi
done


Second solution.

cut -c2-7


Third solution.

cut -c2-7 $1


Post a Comment

0 Comments