1

I have

1. Lorem
He he he

% not sure spelling
2. Lorem 
ipsun

I want to have

\textbf{1. Lorem}
He he he

% not sure spelling
\textbf{2. Lorem}
ipsun

My pseudocode attempt

perl -000pe 's/\n\n\d./; s/\n\d.\n\\textbf\{ /g; s/$/\}/'

which is based on my two previous questions about regex here. I try to match the thing which starts with a number. Replace the beginning of the match and the end of the match.

The code gives me

Backslash found where operator expected at -e line 1, near "s/\n\n\d./; s/\"
Backslash found where operator expected at -e line 1, near "n\"
Backslash found where operator expected at -e line 1, near "n\"
Backslash found where operator expected at -e line 1, near "textbf\"
Backslash found where operator expected at -e line 1, near "$/\"
    (Missing operator before \?)
syntax error at -e line 1, near "s/\n\n\d./; s/\"
Execution of -e aborted due to compilation errors.

How can you bold the given text?

1

Since the -000 makes each "paragraph" into a "line", you can use the classic regular expression anchors (^ and $) to match the beginning and end of each "line". So, in your case, all you need is:

$ perl -000pe 's/^(.+)\n/\\textbf{$1}\n/;' file 
\textbf{1. Lorem}
He he he

\textbf{2. Lorem }
ipsun

Note that the \ needs to be escaped (\\), that's because \ is a special character used to escape others so you also need to use it to escape itself.


If you can have comments as the first line of a paragraph, then this approach fails and you need to bold all lines that start with a number:

perl -pe 's/^\d\..+/\\textbf{$&}/' file
  • I changed the data little more challenging with an initiating comment mark. See above. Can you edit your command to work with it? I think the first match is little different only. Otherwise, the same. – Léo Léopold Hertz 준영 Jun 5 '14 at 17:11
  • @Masi sure, done. – terdon Jun 5 '14 at 17:23
1

Try this:

$ perl -ple '$_ = "\\textbf{$_}" if /^\d/' cat2
\textbf{1. Lorem}
He he he

\textbf{2. Lorem}
ipsun
1

I used the -0777 option which loads the whole file into memory at once. Then, you can replace newlines:

perl -0777 -pe 's/\n(\d\..*)/\n\\textbf{$1}/g'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.