sed用字符替换空行 [英] sed replace empty line with character

查看:845
本文介绍了sed用字符替换空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用sed将文件中的空白行替换为特定字符?

How do you replace a blank line in a file with a certain character using sed?

我使用了以下命令,但它仍返回原始输入:

I have used the following command but it still returns the original input:

sed 's/^$/>/' filename

原始输入:

ACTCTATCATC

CTACTATCTATCC

CCATCATCTACTC

...

所需的输出:

ACTCTATCATC
>
CTACTATCTATCC
>
CCATCATCTACTC
>
...

感谢您的帮助

推荐答案

这是使用awk的一种方法.如果您有空格或空白行,这将不在乎:

Here is a way with awk. This wouldn't care if you have spaces or blank lines:

awk '!NF{$0=">"}1' file

NF代表字段数.由于空白行或仅包含空格的行没有字段,因此我们使用它来插入您的文本. 1触发条件为true并打印行:

NF stands for number of fields. Since blank lines or lines with just spaces have no fields, we use that to insert your text. 1 triggers the condition to be true and prints the line:

测试:

$ cat -vet file
ACTCTATCATC$
      $
CTACTATCTATCC$
$
CCATCATCTACTC$
       $

$是行尾标记

$ are end of line markers

$ awk '!NF{$0=">"}1' file
ACTCTATCATC
>
CTACTATCTATCC
>
CCATCATCTACTC
>

这篇关于sed用字符替换空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