• + 2 comments

    cut -d $'\t' -f1-3 worked for me. -d only takes only one character so cut -d ' ' -f1-3 wouldn't work. also cut -d "\t" -f1-3 wouldn't work.

    • + 7 comments

      An even simpler solution:

      cut -f-3
      

      According to the cut manual page, TAB is the default delimiter, so you can omit the -d option. Your solution works, too, just pointing out there's a shorter road. ;)

      • + 2 comments

        Except it doesn't seem to work

        • + 2 comments

          Could you elaborate? Tried running this command a month ago and just now, the solution passes all 3 testcases and works as intended.

          • + 1 comment

            You are correct, I was piping the results of an echo into the cut. Running the cut before while read resolved the issue. Thanks for your comment!

            • + 0 comments

              Oh, I see. If I understand you correctly, your previous solution which didn't work looked like this:

              while read line; 
              do
                  echo $line
              done
              
              cut -f-3
              

              Note that the read command reads the line, but breaks the input into tokens (fields) and assigns them to the variables. For this reason, if the line contains multiple tokens, read line saves the first token, not the whole line. The same goes for read line in the next iteration (writes the second token to the line variable), and so on. When you echo the line, token by token, it basically rewrites the input without the necessary delimiters (TABs are replaced by spaces). The cut command by default treats TABs as delimiters, but after the while-loop refabrication it can't find any, therefore it's not able to choose the necessary fields, which results in the wrong output.

              In fact, the while loop in your solution is redundant. You can treat the input as if it was piped to your script, like this:

              cat input.txt | solution.sh
              

              I'm not a HackerRank employee, so I'm not completely sure which way their backend sends the input, but I think it's done using the cat command or in a similar way. The output is automatically printed to stdout, so there's no need to echo anything.

          • + 0 comments

            it doesn't work, anything I try, whether dont mention delimiter or explicitly mention tab delimiter, it always outputs the entire line, something is not right with the inputs

        • + 0 comments

          Except it does. Or u may use cut -f1-3. Up to you.

      • + 0 comments

        thnx

      • + 0 comments

        it is not working this command is omiting the first line and giving reast of the line as input

      • + 0 comments

        Great!!! It works!!! Thank you soo much

      • + 0 comments

        Thanks

      • + 0 comments

        cut -f-3 - will work only with higher version of cut like 8.28 , this wont work with versions like 8.21

    • + 0 comments

      cut -f1,2,3 -d$'\t'