Extract words before and after a certain word.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract words before and after a certain word.
# 1  
Old 11-02-2015
Extract words before and after a certain word.

I have a sample text file with file name: sample.txt

The text file has the following text.
Code:
this is an example text where we have to extract certain words before and after certain word these words can be used later to get more information

I want to extract n (a constant) words before and after a certain word. For example, I want to extract 2 words before and after the word "words" in the sample text above. The output that I expect is:
Code:
extract certain words before and
word these words can be

This is what I have attempted:
Code:
grep -owP '.{0,2}words.{0,2}' sample.txt

and the output that I get is this:
Code:
[white-space] word [white-space]
[white-space] word [white-space]

,where [white-space] represents " ". Now when I increase the window size from 2 to 10 in the code above like this:
Code:
grep -owP '.{0,10}words.{0,10}' sample.txt

I get the following output:

Code:
[white-space]certain words before[white-space]
[white-space]these words can be[white-space]

Therefore, one can see that the above code is considering characters before and after and not words. I am using BASH.

Last edited by Don Cragun; 11-02-2015 at 04:07 PM.. Reason: Change ICODE tags to CODE tags.
# 2  
Old 11-02-2015
Hello shoaibjameel123,

Following may help you in same.
Code:
awk '{for(i=1;i<=NF;i++){if($i == "words"){print $(i-2) OFS $(i-1) OFS $i OFS $(i+1) OFS $(i+2)}}}'  Input_file

Output will be as follows.
Code:
extract certain words before and
word these words can be

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 11-02-2015
Quote:
Originally Posted by shoaibjameel123

grep -owP '.{0,2}words.{0,2}' sample.txt

grep -owP '.{0,10}words.{0,10}' sample.txt
Perhaps
Code:
grep -owP '(?:\w+\s){0,2}words(?:\s\w+){0,2}' sample.txt

This User Gave Thanks to Aia For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replacing word and Capitalize words after

I have an assignment and I am not sure what to do. In Unix, I use PuTTY change the semicolon (;) to a period, and capitalize the first letter of the word immediately after it. I know change command is M-% and "." so only one semicolon is changed but I am not sure how to... (1 Reply)
Discussion started by: kathrut43
1 Replies

2. Shell Programming and Scripting

Match the word or words and fetch the entries

Hi all, I have 7 words Now I have 1 file which contain data in large number of rows and columns and 6th column contain any of these words or may be more than one words among above 7 words: I want script should search for the above mentioned 7 words in the 6th column ... (9 Replies)
Discussion started by: manigrover
9 Replies

3. Shell Programming and Scripting

extract whole thing in word, leaving behind last word. - perl

Hi, i've a string /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD/TESi T_11_HD_120/hd-12 i need to get string, like /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD the words from HD should get deleted, i need only a string till HD, i dont want to use any built in... (4 Replies)
Discussion started by: asak
4 Replies

4. Shell Programming and Scripting

How to get a known word between two known words using awk

hi I have posted it earlier but i was unable to put my exact problem.This time posting in parts. I have a text file which i had transferred to UNIX.It has strings like: alter table table_name add (column_name); as well as modify options. now i need to read the table name between alter... (3 Replies)
Discussion started by: alisha
3 Replies

5. Shell Programming and Scripting

Print all the words after a match word

Hi, I want to print all words till the last word after the match of "ERROR" word. For e.g. I'll get an sqlplus error with e.g. 1 $ ./calltest_fn.ksh var test_var:=test_fn1; calltest_fn.ksh file1 file2 file3 ERROR at line 4: ORA-06550: line 4, column 11: PLS-00201: identifier... (5 Replies)
Discussion started by: dips_ag
5 Replies

6. Shell Programming and Scripting

How to remove all words from a matching word in a line?

Hi Guys, :p I have a file like this: 2010-04-25 00:00:30,095 INFO - ]- start process U100M4 2010-04-25 00:00:30,096 DEBUG - ] -- call EJB 2010-04-25 00:00:30,709 INFO - - end processU100M4 2010-04-25 00:00:30,710 DEBUG - got message=Sorry I want to out put format. 2010-04-25... (5 Replies)
Discussion started by: ooilinlove
5 Replies

7. Shell Programming and Scripting

Split a word in short words

Dear, Can somebody help me with this? I have a variable TGT=T2DIRUPDAZ20070326VA I want to get in variables some part of TGT. like this. TGT1=UPDA TGT2=20070326 TGT3= VA These three variables have fixe position in variable TGT. (2 Replies)
Discussion started by: yeclota
2 Replies

8. UNIX for Advanced & Expert Users

How to filter the words, if that word contains the expected letter

Hi, I am trying to filter the words from a file which contain 'abc'. But I am unable to. Could any one help me. For eg: The file contents are 123ab 12hnj1 123abc456 123cgbcahjkf23 23134abchfhj43 gc32abc abc1 2abc3 sd uiguif fhwe 21242 uh123 jkcas124d123 u3hdbh23u ffsd8 Output... (3 Replies)
Discussion started by: venu_eie
3 Replies

9. UNIX for Dummies Questions & Answers

return a word between two words

how do i get a word that exists between two words eg: this is bryan My input to command would be this and bryan and output should be 'is' Is there a command i can use? (4 Replies)
Discussion started by: bryan
4 Replies

10. Shell Programming and Scripting

How to replace a word with a series of words in a file

Hi, I have a Template file 'TL.body' which says as follows: "There are no <FILENAME> files on the server. " The missing file names are identified and stored in a variable. For Eg: MISSFILE="abc.txt def.txt xyz.txt" I want the values of MISSFILE variable to be replaced against... (2 Replies)
Discussion started by: brap45
2 Replies
Login or Register to Ask a Question