I copied some paragraph from a PDF file and want to remove page numbers at the bottom:
Duis diam dolor, iaculis a efficitur vitae, feugiat sed diam. Phasellus porta dolor non mauris
12
imperdiet ante. Etiam volutpat rhoncus massa, ut laoreet elit suscipit sed.
13
Integer quis ultrices turpis. Nunc molestie euismod aliquet.
14
If a line end with dot, its should be merged with next paragraph. I don't know how to combine these two paragraphs.
So final result should like:
Duis diam dolor, iaculis a efficitur vitae, feugiat sed diam. Phasellus porta dolor non mauris imperdiet ante. Etiam volutpat rhoncus massa, ut laoreet elit suscipit sed.
Integer quis ultrices turpis. Nunc molestie euismod aliquet.
I tried
[^.]\n([0-9]+)
on Linux bash, but no luck.
This might work for you (GNU sed):
sed -E ':a;N;s/\n[0-9]+$//;ta;s/([^.])\n/\1 /;ba' file
Append lines, removing any lines with page numbers and any newlines if the previous line does not end with a period.
This will remove any page numbers, join lines inside a paragraph and leave previous paragraphs intact.
This will produce the output you posted from the sample input you provided:
$ awk '!/^[0-9]+$/{buf=buf $0} /\.$/{print buf; buf=""}' file
Duis diam dolor, iaculis a efficitur vitae, feugiat sed diam. Phasellus porta dolor non maurisimperdiet ante. Etiam volutpat rhoncus massa, ut laoreet elit suscipit sed.
Integer quis ultrices turpis. Nunc molestie euismod aliquet.
It'll behave the same way using any awk in any shell on every UNIX box.
Related
I have an OCR text document where paragraphs have been broken into individual lines. I'd like to make them whole paragraphs on a single line again (as per the original PDF).
How can I use regex, or find and replace, to remove the line breaks between two lines of text and replace them with a space?
Eg:
Every line of text is on a newline. I'd like them to be whole paragraphs on a single line.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam vehicula tellus faucibus metus consequat
scelerisque. Maecenas sit amet urna quis ipsum interdum consequat. Praesent elementum libero nec
velit suscipit placerat accumsan vitae lacus. Aliquam erat volutpat. Etiam egestas lectus sed orci
venenatis, ullamcorper gravida elit pulvinar. Pellentesque imperdiet, augue pulvinar sodales dapibus,
tortor magna rutrum nulla, vel ullamcorper mi purus a diam. Ut id odio sed arcu aliquet lobortis.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Donec quam arcu, egestas feugiat eleifend blandit, vulputate non elit. Nulla a erat vel leo maximus
viverra at ac lorem. Nam non imperdiet lorem. Fusce tempor arcu massa, non commodo ligula lobortis
nec. Aliquam sit amet fringilla sapien, non euismod metus. Donec orci mi, sagittis vitae lobortis eu,
aliquet nec libero. Sed sodales magna lacus, pretium lobortis magna varius nec. Pellentesque quis
ipsum viverra orci lobortis egestas. Aliquam porttitor tincidunt ipsum, egestas placerat ante
consectetur in. Morbi porttitor lacus eu augue tincidunt, at aliquet lorem consectetur.
You might be looking for a programatic/dynamic approach for every new scan generated so I'm not sure if this answers your question, but since you have visual studio code in your tags I will answer how to do this in vscode.
Open keyboard shortcuts from File > Preferences > Keyboard shortcuts, and bind editor.action.joinLines to a shortcut of your choice like for example Ctrl + J.
Then go ahead and open the text you are looking to fix in vscode, select it and press that keybinding. You will notice everything will be in 1 line. I hope I helped!
I am using two regular expressions when removing linebreaks from OCR texts.
They can be used in the Find&Replace dialog from VS Code.
Remove linebreaks at lines ending with a hyphen: (?<=\w)- *\n *
Replace remaining linebreaks with whitespace, but keeping blank lines: (?<!\n) *\n *(?!\n).
Note that the * in the regular expression trims whitespace at the end and beginning of the lines.
There is also a Python tool based on Flair called dehyphen that does the job.
In my experience it produces useful results but may take quite long compared to replacing linebreaks with regular expressions.
Is it possible to find all punctuation marks of a given type, only when a key phrase exists?
For example:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer pulvinar ac augue nec auctor. Vestibulum eleifend, sem non placerat porttitor, urna neque pulvinar enim, ut ullamcorper massa libero nec tellus. Sed est massa, congue eu auctor gravida, efficitur sit amet lacus. Nullam tincidunt posuere sollicitudin. Sed ac ullamcorper risus, ac cursus justo. Phasellus vehicula quam nec libero venenatis venenatis. Donec metus erat: maximus in risus eu: imperdiet: dignissim mauris. Aliquam sit amet augue vel ex tincidunt convallis. Morbi a sem neque. Nam tellus dolor, congue in mi eu, laoreet sodales lectus. Fusce sed ullamcorper purus. Nulla facilisi.
For above, as long as "neque" is in the text, I want to find all occurrences of ":"
I've tried something like this without luck:
(.*\neque\b.*)(?!^)([:])
This works well in my system
explanation
extract the given phrase and store it in a variable.
if the phrase exists find the symbol and count its occurrences.
#!/bin/bash
a="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer pulvinar ac augue nec auctor. Vestibulum eleifend, sem non placerat porttitor, urna neque pulvinar enim, ut ullamcorper massa libero nec tellus. Sed est massa, congue eu auctor gravida, efficitur sit amet lacus. Nullam tincidunt posuere sollicitudin. Sed ac ullamcorper risus, ac cursus justo. Phasellus vehicula quam nec libero venenatis venenatis. Donec metus erat: maximus in risus eu: imperdiet: dignissim mauris. Aliquam sit amet augue vel ex tincidunt convallis. Morbi a sem neque. Nam tellus dolor, congue in mi eu, laoreet sodales lectus. Fusce sed ullamcorper purus. Nulla facilisi."
b=$(echo "$a"| grep -o "neque"| head -1)
echo $b
if [ "$b" == "neque" ]
then
number_of_occurences=$(echo "$a"| grep -o ":"| wc -l)
echo "$number_of_occurences"
fi
Your desired action is not clear. What I can read from the highlights in your example, you want to find all words that end in :, but only if the word neque exists anywhere in the text. Assuming that is the case, you can use this regex:
/(?=.*\bneque\b)\w+:/g
Explanation:
(?=.*\bneque\b) - positive lookahead for neque with word boundaries, anywhere in the text; if this fails, the whole regex fails
\w+: - look for a word that is followed by :
use the g to find all occurrences of words followed by :
EDIT: After seeing that the bash tag has been added, here is a shell script version using a shortened string. The first example has the neque keyword, the second one not:
$ echo 'Urna neque metus erat: maximus in risus eu: imperdiet: dignissim.'\
> | egrep '\bneque\b' | egrep -o '\w+:'
erat:
eu:
imperdiet:
$
$ echo 'Urna metus erat: maximus in risus eu: imperdiet: dignissim.'\
> | egrep '\bneque\b' | egrep -o '\w+:'
$
Explanation:
use first egrep to filter by required keyword neque using word boundaries
use second 'egrep' with -o flag to extract words followed by :
I want to search for a string pattern in a line and if found replace the whole line with the matched string pattern.
My string pattern starts with 2 alpha characters and followed with either 5 or 6 numeric characters. Ex. HR12345 or HR123456
Here is sample of how the lines with the pattern looks like.
Class cum accumsan. In. Pellentesque nec magna interdum fusce metus, massa aliquam HR032145
Amet commodo arcu, felis orci Per. Facilisis blandit rhoncus hac porttitor ut duis eu HR32145
Mattis quis magna, suspendisse HR32146 aucibus vel, fames Nonummy molestie penatibus ad.
Nascetur mattis ad egestas et nec HR032111 Penatibus posuere. Posuere.
Inceptos consectetuer neque nullam HR032114. rutrum Eleifend.
Netus tortor conubia parturient sapien interdum adipiscing sociis luctus integer HR032113
HR032112 Mattis erat a ante. Rutrum. Mattis risus fames. Euismod sapien morbi habitasse.
Platea sapien vitae Risus. Erat dictum elit dapibus convallis.
Facilisis ut dis morbi integer fusce dolor Et class Primis iaculis.
Aptent per risus phasellus HR032188
After search replace it should look like
HR032145
HR32145
HR32146
HR032111
HR032114
HR032113
HR032112
Platea sapien vitae Risus. Erat dictum elit dapibus convallis.
Facilisis ut dis morbi integer fusce dolor Et class Primis iaculis.
HR032188
Try the following simple find and replace:
Find:
^.*(HR\d+).*$
Replace:
$1
This replacement will only happen with lines containing HR followed by one or more digits. Hence, the lines which do not have this pattern will not even match, and no replacement will take place there.
Is there a regex to match "all characters including newlines"?
For example, in the regex below, there is no output from $2 because (.+?) doesn't include new lines when matching.
$string = "START Curabitur mollis, dolor ut rutrum consequat, arcu nisl ultrices diam, adipiscing aliquam ipsum metus id velit. Aenean vestibulum gravida felis, quis bibendum nisl euismod ut.
Nunc at orci sed quam pharetra congue. Nulla a justo vitae diam eleifend dictum. Maecenas egestas ipsum elementum dui sollicitudin tempus. Donec bibendum cursus nisi, vitae convallis ante ornare a. Curabitur libero lorem, semper sit amet cursus at, cursus id purus. Cras varius metus eu diam vulputate vel elementum mauris tempor.
Morbi tristique interdum libero, eu pulvinar elit fringilla vel. Curabitur fringilla bibendum urna, ullamcorper placerat quam fermentum id. Nunc aliquam, nunc sit amet bibendum lacinia, magna massa auctor enim, nec dictum sapien eros in arcu.
Pellentesque viverra ullamcorper lectus, a facilisis ipsum tempus et. Nulla mi enim, interdum at imperdiet eget, bibendum nec END";
$string =~ /(START)(.+?)(END)/;
print $2;
If you don't want add the /s regex modifier (perhaps you still want . to retain its original meaning elsewhere in the regex), you may also use a character class. One possibility:
[\S\s]
a character which is not a space or is a space. In other words, any character.
You can also change modifiers locally in a small part of the regex, like so:
(?s:.)
Add the s modifier to your regex to cause . to match newlines:
$string =~ /(START)(.+?)(END)/s;
Yeap, you just need to make . match newline :
$string =~ /(START)(.+?)(END)/s;
You want to use "multiline".
$string =~ /(START)(.+?)(END)/m;
I've struggled with regExp in Perl for some reason from the start and have a quick script i wrote here to count sentences in some text being inputted that won't work. I just get the number 1 back at the end and I know in the file specified there is several so the count should be higher. I can't see the issue...
#!C:\strawberry\perl\bin\perl.exe
#strict
#diagnostics
#warnings
$count = 0;
$file = "c:/programs/lorem.txt";
open(IN, "<$file") || die "Sorry, the file failed to open: $!";
while($line = <IN>)
{
if($line =~ m/^[A-Z]/)
{
$count++;
}
}
close(IN);
print("Sentances count was: ($count)");
The file lorem.txt is here......
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,
I don't know what's in your lorem.txt, but the code that you've given is not counting sentences. It's counting lines, and furthermore it's counting lines that begin with a capital letter.
This regex:
/^[A-Z]/
will only match at the beginning of a line, and only if the first character on that line is capitalized. So if you have a line that looks like it. And then we went... it will not be matched.
If you want to match all capital letters, just remove the ^ from the beginning of the regex.
This does not answer your specific question about regexp, but you could consider using a CPAN module: Text::Sentence. You can look at its source code to see how it defines a sentence.
use warnings;
use strict;
use Data::Dumper;
use Text::Sentence qw(split_sentences);
my $text = <<EOF;
One sentence. Here is another.
And yet another.
EOF
my #sentences = split_sentences($text);
print Dumper(\#sentences);
__END__
$VAR1 = [
'One sentence.',
'Here is another.',
'And yet another.'
];
A google search also turned up: Lingua::EN::Sentence
You are currently counting all lines that begin with a capital letter. Perhaps you intend to count all words that start with a capital letter? If so, try:
m/\W[A-Z]/
(Although this is not a robust count of sentences)
On another note, there is no need to do the file manipulation explicitly. perl does a really good job of that for you. Try this:
$ARGV[ 0 ] = "c:/programs/lorem.txt" unless #ARGV;
while( $line = <> ) {
...
If you do insist on doing an explicit open/close, it is considered bad practice to use raw filehandles. In other words, instead of "open IN...", do "open my $fh, '<', $file_name;"