使用 xsel 将多行合并为一行
Posted
技术标签:
【中文标题】使用 xsel 将多行合并为一行【英文标题】:merge multiple lines into a single line using xsel 【发布时间】:2022-01-08 08:58:52 【问题描述】:我想突出显示一个段落,然后执行xsel
。
很多时候,xsel 会输出多行
$ xsel
The TCP/IP protocol suite is so named for two of its most important protocols:
Transmission Control Protocol (TCP) and Internet Protocol (IP). A less used
name for it is the Internet Protocol Suite, which is the phrase used in official
Internet standards documents. In this book, we use the more common, shorter
term, TCP/IP, to refer to the entire protocol suite.
\ No newline at end of selection
我想把它变成一行:
$ xsel
The TCP/IP protocol suite is so named for two of its most important protocols: Transmission Control Protocol (TCP) and Internet Protocol (IP). A less used name for it is the Internet Protocol Suite, which is the phrase used in official Internet standards documents. In this book, we use the more common, shorter term, TCP/IP, to refer to the entire protocol suite.
\ No newline at end of selection
有时,xsel
输出已经是单行。在这种情况下,我输入的将多行转换为单行的任何命令都应该忽略这个罕见的时刻。
【问题讨论】:
【参考方案1】:您只需将xsel
的输出重定向到tr
之类的实用程序,即可将每个换行符转换为空白。
xsel | tr '\n' ' '
【讨论】:
【参考方案2】:我会使用 GNU AWK
来完成这个任务,如下所示,让 xsel
输出
The TCP/IP protocol suite is so named for two of its most important protocols:
Transmission Control Protocol (TCP) and Internet Protocol (IP). A less used
name for it is the Internet Protocol Suite, which is the phrase used in official
Internet standards documents. In this book, we use the more common, shorter
term, TCP/IP, to refer to the entire protocol suite.
然后
xsel | awk 'BEGINRS="[[:space:]]*\n+";ORS=" "print'
输出
The TCP/IP protocol suite is so named for two of its most important protocols: Transmission Control Protocol (TCP) and Internet Protocol (IP). A less used name for it is the Internet Protocol Suite, which is the phrase used in official Internet standards documents. In this book, we use the more common, shorter term, TCP/IP, to refer to the entire protocol suite.
说明:我将行分隔符 (RS
) 设置为 0 个或多个空格,后跟 1 个或多个换行符,并将输出行分隔符 (ORS
) 设置为单个空格。如果行有尾随空格并且行为空白,这将防止出现多个空格。
(在 gawk 4.2.1 中测试)
【讨论】:
以上是关于使用 xsel 将多行合并为一行的主要内容,如果未能解决你的问题,请参考以下文章