searching words & print prefixed string after it


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers searching words & print prefixed string after it
# 1  
Old 05-23-2012
Question searching words & print prefixed string after it

I have a text which I divided them into sentences and now printed them in a rows.
I want to get the list of most of words ( the, and, a) and print 5 words after them (so 6 with the word itself). I have created an acceptfile with those rows and using grep but I have rows that have these words more than once but they are not being divided. Can you please help me?SmilieSmilie
My data for now looks like this
This is a book
The book is on the shelf and the pen is on the table

But I need it to look like this
A book
The book is on the shelf
The shelf and the pen is
And the pen is on the
The pen is on the table
The table

Thank you in advance
A-V
# 2  
Old 05-23-2012
Hi A-V,

One way using perl:
Code:
$ cat infile
This is a book
The book is on the shelf and the pen is on the table
$ cat script.pl
use strict;
use warnings;

my %word = map { $_ => 1 } qw/the and a/;

while ( <> ) {
        chomp;
        my @f = split;
        for my $pos ( 0 .. $#f ) {
                if ( exists $word{ lc $f[ $pos ] } ) {
                        my $last_word = $pos + 5;
                        printf qq[%s %s\n], ucfirst $f[ $pos ], join qq[ ], @f[ $pos + 1 .. ($last_word > $#f ? $#f : $last_word) ]; 
                }
        }
}
$ perl script.pl infile
A book
The book is on the shelf
The shelf and the pen is
And the pen is on the
The pen is on the table
The table

This User Gave Thanks to birei For This Post:
# 3  
Old 05-23-2012
Deart birei,

thank you very much for your replay

I am new in unix and trying to learn doing things with it... not sure will be able to manage another system at the same time unless its just a copy paste Smilie

update: wow, and it was easy to implement and even understand Smilie

thanks a lot

---------- Post updated at 08:00 AM ---------- Previous update was at 07:50 AM ----------

is there any other way to do it without perl?

I just want to learn different things, specially when it comes to Unix

Last edited by A-V; 05-23-2012 at 09:57 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print ALL matching words in a string

Hi. str=" {aaID=z_701; time=2012-10-08 00:00:00.000}; {aaID=S_300; time=2012-10-08 00:00:00.000}]}; ansokningsunderlag={anmaln......} {aaID=x_500; time=2012-10-08 00:00:00.000}]}; ansokningsunderlag={anmaln......}" I want to print: z_701 S_300 x_500 if I use : echo $str | sed -n... (4 Replies)
Discussion started by: freddan25
4 Replies

2. Shell Programming and Scripting

Finding my lost file by searching for words in it

Got a question for you guys...I am searching through a public directory (that has tons of files) trying to find a file that I was working on a longggggg time ago. I can't remember what it is called, but I do remember the content. It should contains words like this: Joe Pulvo botnet zeus... (5 Replies)
Discussion started by: statichazard
5 Replies

3. Shell Programming and Scripting

Awk: Searching for length of words between slash character

Dear UNIX Community, I have a set of file paths like the one below: \\folder name \ folder1 \ folder2 \ folder3 \ folder4 \\folder name \ very long folder name \ even longer name I would like to find the length of the characters (including space) between the \'s. However, I want... (6 Replies)
Discussion started by: vnayak
6 Replies

4. UNIX for Dummies Questions & Answers

Searching for multiple words on a line in any order issue

Hi again I have figured out how to be able to sort through lines in a file with multiple words in any order and display them using this command: cat file | grep -i $OPTION1 | grep -i $OPTION2 | grep -i $OPTION3 OPTION1 is 2008, OPTION2 is Mar, OPTION 3 is Tue Result: Tue Mar 25... (4 Replies)
Discussion started by: semaj
4 Replies

5. Shell Programming and Scripting

Searching for a string in .PDF files inside .RAR & .ZIP archives.

Hi, I have got a large number of .PDF files that are archived in .RAR & ZIP files in various directories and I would like to search for strings inside the PDF files. I would think you would need something that can recursively read directories, extract the .RAR/.ZIP file in memory, read the... (3 Replies)
Discussion started by: lewk
3 Replies

6. Shell Programming and Scripting

Find a string using grep & print the line above or below that.

Hi All, Please tell me how can I Find a string using grep & print the line above or below that in solaris? Please share as I am unable to use grep -A or grep -B as it is not working on Solaris. (10 Replies)
Discussion started by: Zaib
10 Replies

7. Shell Programming and Scripting

Perl searching special words in lines

Hi , i am a new with perl, i want to made a script that find in file rows that start with specil words, as an example a line will start with" ............................................. specialword aaa=2 bbb=5 ............................................. and to put this in a new file... (3 Replies)
Discussion started by: alinalin
3 Replies

8. Shell Programming and Scripting

searching for words between delimeters from the rear

Hi, i need to pick up dates and times from the file names which are of unequal length. The dates and time are delimited by dot. I am interested in getting the strings between the delimeter for fields -3, -4, -5 from behind (rear) so that the out put looks like : 071118.011300.556 I have... (2 Replies)
Discussion started by: oktbabs
2 Replies

9. Shell Programming and Scripting

Searching words in a file containing a pattern

Hi all, I would like to print words in a file seperated by whitespaces containing a specific pattern like "=" e.g. I have a file1 containing strings like %cat file1 The= some= in wish= born <eof> .I want to display only those words containing = i.e The= , some=,wish= ... (5 Replies)
Discussion started by: sree_123
5 Replies

10. UNIX for Dummies Questions & Answers

searching and displaying most commonly used words

Hi guys, i need to search the most commonly occuring words in a file and display their counts of about 30000 words and the words shud not be of typ specified in file 2 e. words like is,for,the,an,he,she etc... k. file1: ALICE was beginning to get very tired of sitting by... (2 Replies)
Discussion started by: arunsubbhian
2 Replies
Login or Register to Ask a Question