Filter NOT words


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Filter NOT words
# 1  
Old 12-22-2008
Filter NOT words

Hi,

I have string like this (for example)

Code:
$str="orange AND grapes NOT pineapple";

$str="orange AND grapes NOT pineapple AND papaya";

$str="orange AND grapes NOT pineapple AND jackfruit NOT "fruit juice"";

I have to filter out NOT terms.

How can i do that?

I used this code but its not giving NOT terms properly.

Code:
while($str=~/\sNOT\s([^()]+)/g)
{
        print "<br>entered<br>";
        print "<br>**********$1<br>";
}

Please suggest me a way to filter NOT terms in perl.
The output should be like this:

Code:
orange AND grapes
orange AND grapes AND papaya
orange AND grapes AND jackfruit

Thanks and regards,
Vanitha
# 2  
Old 12-22-2008
try:
Code:
@arr = ("orange AND grapes NOT pineapple",
        "orange AND grapes NOT pineapple AND papaya",
        "orange AND grapes NOT pineapple AND jackfruit NOT fruit juice");

for $str (@arr) {
    print $str, "\n";
}
print "\n\n";

for $str (@arr) {
    $str =~ s/\sNOT [\sa-z]+(\z|( AND))/\1/g;
    print $str, "\n";
}

works for the data you provided. might not work if the data varies.
# 3  
Old 12-22-2008
Hi,

yogesh you overlooked the "..." around "fruit juice".

This should handle the ", too:

Code:
$str=~s/NOT ("[^"]*"\s*|\w+\s*)//g;

HTH Chris
# 4  
Old 12-22-2008
Quote:
Originally Posted by Christoph Spohr
Hi,

yogesh you overlooked the "..." around "fruit juice".

This should handle the ", too:

Code:
$str=~s/NOT ("[^"]*"\s*|\w+\s*)//g;

HTH Chris
Thanks Chris .

Alternate solution: $str=~s/\sNOT\s("[a-z]*\s*[a-z]*|[a-z]*)//g;
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. Shell Programming and Scripting

Search words in any quote position and then change the words

hi, i need to replace all words in any quote position and then need to change the words inside the file thousand of raw. textfile data : "Ninguno","Confirma","JuicioABC" "JuicioCOMP","Recurso","JuicioABC" "JuicioDELL","Nulidad","Nosino" "Solidade","JuicioEUR","Segundo" need... (1 Reply)
Discussion started by: benjietambling
1 Replies

3. Shell Programming and Scripting

Filter all the lines with minimum specified length of words of a text file

Hi Can someone tell me which script will work best (in terms of speed and simplicity to write and run) for a large text file to filter all the lines with a minimum specified length of words ? A sample script with be definitely of great help !!! Thanks in advance. :) (4 Replies)
Discussion started by: my_Perl
4 Replies

4. UNIX for Dummies Questions & Answers

Replace the words in the file to the words that user type?

Hello, I would like to change my setting in a file to the setting that user input. For example, by default it is ONBOOT=ON When user key in "YES", it would be ONBOOT=YES -------------- This code only adds in the entire user input, but didn't replace it. How do i go about... (5 Replies)
Discussion started by: malfolozy
5 Replies

5. Shell Programming and Scripting

Gawk gensub, match capital words and lowercase words

Hi I have strings like these : Vengeance mitt Men Vengeance gloves Women Quatro Windstopper Etip gloves Quatro Windstopper Etip gloves Girls Thermobite hooded jacket Thermobite Triclimate snow jacket Boys Thermobite Triclimate snow jacket and I would like to get the lower case words at... (2 Replies)
Discussion started by: louisJ
2 Replies

6. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

7. UNIX for Dummies Questions & Answers

Filter logs for key words and create an index

well, im a total novice of unix commands and shell scripting, i never made anything. But yesterday in my work i was working with a lot of log files and i was wondering if there is a command for doing something like this: i have a bunch of files with text like this: blablabla errorcode1... (11 Replies)
Discussion started by: matius_88
11 Replies

8. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

9. Shell Programming and Scripting

Sed filter words from string

I have following string in a variable: str="sstring garbage adfsdf tab.col1 lkad rjfj tab2.col2 adja tab2.col4 garbage" I want to filter "word.word" pattern from it. I assume the pattern won't occur in start or end of the string. Output shoud be: tab.col1 tab2.col2 tab2.col4 Can this be... (7 Replies)
Discussion started by: amicon007
7 Replies

10. 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
Login or Register to Ask a Question