Prevent newline command in cut command

Is it possible to cut string without breaking the string?

printf 'test.test' prints test.test without a new line.

But if I cut the output with printf 'test.test' | cut -d. -f1 printf 'test.test' | cut -d. -f1 printf 'test.test' | cut -d. -f1 , there will be a line of a new line << 24>.

+6
source share
3 answers

There are many ways. In addition to the answers from isedev and fedorqui, you can also:

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

No, that I know. man cut quite short and does not reflect anything like that.

Instead, you can provide cut output to printf using here-line so that the new line again depends on printf :

 printf '%s' $(cut -d. -f1 <<< "test.test") 
+4
source

If you do not need to use cut , you can achieve the same result with awk :

 printf 'test.test' | awk -F. '{printf($1)}' 
+3
source

Source: https://habr.com/ru/post/976143/


All Articles