Sort by

recency

|

98 Discussions

|

  • + 1 comment

    Okay guys, I understand that at first it seems nonsensical, but yes, it prints the first two because the delimiter ' ' space doesn't exist. However, as we saw earlier, before it was tabs and now it's spaces. The sample doesn't have four words, but if you add them, it prints the fourth. The error is the sample; the command works.

    • + 1 comment

      sheesh, i too kinda found it really confusing on why are the sample test cases like this, and chatGPT was giving a bunch of bullshit reasons, but this makes a lot of sense, thanks man!

      • + 0 comments

        Thank you, my friend, but there are easier ways to solve it. Here are two simpler options: awk '{print$4}'prints whatever is in the fourth position, but it also includes empty spaces. If we want to remove them, we can useawk 'NF >= 4 {print $4}', or more specifically with a delimiter: awk -F ' ' 'NF >= 4 {print$4}', where-F ' 'sets the space as a delimiter. If the delimiter were a tab, a newline, a dash, a semicolon, or a colon, we just replace it within the quotes. For example, for a tab:awk -F '\t' 'NF >= 4 {print $4}'. But it must be done locally because this won't pass the tests. I hope this helps!

  • + 0 comments
    cut -d ' ' -f4
    
  • + 0 comments
    while ISF=' ' read -r -a line; do
    
        if  [ ${#line[@]} -ne 1 ]
        then
            echo ${line[3]}
        else
            echo ${line[0]}
        fi
    done
    
    -r Option
    -r: When the -r option is used, read treats backslashes literally. This means that the backslashes will not be used as escape characters. Without -r, read would interpret backslashes as escape characters, which could be used to escape special characters (like newline).
    -a Option
    -a: The -a option allows you to read the input into an array. This means that each word of the input will be stored as an element in the array. You need to specify the name of the array immediately after the -a option.
    
    
    e
    
  • + 0 comments
    while read lines 
    do 
        echo $lines | cut -d " " -f 4
    done
    
    • -d flag (delimiter flag) for specifying space (" ") as the delimiter
    • -f flag (field flag) has a default delimiter of tab
  • + 0 comments

    cut -d ' ' -f4