Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Could you please tell me how could I extract the first N (e.g. 30) words of each paragraph in a text? Maybe with a regex?

share|improve this question

closed as not a real question by slhck May 15 '13 at 18:52

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
What Operating System are you using? What do you mean by "text" is this a plain text (.txt) file or something more complex like a .doc or pdf? Are paragraphs separated by a blank line? Are they indented? Could you post an example of the text you will work with? –  terdon May 15 '13 at 16:46
    
Regex is the technology you need, but it you'll need a tool that supports it. (Notepad++, PHP, JavaScript, SED, etc) –  BrianAdkins May 15 '13 at 16:46

3 Answers 3

As I said in my comment, a lot of important information is missing. The following will extract the first 30 words of each paragraph from a simple text file and will work as is in any *nix (Linux, OSX, Unix etx).

As an example, I will save this text as file.txt :

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla at diam commodo turpis dictum porttitor. Nunc velit massa, porttitor sit amet rutrum vel, imperdiet eget sem. Pellentesque a neque porttitor elit fringilla pretium. Sed sed felis quam. Pellentesque pellentesque lorem non libero feugiat sollicitudin. In ut consequat felis. Phasellus sed arcu mi, vitae dictum arcu. Quisque lectus massa, tempus vitae elementum nec, adipiscing ut risus.

Donec in lacus urna, sed dictum lectus. Donec pharetra quam sed augue ornare aliquam. Aenean mollis velit eu justo scelerisque elementum. Aenean at lorem at elit vestibulum malesuada. Aliquam sollicitudin volutpat massa et convallis. Ut eget ipsum vitae dolor ullamcorper consectetur. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Quisque elit nisi, volutpat quis malesuada ac, condimentum in quam. Morbi sagittis varius felis in aliquam. Nullam interdum tempor lorem a bibendum. Cras lacinia rhoncus massa ac tempor.

Pellentesque fringilla, ante a hendrerit iaculis, mauris sem placerat felis, vitae ultrices nisl lorem ac ligula. Sed viverra nunc quis dui dictum a porta tellus semper. Aenean felis sapien, dictum et luctus quis, mattis at massa. Nam lacus magna, suscipit at consectetur ut, mollis at sem. Nam euismod mi a nulla rutrum placerat. Nulla dignissim facilisis turpis et faucibus. Donec libero eros, venenatis congue volutpat id, fringilla in felis. Ut vulputate, tellus sed malesuada varius, dolor ligula elementum leo, eu faucibus nulla erat at nisi.

Aliquam erat volutpat. Nulla convallis, leo sit amet placerat lacinia, nisl lectus tempor mi, id pharetra ipsum sapien varius nulla. Nam mollis, nulla at molestie gravida, neque libero consequat odio, nec aliquam tellus arcu ullamcorper quam. Etiam a ligula nec augue dignissim elementum. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas facilisis hendrerit sodales. Sed lobortis tincidunt accumsan. In nunc massa, varius in vehicula at, placerat sit amet elit.

Fusce sed dui ante. Mauris purus est, rhoncus in cursus sit amet, pretium porttitor magna. Sed dapibus, nisl in hendrerit hendrerit, purus libero accumsan lectus, at gravida erat sem a ligula. Phasellus accumsan est non magna sagittis iaculis a eget elit. Vestibulum posuere massa quis neque pharetra at elementum justo condimentum. Donec malesuada enim a nulla mattis auctor. Morbi scelerisque, neque hendrerit lobortis eleifend, turpis quam adipiscing arcu, convallis accumsan lectus neque vitae eros. Maecenas sapien magna, fringilla eu pharetra hendrerit, varius vitae turpis.

Running this little Perl script will print the 1st 30 words of each paragraph (paragraphs need empty lines between them):

$ perl -e 'while(<>){
     chomp; $par.=$_; 
     if (/^\s*$/){
        @a=split(/\s/,join("",$par)); 
        print "@a[0..29]\n\n";
        $par="";
        next;
     }
   } 
 @a=split(/\s/,join("",$par)); 
 print "@a[0..29]\n";' file.txt | fold -s

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla at diam commodo 
turpis dictum porttitor. Nunc velit massa, porttitor sit amet rutrum vel, 
imperdiet eget sem. Pellentesque a neque porttitor

Donec in lacus urna, sed dictum lectus. Donec pharetra quam sed augue ornare 
aliquam. Aenean mollis velit eu justo scelerisque elementum. Aenean at lorem at 
elit vestibulum malesuada. Aliquam sollicitudin

Pellentesque fringilla, ante a hendrerit iaculis, mauris sem placerat felis, 
vitae ultrices nisl lorem ac ligula. Sed viverra nunc quis dui dictum a porta 
tellus semper. Aenean felis sapien, dictum

Aliquam erat volutpat. Nulla convallis, leo sit amet placerat lacinia, nisl 
lectus tempor mi, id pharetra ipsum sapien varius nulla. Nam mollis, nulla at 
molestie gravida, neque libero consequat odio,

Fusce sed dui ante. Mauris purus est, rhoncus in cursus sit amet, pretium 
porttitor magna. Sed dapibus, nisl in hendrerit hendrerit, purus libero 
accumsan lectus, at gravida erat sem a

There are many many ways of doing this, some simpler some more complex. If you update your question with your specific requirements I will update my answer.

share|improve this answer

In the text editor Sublime Text 2, you can use the following regular expression to match the first 30 words of each paragraph in a plain text file:

^((([^ \n]+) ?){1,30})

Screenshot

To remove everything else, search for ^((([^ \n]+) ?){1,30}).* instead and replace with $1. This discards everything after the first 30 words in each paragraph.

share|improve this answer
    
That assumes that all paragraphs consist of a single line with no \n right? –  terdon May 15 '13 at 18:18
    
@terdon True. This is a limitation over ST2's other text commands (Expand Selection to Paragraph, Wrap Paragraph, etc.). –  Daniel Beck May 15 '13 at 18:23

As terdon points out, your problem is quite vaguely specified. If we assume that "a text" contains paragraphs which are separated by blank lines, and "a word" is a sequence of word characters as defined in the regex flavour being used, the following example (in Python) might get you started (text from here):

>>> import re
>>> pattern = r"(?:^|\n\n)((?:\W*\w+){,30})"
>>> for x in re.findall(pattern, text):
...     print(x)
...     print("---")
... 
Loop quantum gravity (LQG) is a theory that attempts to describe the
quantum properties of gravity. It is also a theory of quantum space
and quantum time, because, as discovered
---
More precisely, space can be viewed as an extremely fine fabric or
network "woven" of finite loops. These networks of loops are called
spin networks. The evolution of a spin
---
Today LQG is a vast area of research, developed in several directions,
which involves about 50 research groups world wide.[1] They all share
the basic physical assumptions and the
---
Several research directions study the physical consequences of the
theory. Among these, the most developed is the application of LQG to
cosmology, called Loop quantum cosmology (LQC). LQC applies LQG
---
>>> 

The regex used in the code above:

(?:^|\n\n)((?:\W*\w+){,30})

... has several parts. First, (?:^|\n\n) matches either the beginning of the text ^, or a pair of newlines \n\n (a blank line, in other words). Then, a sequence of zero or more non-word characters \W* followed by one or more word characters \w+ is matched between zero and thirty times {,30}. The (?:...) parentheses group the various parts of the regex without allowing them to be "captured" by re.findall(), and the bare (...) parentheses signal that this part of the regex is to be captured.

share|improve this answer

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