Search code examples
linuxnewlinecut

prevent newline in cut command


Is it possible to cut a string without a line break?

printf 'test.test' prints the test.test without a newline.

But if I cut the output with printf 'test.test' | cut -d. -f1 there's a newline behind test.


Solution

  • There are many ways. In addition to isedev and fedorqui's answers, you could also do:

    • perl -ne '/^([^.]+)/ && print $1' <<< "test.test"
    • cut -d. -f1 <<< "test.test" | tr -d $'\n'
    • cut -d. -f1 <<< "test.test" | perl -pe 's/\n//'
    • while read -d. i; do printf "%s" "$i"; done <<< "test.test