Logical AND operation with egrep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Logical AND operation with egrep
# 8  
Old 09-01-2009
Its not like 'grep' will not work with csv file. It will work with all kind of text file, even with binary files.

But in case of CSV you might be having a separator, so don't forget to include separator in the pattern match.

If the colon is separator then,
Code:
egrep str1:str2:str3

# 9  
Old 09-02-2009
If you want to find ALL combinations of str1 str2 str3 in a line from a CSV file, there is no easy regex solution. Either you pipe successive egrep's like in the joeyg's example or you can use awk.

To find all lines containing Joe and Linda in any order:
Code:
$ cat > test.file
Joe,Linda,Bobby,Terry
Kim,Rhonda,Steve,Joe
Linda,Joe,Bob,Terry

$ awk -F"," '{flag=0;for (i=1;i<=NF;i++) if($i ~ /Joe|Linda/) flag++} flag==2 {print}' test.file
Joe,Linda,Bobby,Terry
Linda,Joe,Bob,Terry

Now, imagine we are after lines with Bob and Joe, the above awk snippet will logically return:
Code:
$ awk -F"," '{flag=0;for (i=1;i<=NF;i++) if($i ~ /Joe|Bob/) flag++} flag==2 {print}' test.file
Joe,Linda,Bobby,Terry
Linda,Joe,Bob,Terry

As awk use POSIX regex we can use the ^ and $ metacharacters:
Code:
$ awk -F"," '{flag=0;for (i=1;i<=NF;i++) if($i ~ /^(Joe|Bob)$/) flag++} flag==2 {print}' test.file
Linda,Joe,Bob,Terry

# 10  
Old 09-02-2009
Quote:
Originally Posted by ripat
If you want to find ALL combinations of str1 str2 str3 in a line from a CSV file, there is no easy regex solution.
What about this?

Code:
awk '/str1/ && /str2/ && /str3/' file

# 11  
Old 09-02-2009
Quote:
Originally Posted by Franklin52
What about this?

Code:
awk '/str1/ && /str2/ && /str3/' file

[EMBARASSED]
Well, it's so much... better! And so obvious.
[/EMBARASSED]
# 12  
Old 09-02-2009
My string is coming through web application in a variable
like
VARIABLE="str1 str2 str3"

i need to use this variable in search.

This is not working when i am using my variable

awk '/$VARIABLE/' file Smilie

Last edited by sam25; 09-02-2009 at 12:08 PM..
# 13  
Old 09-02-2009
perhaps this ...

Code:
awk -v n=$variable '/n/' file

# 14  
Old 09-02-2009
not working error coming

$awk -v n=$key '/n/' file.csv
awk: Cannot find or open file /n/.
The source line number is 1.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Column operation : cosne and sine operation

I have a txt file with several columns and i want to peform an operation on two columns and output it to a new txt file . file.txt 900.00000 1 1 1 500.00000 500.00000 100000.000 4 4 1.45257346E-07 899.10834 ... (4 Replies)
Discussion started by: shashi792
4 Replies

2. Solaris

Operation and Maintenance

I gurus of Solaris, I need to do a Procedure concerning in the Maintenance of Solaris Server. What are the parameters that I must be see Periodically in a Server. For example the space I (df -h) must be each week.- In this server exist a Database aplication (Oracle), and log's that increase or... (4 Replies)
Discussion started by: andresguillen
4 Replies

3. Shell Programming and Scripting

String Operation

/home8/mc09ats/UnixCw/file4 this is the path...i have 2 variables filename and filepath...i want filename=file4 filepath=/home8/mc09ats/UnixCw i.e. i think i have to find last occurence of "/" in string and the string after "/" want to take in some variable and string before last "/"... (4 Replies)
Discussion started by: AbhijitIT
4 Replies

4. Shell Programming and Scripting

File Operation

I have one text file like 1 filename 2 filename2 3 hi 4 myname i have one variable in which i have index value..i.e.1,2,3 and so... i want to copy value after the index into somevariable..... how i can do it.... (2 Replies)
Discussion started by: AbhijitIT
2 Replies

5. UNIX for Dummies Questions & Answers

string operation

I am trying to ask for full name in a script, Then echo back to the user with the surname only, omitting the first name. Thanks (2 Replies)
Discussion started by: orjnet
2 Replies

6. UNIX for Dummies Questions & Answers

search ")" with egrep - egrep: syntax error

Hi Guys, we have a shell script which basically query the Database which retrieves huge data and use the data with "egrep" . Now there is some data which contains characters like "abc)" and the same is used like below : "egrep (.+\|GDPRAB16\|GDPR/11702 96 abc)\|$ temp.txt" now while... (7 Replies)
Discussion started by: sagarjani
7 Replies

7. Shell Programming and Scripting

How to do logical AND and logical OR with grep

Hi can someone please help me on this. I need to perform this code: Grep any lines that meets the following criteria (A AND B) OR (A AND C) I tried this code, but it didn't work Grep-I "A &&B" | "A&&C" *.* $ thanks in advance (12 Replies)
Discussion started by: Needhelp2
12 Replies

8. UNIX for Dummies Questions & Answers

cat operation

Hi, Can anyone explain me what is the functionality of this code cat << EOF > $TSTFILE /$1/ { print "SENT" } EOF Suggestions welcome Thanks in advance (0 Replies)
Discussion started by: trichyselva
0 Replies

9. Shell Programming and Scripting

Array operation

Hi, I would like ask for you help for coding array operation. array= ( a b c d e f ) I would like to remove entry "d" from my array and import the remaining entries back to the array. Thanks. (3 Replies)
Discussion started by: phamp008
3 Replies

10. UNIX for Dummies Questions & Answers

Egrep cheat sheet anywhere? Looking for meaning of egrep -c

Hi I've been searching google and have not found what egrep -c means. Does anyone know where I can get a cheat sheet or what that -c means? thanks, Linda (2 Replies)
Discussion started by: leelm
2 Replies
Login or Register to Ask a Question