Reputation: 115
I am a newbie, but would like to create a script which does the following.
Suppose I have a file of the form
This is line1
This is line2
This is line3
This is line4
This is line5
This is line6
I would like to replace it in the form
\textbf{This is line1}
This is line2
This is line3
\textbf{This is line4}
This is line5
This is line6
That is, at the start of the paragraph I would like to add a text \textbf{
and end the line with }
. Is there a way to search for double end of lines? I am having trouble creating such a script with sed. Thank you !
Upvotes: 0
Views: 96
Reputation: 203597
Just use awk's paragraph mode:
$ awk 'BEGIN{RS="";ORS="\n\n";FS=OFS="\n"} {$1="\\textbf{"$1"}"} 1' file
\textbf{This is line1}
This is line2
This is line3
\textbf{This is line4}
This is line5
This is line6
Upvotes: 0
Reputation: 8412
An approach using sed
sed '/^$/{N;s/^\(\n\)\(.*\)/\1\\textbf{\2}/};1{s/\(.*\)/\\textbf{\1}/}' my_file
find all lines that only have a newline character and then add the next line to it. ==
^$/{N;s/^\(\n\)\(.*\)/\1\\textbf{\2}/}
mark the line below the blank line and modify it
find the first line in the file and do the same == 1{s/\(.*\)/\\textbf{\1}/}
Upvotes: 0
Reputation: 26667
Using awk you can write something like
$ awk '!f{ $0 = "\\textbf{"$0"}"; f++} 1; /^$/{f=0}' input
\textbf{This is line1}
This is line2
This is line3
\textbf{This is line4}
This is line5
This is line6
What it does?
!f{ $0 = "\\textbf{"$0"}"; f++}
!f
True if value of f
is 0
. For the first line, since the value of f
is not set, will evaluates true. If its true, awk performs tha action part {}
$0 = "\\textbf{"$0"}"
adds \textbf{
and }
to the line
f++
increments the value of f
so that it may not enter into this action part, unless f
is set to zero
1
always True. Since action part is missing, awk performs the default action to print the entire line
/^$/
Pattern matches an empty line
{f=0}
If the line is empty, then set f=0
so that the next line is modfied by the first action part to include the changesUpvotes: 2