Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 1 down vote accepted

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
share|improve this answer
    
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. –  Masi Jun 5 at 17:11
    
@Masi sure, done. –  terdon Jun 5 at 17:23

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'
share|improve this answer

Try this:

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

\textbf{2. Lorem}
ipsun
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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