Help with egrep command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with egrep command
# 1  
Old 04-04-2012
Help with egrep command

Hello folks,
Here's how my current egrep command works:

Code:
egrep "NY|DC|LA|VA|MD" state_data.txt


I am planning to use a file to enter all allowable state values like say a new state_names.lookup with the following data:
Code:
NY
DC
LA
VA
MD


Code:
egrep "`cat state_names.lookup`" state_data.txt


if i execute the above command, it only gives me the entries with NY and all the below values such as DC, LA, VA, MD etc.. are ignored even though there are other states with data in state_data.txt file.

How can I make this work ? My thought is if I get more states in future, I just edit and add more entries to state_names.lookup file rather than to edit the actual command...

Any help is appreciated.
Thanks,
Carl

Moderator's Comments:
Mod Comment Please use code tags. Video tutorial on how to use them. Thank you.
# 2  
Old 04-04-2012
Try:
Code:
grep -f state_names.lookup state_data.txt

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 04-04-2012
thanks. your solution worked but i had to use egrep (and not grep as you mentioned)
# 4  
Old 04-04-2012
Hi calredd, why did you need egrep (or grep -E) for extended regular expressions ? I thought you file "state_names.lookup" contained just plain strings. What happened?
# 5  
Old 04-04-2012
Quote:
Originally Posted by Scrutinizer
Hi calredd, why did you need egrep (or grep -E) for extended regular expressions ? I thought you file "state_names.lookup" contained just plain strings. What happened?
I first ran the same was you suggested but it didnt work...

>grep -f state_names.lookup states_input.txt
grep: illegal option -- f
Usage: grep -hblcnsviw pattern file . . .


but when I change grep to egrep, bingo it worked....
yes my state_names.lookup file has plain strings (one entry per line)
# 6  
Old 04-04-2012
O, I see.. Solaris. /usr/xpg4/bin/grep -f should work then. Interesting, so egrep and fgrep do have the -f option, but regular grep does not. Thanks for reporting back...

S.


--
If I were using Solaris right now, I think the first thing I would do is change my path to:
Code:
export PATH=/usr/xpg4/bin:$PATH

And be done with it...

Last edited by Scrutinizer; 04-04-2012 at 03:41 PM..
# 7  
Old 04-04-2012
Quote:
Originally Posted by Scrutinizer
O, I see.. Solaris. /usr/xpg4/bin/grep -f should work, but you got egrep to work so..
Sorry, didnt mention I was using solaris... yep, got the egrep to work, so no worries..
Thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Egrep -v command not filtering correctly

Hello guys, I have an issue when trying to do an egrep -v on a file, let me show you. I want to filter the last column as to where it filters out the columns with asterisks and zeros ( * and 0 ) it is working properly up to a certain point where I have a value of '10000' which is also getting... (3 Replies)
Discussion started by: evergreen
3 Replies

2. UNIX for Beginners Questions & Answers

Egrep command is not working for me

my file is below REREGISTER is something to Failed to create the request Failed to create the request in not easy I know how REREGISTERcommand i run is egrep 'REREGISTER|Failed|to|create|the|request' test1 expected output REREGISTER is something to Failed to create the request i should... (2 Replies)
Discussion started by: mirwasim
2 Replies

3. UNIX for Advanced & Expert Users

Need to use fgrep and egrep in a single command

I have a source file which is something like : C/*M/ / ***HMACCT ** MONTH FOR CURRENT MINUS 14 CAL DAY C/*D/ / ***HMACCT ** DAY FOR CURRENT MINUS 14 CAL DAY C/*X/ / ***HMACCT ** CENTURY FOR CURRENT MINUS 14 CAL DAY C/*Y/ / ***HMACCT **... (5 Replies)
Discussion started by: gotamp
5 Replies

4. Shell Programming and Scripting

help in egrep command in file

Hi Guys, I need hepl on egrep commnad I have one file and i need grep only specific lines.. EX: 2012-04-01 02:15:14 w 2012-04-01 02:15:14 w 2012-04-01 02:15:14 w 2012-10-26 02:15:14 w 2012-04-01 02:15:14 w 2012-10-26 02:15:14 w 2012-10-26 02:15:14 w 2012-10-26 03:18:56 M... (1 Reply)
Discussion started by: asavaliya
1 Replies

5. Shell Programming and Scripting

Help with egrep command

cat /tmp/inventory.csv|grep AARP|egrep -v "T11|12.4\(7\)" how do i exclude in addition to above 12.4\(3\) I have tried adding this in i.e -v "T11|12.4\(7\)|12.4\(3\)" but it did not work (3 Replies)
Discussion started by: slashbash
3 Replies

6. Shell Programming and Scripting

egrep command and order

Hi All, Here is my question. I have two files, file1.txt and file2.txt. I need the line number (index number) of file2.txt where the words in file1.txt appear. But they have to be in the same order as file1.txt. In example, file1.txt Z K A ... T file2.txt W A Q R (6 Replies)
Discussion started by: senayasma
6 Replies

7. Shell Programming and Scripting

egrep -w command by reading from file

Dear All, I want to get the rows from file1.txt whose first column is exactly as the numbers (not subset) from file1.txt. so, i have to read by line from file1.txt and search in file2.txt. So the first column of the output must be the same as the file1.txt. I used % egrep -w -f... (5 Replies)
Discussion started by: senayasma
5 Replies

8. Shell Programming and Scripting

Query regarding egrep command

Hi All, I am having a query regarding the usage of egrep command. i am having two unix environmanets in environment when i am using "egrep -f" it is working fine and other unix environment i am getting a syntax error. Please let me know if i need to set any environmane variables. ... (12 Replies)
Discussion started by: Sriram.Vedula53
12 Replies

9. Shell Programming and Scripting

Help with egrep command

Hi All, I am using egrep command to search one pattern. Following is the command i am using egrep -i "ACL*" filename but its also giving me the records which do not contain ACL. any help would be appreciated. Regards, Sam (3 Replies)
Discussion started by: sam25
3 Replies

10. Shell Programming and Scripting

egrep command

I'd like to grep a pattern of a version number as *_number.number.number number should be digit my grep is |egrep '^*++\.+' It works for V_3.2.1 or V _5.3.2 but not with V_43.6.543 !!!!! How can I specify any repetition of digit in the ? thanks, (4 Replies)
Discussion started by: annelisa
4 Replies
Login or Register to Ask a Question