eramm
eramm

Reputation: 241

Adding a character to the beginning *and* end of every line

I have a line of plain text that i need to wrap in a '<' and '/>'.

So I can do this perl -pe 's/^/</' myfile and then do it again substituting the ^ with a $

Is there anyway to do this with one line of code instead of iterating twice over the file ?

Could it be done better in sed or awk ? How ?

Upvotes: 1

Views: 1074

Answers (3)

michael501
michael501

Reputation: 1482

yet another way ( but faster :-) )

   perl -i -ne 'chomp;print "<$_>\n"' myfile

Upvotes: 2

Andrew Cheong
Andrew Cheong

Reputation: 30273

perl -pe 's/^.*$/<$0\/>/mg' myfile

EDIT: The above found incorrect—see comments by @cjm, below.

Upvotes: 0

cjm
cjm

Reputation: 62109

perl -pe 's/^/</; s!$!/>!' myfile

You can use any number of statements; just separate them with a semicolon. And use alternate delimiters when your regex includes a / for clarity.

Upvotes: 3

Related Questions