So obviously I am trying italicize the first word of each paragraph in a huge document. I think its relatively easy to add the prefix but how do I delineate just the first word for the postfix instead of the end of the line? The file should be space delimited.
sed -e 's/^/<i>/' file > file.new
Do I need to run sed twice with different syntax or is this doable with one command? I am outputting to a new file in case I screw up.
Here is an example of a couple lines of the file:
Snapdragon Plant with a two-lipped flower.
Snap-fastener = *press-stud.
Snapper Any of several edible marine fish.
Snappish 1 curt; ill-tempered; sharp. 2 inclined to snap.
I want it to be as below:
<i>Snapdragon</i> Plant with a two-lipped flower.
<i>Snap-fastener</i> = *press-stud.
<i>Snapper</i> Any of several edible marine fish.
<i>Snappish</i> 1 curt; ill-tempered; sharp. 2 inclined to snap.
not all the lines are single, some terms have multiple lines of definition.
Best Answer
Using sed,
<i>
...</i>
.like this:
On this sample input:
The output is:
To break down the pieces of the sed command:
/^[a-zA-Z]/
-- this is an address filter; it means to apply the subsequent command only to lines that match this regular expression. The regular expression requires that a letter (either lower-casea-z
or upper-caseA-Z
) must follow the beginning of the line^
.s!\([^ ]*\)!<i>\1</i>!
-- this is the search and replace command. It uses a delimiter between the search and the replacement; the common delimiter is a forward-slash, but since the replacement text has a forward-slash, I changed the delimiter to an exclamation point!
. The search term has two pieces to it: the capturing parenthesis, which have to be escaped, and the regular expression[^ ]*
, which says: "match anything-except-a-space, zero or more times*
. The replacement text refers back to that captured group with\1
and surrounds it with the HTML tag.To additionally wrap each non-empty line with paragraph tags, add another sed expression:
The additional expression says:
{
group the next two commands together^
with an opening paragraph tag$
with a closing paragraph tag}
end the grouping