为什么此命令仅对其他所有行有效?

笑脸

当我跑步时\ls | xargs -I {} echo {} | sed 'N;s/\n/xxxxxxxxx/',我得到以下信息:

- Books aliasxxxxxxxxxA New Kind of Science
Computability-and-Logic.pdfxxxxxxxxxComputability-and-Logic_k2opt.pdf
Hein J. L. - Prolog Experiments in Discrete Mathematics, Logic, and Computability (2005).pdfxxxxxxxxxHein J. L. - Prolog Experiments in Discrete Mathematics, Logic, and Computability (2005)_k2opt.pdf
How Automated Recommendations Affect the Playlist Creation Behavior of Users.pdfxxxxxxxxxHow Automated Recommendations Affect the Playlist Creation Behavior of Users_k2opt.pdf
Lumanote- A Real-Time Interactive Music Composition Assistant.pdfxxxxxxxxxgeMsearch- Personalized Explorative Music Search.pdf
research_report_dc_02.pdfxxxxxxxxxresearch_report_dc_02_k2opt.pdf
thebookofshaders.pdfxxxxxxxxxthebookofshaders_k2opt.pdf

我不明白为什么输出不是这个:

- Books aliasxxxxxxxxxA New Kind of SciencexxxxxxxxxComputability-and-Logic.pdfxxxxxxxxxComputability-and-Logic_k2opt.pdfxxxxxxxxxHein J. L. - Prolog Experiments in Discrete Mathematics, Logic, and Computability (2005).pdfxxxxxxxxxHein J. L. - Prolog Experiments in Discrete Mathematics, Logic, and Computability (2005)_k2opt.pdfxxxxxxxxxHow Automated Recommendations Affect the Playlist Creation Behavior of Users.pdfxxxxxxxxxHow Automated Recommendations Affect the Playlist Creation Behavior of Users_k2opt.pdf
斯蒂芬·查泽拉斯(Stephane Chazelas)
$ seq 10  | sed 'N;s/\n/+/'
1+2
3+4
5+6
7+8
9+10

N在模式空间中添加下一行,然后使用将该s两行连接起来+,然后sed打印该行,并为下一行输入重复脚本(其中第3和第4行用+...等连接)。

你需要

$ seq 10 | sed 'N;N;N;N;N;N;N;N;N;s/\n/+/g'
1+2+3+4+5+6+7+8+9+10

或在您的sed脚本中使用循环来联接所有行:

$ seq 10 | sed -e :1 -e '$!N;s/\n/+/;t1'
1+2+3+4+5+6+7+8+9+10

请注意,它会将整个输入插入到模式空间中,无法很好地扩展到大文件。

要使用一个字符分隔符连接行,可以使用paste

$ seq 10 | paste -sd + -
1+2+3+4+5+6+7+8+9+10

对于不将整个输入加载到内存中的多字符分隔符:

$ seq 10 | awk -v sep=-+- -vORS= 'NR>1 {print sep}; 1; END {if (NR) print RS}'
1-+-2-+-3-+-4-+-5-+-6-+-7-+-8-+-9-+-10

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么此sed命令仅在其他所有匹配项上起作用?

来自分类Dev

为什么此导入有效?

来自分类Dev

为什么此语法有效?

来自分类Dev

为什么此输入有效

来自分类Dev

为什么此查询有效?

来自分类Dev

为什么同步(此)有效?

来自分类Dev

为什么此通配符有效?

来自分类Dev

为什么同步(此)有效?

来自分类Dev

为什么此命令在我的终端上有效,但在我的代码中无效?

来自分类Dev

为什么定义operator +来调用operator + =比使用其他方法更有效?

来自分类Dev

为什么这些SQL查询之一有效,而其他却无效?

来自分类Dev

为什么这些图标之一有效,而其他图标无效?

来自分类Dev

为什么执行方法有效,但未调用其他延迟作业的事件?

来自分类Dev

为什么此代码有效?[R] [dplyr] [变异]

来自分类Dev

试图了解为什么此Python代码有效

来自分类Dev

为什么此内存管理技巧有效?

来自分类Dev

Typedef-为什么此C ++代码有效?

来自分类Dev

为什么此WCF代理代码有效?

来自分类Dev

为什么此Visual Basic代码有效?

来自分类Dev

为什么此JavaScript不能始终有效?

来自分类Dev

为什么此枚举声明现在有效?

来自分类Dev

为什么此LinkedList排序算法有效?

来自分类Dev

为什么此代码有效?[R] [dplyr] [变异]

来自分类Dev

为什么此子查询有效?

来自分类Dev

为什么此railstutorials的“关注”功能有效?

来自分类Dev

为什么此相对URL有效?

来自分类Dev

为什么此代码有效,而那个无效?

来自分类Dev

为什么此WCF代理代码有效?

来自分类Dev

为什么此语句在Java中有效?