macos 从粘贴板中的输入字符串中去除换行符的最简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3482289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Easiest way to strip newline character from input string in pasteboard
提问by Andrew L
Hopefully fairly straightforward, to explain the use case when I run the following command (OS X 10.6):
希望相当简单,当我运行以下命令(OS X 10.6)时解释用例:
$ pwd | pbcopy
the pasteboard contains a newline character at the end. I'd like to get rid of it.
粘贴板的末尾包含一个换行符。我想摆脱它。
回答by grep
pwd | tr -d '\n' | pbcopy
pwd | tr -d '\n' | pbcopy
回答by Paused until further notice.
printf $(pwd) | pbcopy
or
或者
echo -n $(pwd) | pbcopy
Note that these should really be quoted in case there are whitespace characters in the directory name. For example:
请注意,如果目录名称中有空格字符,则应该真正引用这些字符。例如:
echo -n "$(pwd)" | pbcopy
回答by s4y
I wrote a utility called noeol
to solve this problem. It pipes stdin to stdout, but leaves out the trailing newline if there is one. E.g.
我写了一个实用程序noeol
来解决这个问题。它通过管道标准输入到标准输出,但如果有换行符,则省略尾随的换行符。例如
pwd | noeol | pbcopy
…I aliased copy
to noeol | pbcopy
.
……我别名copy
为noeol | pbcopy
。
Check it out here: https://github.com/Sidnicious/noeol
回答by slm
For me I was having issues with the tr -d '\n'
approach. On OSX I happened to have the coreutils package installed via brew install coreutils
. This provides all the "normal" GNU utilities prefixed with a gin front of their typical names. So head
would be ghead
for example.
对我来说,我的tr -d '\n'
方法有问题。在 OSX 上,我碰巧通过brew install coreutils
. 这提供了所有“普通”GNU 实用程序,g在它们的典型名称前加上前缀。举例head
来说,也是如此ghead
。
Using this worked more safely IMO:
使用这个更安全 IMO:
pwd | ghead -c -1 | pbcopy
You can use od
to see what's happening with the output:
您可以使用od
来查看输出发生了什么:
$ pwd | ghead -c -1 | /usr/bin/od -h
0000000 552f 6573 7372 732f 696d 676e 6c6f 6c65
0000020 696c
0000022
vs.
对比
$ pwd | /usr/bin/od -h
0000000 552f 6573 7372 732f 696d 676e 6c6f 6c65
0000020 696c 000a
0000023
The difference?
区别?
The 00
and 0a
are the hexcodes for a nul and newline. The ghead -c -1
merely "chomps" the last character from the output before handing it off to | pbcopy
.
的00
和0a
是一个NUL和换行符hexcodes。在ghead -c -1
将输出交给| pbcopy
.
$ man ascii | grep -E '\b00\b|\b0a\b'
00 nul 01 soh 02 stx 03 etx 04 eot 05 enq 06 ack 07 bel
08 bs 09 ht 0a nl 0b vt 0c np 0d cr 0e so 0f si