Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prevent newline in cut command

Tags:

linux

newline

cut

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.

like image 893
miu Avatar asked Oct 02 '14 12:10

miu


People also ask

How do I get rid of new line in text file?

Open TextPad and the file you want to edit. Click Search and then Replace. In the Replace window, in the Find what section, type ^\n (caret, backslash 'n') and leave the Replace with section blank, unless you want to replace a blank line with other text. Check the Regular Expression box.

How do you echo without newline?

The best way to remove the new line is to add '-n'. This signals not to add a new line. When you want to write more complicated commands or sort everything in a single line, you should use the '-n' option.

How do I get rid of the new line in awk?

Use printf() when you want awk without printing newline.


2 Answers

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
like image 152
terdon Avatar answered Sep 19 '22 18:09

terdon


If you don't have to use cut, you can achieve the same result with awk:

printf 'test.test' | awk -F. '{printf($1)}'
like image 45
isedev Avatar answered Sep 19 '22 18:09

isedev