Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed replace empty line with character

Tags:

bash

sed

awk

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

Original input:

ACTCTATCATC

CTACTATCTATCC

CCATCATCTACTC

...

Desired output:

ACTCTATCATC
>
CTACTATCTATCC
>
CCATCATCTACTC
>
...

Thanks for any help

like image 399
cebach561 Avatar asked Dec 28 '25 17:12

cebach561


1 Answers

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

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

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:

Test:

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

$ are end of line markers

$ awk '!NF{$0=">"}1' file
ACTCTATCATC
>
CTACTATCTATCC
>
CCATCATCTACTC
>
like image 87
jaypal singh Avatar answered Dec 31 '25 15:12

jaypal singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!