Visit Our UNIX and Linux User Community


Grep with regex containing one string but not the other


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep with regex containing one string but not the other
# 1  
Old 04-08-2015
Grep with regex containing one string but not the other

Hi to you all,

I'm just struggling with a regex problem and I'm pretty sure that I'm missing sth obvious... Smilie

I need a regex to feed my grep in order to find lines that contain one string but not the other.

Here's the data example:

Code:
2015-04-08 19:04:55,926|xxxxxxxxxx|          |foobar|          |INFO |REQUEST|UserID1:42 | yeah
2015-04-08 19:04:56,157|yyyyyyyyyy|          |foobuzz|          |INFO |REQUEST|UserID1:23 | ohnooo

I want all lines with "UserID1" not containing "foobuzz". I tried this one:

Code:
LC_ALL=C grep -P '(?!foobuzz).*UserID1.*' example.txt

But it gives me both lines. (I have the "LC_ALL=C" workaround from a RedHat article, otherwise I get a core dump for free. ;-) )

What's wrong with me, uhm, with the regex?

And yes, it should be grep because the new regex will be part of an existing regex file which is used by a script in order to grep some gigabytes of data multiple times a day. I don't know if it would be faster with awk or sth like this. Any advice is appreciated!

I will get back to you tomorrow - it's late, I'm the last one in the office, sun is gone... ;-)

Best regards

Stephan

Additional informations:

Code:
LSB Version:    :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch
Distributor ID: RedHatEnterpriseServer
Description:    Red Hat Enterprise Linux Server release 6.6 (Santiago)
Release:        6.6
Codename:       Santiago

Code:
GNU grep 2.6.3

# 2  
Old 04-08-2015
Code:
grep UserID1 example.txt | grep -v foobuzz
OR
awk '/UserID1/ && !/foobuzz/' example.txt
OR
awk '!/.*foobuzz.*UserID1/' example.txt
OR
and the list goes on and on....


Last edited by vgersh99; 04-08-2015 at 03:55 PM..
# 3  
Old 04-08-2015
With this 2 line example data, it would even work with:
Code:
$ grep -v foobuzz sample.txt 
2015-04-08 19:04:55,926|xxxxxxxxxx|          |foobar|          |INFO |REQUEST|UserID1:42 | yeah

Not sure why this doesnt work though...
Code:
grep -e UserID1 -ve buzz sample.txt

# 4  
Old 04-08-2015
You can't feed two different regular expressions into one grep. It doesn't do any sort of conditional logic, it just matches lines.

awk is a programming language, and can:


Code:
awk '/User1/ && !/buzz/'

# 5  
Old 04-09-2015
To be more precise:

