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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print one sentence 40 to 50 words end with period in a file
# 1  
Old 08-13-2015
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.

Code:
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:
Code:
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.

Code:
#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.


Last edited by lxdorney; 08-13-2015 at 12:51 PM..
# 2  
Old 08-13-2015
sed can do it, for example
Code:
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.
# 3  
Old 08-13-2015
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
Code:
grep -m 1

# 4  
Old 08-14-2015
Quote:
Originally Posted by lxdorney
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
Code:
grep -m 1

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

With Perl:
Code:
 perl -nle '/.{99,154}\D\./ and print $& and last' test.txt

This User Gave Thanks to Aia For This Post:
# 5  
Old 08-14-2015
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
# 6  
Old 08-14-2015
Quote:
Originally Posted by lxdorney
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.
Quote:
Originally Posted by Aia
Code:
sed -n '/^\(.\{99,154\}[^0-9]\.\).*/ {s//\1/p;q}' test.txt

With Perl:
Code:
 perl -nle '/^.{99,154}\D\./ and print $& and last' test.txt


Last edited by Aia; 08-14-2015 at 12:50 AM..
This User Gave Thanks to Aia For This Post:
# 7  
Old 08-14-2015
Traditional sed needs a ; before the closing }
Code:
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:
Code:
sed -n '/^\(.\{99,154\}[^0-9]\.\).*/ {s//\1/;p;q;}' test.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add words in beginning , end after removing a word in a file

My file has the entries like below... /dev/sds /dev/sdak /dev/sdbc /dev/sdbu I want to make the file like below echo 1 > /sys/block/sds/device/rescan echo 1 > /sys/block/sdak/device/rescan echo 1 > /sys/block/sdbc/device/rescan echo 1 > /sys/block/sdbu/device/rescan (2 Replies)
Discussion started by: saravanapandi
2 Replies

2. Shell Programming and Scripting

awk Adding a Period at the end of a line

I started venturing in learning the art of using AWK/GAWK and wanted to simply added a period from line #11 to line #28 or to the end of the file if there is data. So for example: 11 Centos.NM 12 dojo1 13 redhat.5.5.32Bit 14 redhat.6.2.64Bit... (5 Replies)
Discussion started by: metallica1973
5 Replies

3. Shell Programming and Scripting

match sentence and word adn fetch similar words in alist

Hi all, I have ot match sentence list and word list anf fetch similar words in a separate file second file with 2 columns So I want the output shuld be 2 columns like this (3 Replies)
Discussion started by: manigrover
3 Replies

4. Shell Programming and Scripting

Print file end

Hello All, I have file names in a directory that ends this way File Names in the directory abc.log.1.txt abc.log.2.txt abc.log.3.txt I want to print the 1, 2 and 3 in the file names as the first column in my output, and some contents of the files as second and fourth columns. I wrote... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Shell Programming and Scripting

print in incremental order a sentence

Dear help! I want to print The number i is number i let i=1 to 5 output should be like The number 1 is number 1 The number 2 is number 2 The number 3 is number 3 The number 4 is number 4 The number 5 is number 5 Would be gr8 if you mke this with awk Thanks (7 Replies)
Discussion started by: Indra2011
7 Replies

6. Shell Programming and Scripting

Trim the sentence containing colon and period to extract a word in between

Hello All , i am a newbie in korn shell scripting trying to trim a sentence that is parsed into a variable . The format of the sentence has three words that are separated from other by a " : " colon and "." period . Format of the sentence looks like ... (5 Replies)
Discussion started by: venu
5 Replies

7. Shell Programming and Scripting

Need help in script; [ script to control a sentence & its words ]

Hello friends, I am looking for any sed/awk/python script that can identify the position of a character or word in a file. Well, I prefer sed. <space> is a tab space since I actually dont know how to make the forum editor display a space as such. Sample text ----------- ... (3 Replies)
Discussion started by: frozensmilz
3 Replies

8. UNIX for Dummies Questions & Answers

insert period at the end of each line

file1 contains: this is a test this is a test and only a test this is another test this is another test and only another only i'd like my file to look like this: this is a test. this is a test and only a test. this is another test. this is another test and only another only. (6 Replies)
Discussion started by: tjmannonline
6 Replies

9. Shell Programming and Scripting

Print starting 3rd line until end of the file.

Hi, I want to Print starting 3rd line until end of the file. Pls let me know the command. Thanks in advance. (1 Reply)
Discussion started by: smc3
1 Replies

10. Shell Programming and Scripting

how to find capital letter names in a file without finding words at start of sentence

Hi, I want to be able to list all the names in a file which begin with a capital letter, but I don't want it to list words that begin a new sentence. Is there any way round this? Thanks for your help. (1 Reply)
Discussion started by: kev269
1 Replies
Login or Register to Ask a Question