eagleeyetom
eagleeyetom

Reputation: 13

How to wrap a string containing a character with a tag?

Hi I'm a Linux noob and I'd to know how to replace a line starting with "/" with tag.
Here's an example:

/Foo is awesome

and I would like to get

<i>Foo is awesome</i>

I would appreciate any help!

Upvotes: 1

Views: 147

Answers (4)

Ed Morton
Ed Morton

Reputation: 204416

$ awk 'sub("^/","<i>"){$0 = $0 "</i>"} 1' file
<i>Foo is awesome</i>

Upvotes: 0

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed '\#^/# {s//<i>/;s#$#</i>#;}' YourFile
# or (with maybe space before)
sed '\#^ */# {s//<i>/;s#$#</i>#;}' YourFile

another sed way, this allow to work on this line (if something else to do and not simply sourround by tag)

Upvotes: 1

Tom Fenech
Tom Fenech

Reputation: 74685

You could use awk with substr like this:

awk '/^\//{$0 = "<i>" substr($0,2) "</i>"}1' file

When there is a / at the start of the line, append the tags and use substr to remove the slash from the original line. 1 is true so awk does the default action for each line, which is to print the line.

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174826

You could try the below sed command,

$ sed 's~^/\(.*\)~<i>\1</i>~' file
<i>Foo is awesome</i>

Through awk,

$ awk '/^\//{sub(/^\//,"<i>");sub(/$/,"</i>")}1' file
<i>Foo is awesome</i>

Upvotes: 0

Related Questions