'Sort' command #6 Discussions | Linux Shell | HackerRank

Sort by

recency

|

28 Discussions

|

  • + 0 comments
    sort -t $'\t' -k2n
    

    -t$'\t': This option specifies that the field separator should be a tab character\t. The$'\t' syntax is a Bash feature that allows you to represent special characters, such as tabs, in a way that the shell can understand.

    -k2n: This option tells sort to sort the file based on the numeric value of the second column. The 2 specifies the column number, and the n tells sort to sort the column numerically.

  • + 0 comments
    #!/bin/bash
    rm file.txt > stdout.txt 2> stderr.txt
    while IFS= read -r line || [ -n "$line" ]; do
      echo "$line" >> file.txt
    done
    # -n sort numeric
    # -k column sort
    # -t seperator type
    sort -n -k2 -t$'\t'< file.txt
    
  • + 1 comment

    I did not know about 3.1.2.4 ANSI-C Quoting so I wrote:

    awk -F '\t' '{ print $2, $0 }'  | sort -n | awk '{ $1=""; print substr($0, 2) }'
    

    correct answer is `sort -t $'\t' -k2n

    but

  • + 0 comments

    You can find here HackerRank Linux Shell Solutions

    Sort Command #6 HackerRank Solution

  • + 0 comments

    sort -t $'\t' -k 2n