18

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.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
miu
  • 1,234
  • 2
  • 18
  • 34
  • 1
    As I see you comment about a more generic problem, you'd better update the question with what you exactly want to do. – fedorqui Oct 02 '14 at 13:00

3 Answers3

19

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
terdon
  • 3,260
  • 5
  • 33
  • 57
  • Very comprehensive! +1 Is it necessary to use `$'\n'` on `tr -d`? To me, `tr -d '\n'` works fine. – fedorqui Oct 02 '14 at 12:57
  • 1
    @fedorqui huh, apparently not. I think I remember that it used to be but I might be wrong. Perhaps it depends on the `tr` implementation. I did it this way out of habit. – terdon Oct 02 '14 at 12:59
  • What does the `$` (dollar) in `tr -d $'\n'` mean? – AlikElzin-kilaka Jul 20 '16 at 09:15
  • @AlikElzin-kilaka it is not needed for GNU `tr` (the one on Linux machines) but it might be for other systems. It is a way of escaping, some programs can't deal with backslash escapes (things like `\n`, `\t` etc.) so we need to pass them that way. For example, compare `echo \n` and `echo $'\n'`. – terdon Jul 20 '16 at 09:33
  • `$'\n'` is bash string with escape sequences expanded (starts with `$'` and ends with `'`) containing a single newline character `\n`. Those strings work just like `echo -e` would do. Alternatively one could open an ordinary shell string, tap `` key, then close the string, but it would be less concise. – Krzysztof Jabłoński Jan 05 '17 at 17:27
6

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

printf 'test.test' | awk -F. '{printf($1)}'
isedev
  • 18,848
  • 3
  • 60
  • 59
  • Thanks! First I had `awk 'NR==3 {printf "WiFi Signal Strength = " $3*10/7}' /proc/net/wireless | cut -d. -f1; printf "%%\n"` but the `cut` made a newline so I asked for a solution. Nevertheless if there's an easier way to solve it with one awk command, I would prefer it! (e.g. `awk 'NR==3 {print "WiFi Signal Strength = " $3*10/7 "%"}' /proc/net/wireless` and cut the output of `$3*10/7` after a dot) – miu Oct 02 '14 at 12:58
  • 1
    how about: `printf("WiFi Signal Strength = %.0f%%",$3*10/7)` (the last %% corresponds to "%" in your awk code). – isedev Oct 02 '14 at 13:02
  • you should really accept @fedorqui's answer - it corresponds to the actual posted question. I posted my answer as a possible alternative for educational purposes only. – isedev Oct 02 '14 at 13:06
  • You're right! But anyway: thanks a lot! This is what I was looking for. :) (final command: `awk 'NR==3 {printf("WiFi Signal Strength = %.0f%%\n",$3*10/7)}' /proc/net/wireless`) – miu Oct 02 '14 at 13:08
  • @gummiflummi you could also use `printf "%d", $3*10/7` to get the integer. In case you can also post another question so that isedev gets his deserved points. – fedorqui Oct 02 '14 at 13:19
5

No that I know. man cut is quite short and doesn't reflect anything similar.

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

printf '%s' $(cut -d. -f1 <<< "test.test")
fedorqui
  • 275,237
  • 103
  • 548
  • 598