简体   繁体   English

使用sed或类似方法修改文件中的行

[英]using sed or similar to modify lines in a file

I have a huge file composed of the following: 我有一个巨大的文件由以下组成:

this is text

1234.1234567

this is another text

1234.1234567

and so on 等等

I would like to transfer it to: 我想把它转移到:

this is text:1234.1234567

this is another text:1234.1234567

is this possible using sed? 这可能使用sed吗? or any other similar command? 或任何其他类似的命令?

Thanks 谢谢

If you just want to join lines using : as separator, you could use paste : 如果您只想使用:作为分隔符来连接行,则可以使用paste

paste -d : - - < file.txt

Or using awk : 或者使用awk

awk -v sep=: '{ if (NR % 2 == 0) { print prev sep $0 } else prev = $0 }' file.txt

If you have lines containing just alphabets and other containing floating point numbers, you can do the following: 如果您的行只包含字母和其他包含浮点数的行,则可以执行以下操作:

awk '/[a-zA-Z]+/ {printf "%s:", $0}
     /[0-9.]+/ {print $0}' data

data is the filename. data是文件名。 You can redirect the output to another file. 您可以将输出重定向到另一个文件。

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

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