Cindy CC
Cindy CC

Reputation: 1

How to replace blank lines with character/strings with sed

I am trying to replace all my blank lines in a txt file with tags, so for example I have:

aaaaa

bbbbb

ccccc

and I want

<a>aaaaa</a><a>bbbbb</a><a>ccccc</a>

I have tried

sed 's/^\s*$/</a><a>/‘ file.txt

but it does not run, what instead should I do? thanks!

Upvotes: 0

Views: 430

Answers (1)

Snehal Patel
Snehal Patel

Reputation: 1330

I have used below awk command and it's working as expected.

awk 'NF{printf "<a>" "%s",$0 "</a>"} END {print ""}' test.txt 

Output :

<a>aaa</a><a>bbbbb</a><a>ccccc</a>

Upvotes: 1

Related Questions