简体   繁体   English

使用 xsel 将多行合并为一行

[英]merge multiple lines into a single line using xsel

I want to highlight a paragraph and then do xsel .我想突出显示一个段落,然后执行xsel Often times, the xsel outputs multiple lines很多时候,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

I would like to turn this into a single line:我想把它变成一行:

$ 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

sometimes, the xsel output is already single line.有时, xsel output 已经是单行了。 In this instance, whatever command I put in to convert multiple lines into a single line should ignore this rare moment.在这种情况下,我输入的将多行转换为单行的任何命令都应该忽略这个罕见的时刻。

I would use GNU AWK for this task as follows, let xsel output be我会使用 GNU AWK来完成这个任务,如下所示,让xsel output

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.

then然后

xsel | awk 'BEGIN{RS="[[:space:]]*\n+";ORS=" "}{print}'

output output

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.

Explanation: I set row separator ( RS ) to 0 or more whitespaces followed by 1 or more newline and output row separator ( ORS ) to single space.说明:我将行分隔符 ( RS ) 设置为 0 个或多个空格,后跟 1 个或多个换行符,并将 output 行分隔符 ( ORS ) 设置为单个空格。 This will prevent multiple space in case line have trailing whitespace and in case line is blank.如果行有尾随空格并且行为空白,这将防止出现多个空格。

(tested in gawk 4.2.1) (在 gawk 4.2.1 中测试)

You just have to redirect the output of xsel to some utility like tr for translating each newline character into a blank.您只需将 xsel 的xsel重定向到诸如tr之类的实用程序,即可将每个换行符转换为空白。

xsel | tr '\n' ' '

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM