Sort by

recency

|

96 Discussions

|

  • + 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

  • + 0 comments

    Other than using cut -d " " -f 4 you can also use tr ' ' '\t' | cut -f 4

  • + 2 comments

    This exercise has no sense. I know that it works with cut -d' ' -f4 but still the behaviour with lines that don't reach 4 words is absurd, the first word is returned or not depending on its position, and the sample to work with has no sense either: no line has 4 words so no output should be made for any of them.