We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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. ;)
Oh, I see. If I understand you correctly, your previous solution which didn't work looked like this:
while read line;
doecho$linedone
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.
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
Cut #5
You are viewing a single comment's thread. Return to all 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.
An even simpler solution:
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. ;)
Except it doesn't seem to work
Could you elaborate? Tried running this command a month ago and just now, the solution passes all 3 testcases and works as intended.
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!
Oh, I see. If I understand you correctly, your previous solution which didn't work looked like this:
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 forread line
in the next iteration (writes the second token to theline
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). Thecut
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:
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.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
Except it does. Or u may use cut -f1-3. Up to you.
thnx
it is not working this command is omiting the first line and giving reast of the line as input
Great!!! It works!!! Thank you soo much
Thanks
cut -f-3 - will work only with higher version of cut like 8.28 , this wont work with versions like 8.21
cut -f1,2,3 -d$'\t'