I have a shell script which calls a grep command (and does some more postprocessing).
This grep uses a file that holds a bunch of regexes and one of the regexes should be able to do a negative lookbehind (if that's the correct term for the regex logic that I want to achieve). So a piped "grep -v" doesn't help me in this case.

And wouldn't it slow down the processing if I would refactor it to awk (which handles the file with the regexes)?
# 6  
Old 04-09-2015
Hi, with grep:
Code:
$ cat gr.txt
2015-04-08 19:04:55,926|xxxxxxxxxx|          |foobar|          |INFO |REQUEST|UserID1:42 | yeah
2015-04-08 19:04:56,157|yyyyyyyyyy|          |foobuzz|          |INFO |REQUEST|UserID1:23 | ohnooo
2015-04-08 19:04:56,157|yyyyyyyyyy|          |foobuz|          |INFO |REQUEST|UserID1:23 | ohnooo
$ grep  -P '\|(?!foobuzz)[^|]*\|[^|]*\|[^|]*\|[^|]*\|UserID1' gr.txt
2015-04-08 19:04:55,926|xxxxxxxxxx|          |foobar|          |INFO |REQUEST|UserID1:42 | yeah
2015-04-08 19:04:56,157|yyyyyyyyyy|          |foobuz|          |INFO |REQUEST|UserID1:23 | ohnooo
$

Regards.
This User Gave Thanks to disedorgue For This Post:
# 7  
Old 04-09-2015
Yeah, that's what I wanted to see! Smilie

Could you please explain why it didn't work out with a simple ".*" instead of the more exact "[^|]*\|[^|]*\|[^|]*\|[^|]*\|"? I'm not that familiar with the perl'ish version of grep.

Thanks!

Best regards

Stephan

Quote:
Originally Posted by disedorgue
Hi, with grep:
Code:
$ cat gr.txt
2015-04-08 19:04:55,926|xxxxxxxxxx|          |foobar|          |INFO |REQUEST|UserID1:42 | yeah
2015-04-08 19:04:56,157|yyyyyyyyyy|          |foobuzz|          |INFO |REQUEST|UserID1:23 | ohnooo
2015-04-08 19:04:56,157|yyyyyyyyyy|          |foobuz|          |INFO |REQUEST|UserID1:23 | ohnooo
$ grep  -P '\|(?!foobuzz)[^|]*\|[^|]*\|[^|]*\|[^|]*\|UserID1' gr.txt
2015-04-08 19:04:55,926|xxxxxxxxxx|          |foobar|          |INFO |REQUEST|UserID1:42 | yeah
2015-04-08 19:04:56,157|yyyyyyyyyy|          |foobuz|          |INFO |REQUEST|UserID1:23 | ohnooo
$

Regards.

Previous Thread | Next Thread
Test Your Knowledge in Computers #89
Difficulty: Easy
Software management applications like the Ubuntu Software Center are front-ends to the core terminal commands use to install and manage software applications.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep string with regex numeric characters

Hi all, I have the following entries in a file: Cause Indicators=80 90 Cause Indicators=80 90 Cause Indicators=82 90 Cause Indicators=82 90 Cause Indicators=82 90 The first 2 digits might change so I am after a sort of grep which could find any first 2 digits + the second 2,... (3 Replies)
Discussion started by: nms
3 Replies

2. Shell Programming and Scripting

grep regex, match exact string which includes "/" anywhere on line.

I have a file that contains the 2 following lines (from /proc/mounts) /dev/sdc1 /mnt/backup2 xfs rw,relatime,attr2,noquota 0 0 /dev/sdb1 /mnt/backup xfs rw,relatime,attr2,noquota 0 0 I need to match the string in the second column exactly so that only one result is returned, e.g. > grep... (2 Replies)
Discussion started by: jelloir
2 Replies

3. Shell Programming and Scripting

grep fixed string with regex

Hello, all! Maybe the title is badly formulated, you can help me with that...! I'm using the GNU grep, and I need to make sure that grep will extract only what I tell it to. I have the following regular expression: *? Well, I need to make sure I grep only a word which may start with a... (11 Replies)
Discussion started by: teresaejunior
11 Replies

4. Shell Programming and Scripting

filtering out duplicate substrings, regex string from a string

My input contains a single word lines. From each line data.txt prjtestBlaBlatestBlaBla prjthisBlaBlathisBlaBla prjthatBlaBladpthatBlaBla prjgoodBlaBladpgoodBlaBla prjgood1BlaBla123dpgood1BlaBla123 Desired output --> data_out.txt prjtestBlaBla prjthisBlaBla... (8 Replies)
Discussion started by: kchinnam
8 Replies

5. UNIX for Dummies Questions & Answers

Regex to match when input is not a certain string (can't use grep -v)

Hey everyone, Basically, all I'm looking for is a way to regex for not a certain string. The regex I'm looking to avoid matching is: D222 i.e. an equivalent of: awk '!/D222/' The problem is that I use this in the following command in a Bash script: ls ${source_directory} | awk... (1 Reply)
Discussion started by: kdelok
1 Replies

6. UNIX for Dummies Questions & Answers

| help | unix | grep (GNU grep) 2.5.1 | advanced regex syntax

Hello, I'm working on unix with grep (GNU grep) 2.5.1. I'm going through some of the newer regex syntax using Regular Expression Reference - Advanced Syntax a guide. ls -aLl /bin | grep "\(x\)" Which works, just highlights 'x' where ever, when ever. I'm trying to to get (?:) to work but... (4 Replies)
Discussion started by: MykC
4 Replies

7. UNIX for Dummies Questions & Answers

Help with grep and regex

Hi all, I'm a beginner with linux, regex, grep, etc I am trying to get data out of a file that has about 13,000 lines in this format name - location I want to grep all the names out to one file and the locations to another so I can put them into a spreadsheet. Some have hyphenated... (14 Replies)
Discussion started by: raichlea
14 Replies

8. UNIX for Dummies Questions & Answers

grep with Regex help!

Hello everybody, I'd like to know how is it I should write a regex in unix to match a string not followed by another string (anywhere in the line). To be more specific, I want to find lines where "drop table" is found, but not followed anywhere in the line by the character "&". For... (3 Replies)
Discussion started by: mvalonso
3 Replies

9. Shell Programming and Scripting

sed, grep, awk, regex -- extracting a matched substring from a file/string

Ok, I'm stumped and can't seem to find relevant info. (I'm not even sure, I might have asked something similar before.): I'm trying to use shell scripting/UNIX commands to extract URLs from a fairly large web page, with a view to ultimately wrapping this in PHP with exec() and including the... (2 Replies)
Discussion started by: ropers
2 Replies

10. UNIX for Dummies Questions & Answers

use of regex on grep

having a look on the regex site I saw that characters can be search using hex values http://www.regular-expressions.info/characters.html So I try to use it whith grep to find a on a string (octal Decimal Hexa : 350 232 E8) but it doesn't work E.g. /usr/bin/echo '\0350' | egrep '\xE8' ... (0 Replies)
Discussion started by: solea
0 Replies

Featured Tech Videos