Sort by

recency

|

138 Discussions

|

  • + 0 comments

    Using Loop

    while read word;
    do
       echo "${word}" | cut -c3
    done
    

    Shortcut

    cut -b3
    
  • + 0 comments

    for i in {1..100}; do read line echo "$line" | cut -c 3 done

  • + 1 comment

    Does anyone know why the test case 2 if failing for below code:?

    while read -r line; do

    echo "${line:2:1}"
    

    done

  • + 0 comments

    cut -c3

    echo "hello" |cut -c3

    output = l

    cut is a command-line tool used to extract sections from each line of input — by bytes, characters, or fields.

  • + 0 comments
    1. Using cut Command: echo "$line" | cut -c3 Best For: Simple, single-character extraction. Limitation: Only handles one character at a time.
    2. Using Bash String Slicing Command: echo "${line:2:1}" Best For: Efficient, direct slicing of strings. Limitation: Fails on short or empty lines.
    3. Using awk Command: echo "Extra open brace or missing close brace0, 3, 1)}' Best For: More complex extraction with flexibility. Limitation: Overhead for simple tasks.
    4. Using while read Loop Command: while read line; do echo "${line:2:1}"; done Best For: Iterating over multiple lines. Limitation: Fails if lines are too short