Print one sentence 40 to 50 words end with period in a file

Hi All,

Is there another way to achieve this?
how get short phrase in a sentence with character count of 100 to 155 words end with period but don't end something like 50,000. .

Here's my current script but the output is not good. This will use for my snippets or preview.

grep '.\>.\{100,155\}' trim.txt -m 1
 The quick brown fox jumps over the lazy dog near the bank of the river  The quick brown fox jumps over the P50,000.00 lazy dog near the bank of  the river. The quick brown fox jumps over the lazy dog near the bank of  the river The quick brown fox jumps over the lazy dog near the bank of  the river. 

What I want the output is this:

The quick brown fox jumps over the lazy dog near the bank of the river  The quick brown fox jumps over the P50,000.00 lazy dog near the bank of  the river.
#cat test.txt 

The quick brown fox jumps.
The quick brown fox jumps over the lazy dog near the bank of the river.

The quick brown fox jumps over the lazy dog near the bank of the river The quick brown fox jumps over the P50,000.00 lazy dog near the bank of the river. The quick brown fox jumps over the lazy dog near the bank of the river The quick brown fox jumps over the lazy dog near the bank of the river.

The quick brown fox jumps over the lazy dog near the bank of the riverThe quick brown fox jumps over the lazy dog near the bank of the riverThe quick brown fox jumps over the lazy dog near the bank of the river The quick brown fox jumps over the lazy dog near the bank of the river 
The quick brown fox jumps over the lazy dog near the bank of the river.

sed can do it, for example

sed -n 's/\(.\{99,154\}[^0-9]\.\).*/\1/p' test.txt

Get 99..154 characters followed by a non-digit followed by a dot, match the rest, substitute the entire match by the part that matches the expression in \( \). Suppress the default print, print if match succeeded.

Thanks for the reply, is working but the script print all matches on the range I only need the first match. is there additional to your script to work?, like in

grep -m 1
sed -n '/\(.\{99,154\}[^0-9]\.\).*/ {s//\1/p;q}' test.txt

With Perl:

 perl -nle '/.{99,154}\D\./ and print $& and last' test.txt
1 Like

Thanks very much,

If you could explain little by little much better for the benefit of all the beginners.
sed -n '/\(.\{99,154\}[^0-9]\.\).*/ {s//\1/p;q}' test.txt

Sure.
If it matches the pattern within the slashes execute the actions within the block {}. The block actions are, replace empty for the first matched group and print and quit.

The Perl line does it, almost the same way but in a less cryptic way. $& is the pattern matched.

Let's add a beginning of line anchor `^' to prevent the possibility of matching from the middle of the line.

1 Like

Traditional sed needs a ; before the closing }

sed -n '/^\(.\{99,154\}[^0-9]\.\).*/ {s//\1/p;q;}' test.txt

Because here the substitution always works, you could as well have an unconditional print command:

sed -n '/^\(.\{99,154\}[^0-9]\.\).*/ {s//\1/;p;q;}' test.txt