Need to use fgrep and egrep in a single command


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Need to use fgrep and egrep in a single command
# 1  
Old 01-07-2016
Need to use fgrep and egrep in a single command

I have a source file which is something like :
Code:
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   ** YEAR FOR CURRENT MINUS 14 CAL DAY
C/+D/  /            ***HMCAG    ** DAY FOR CURRENT PLUS 1 BUSINESS DAY
C/+M/  /            ***HMCAG    ** MONTH FOR CURR PLUS 1 BUSINESS DAY
C/+T/  /            ***HMCAG    ** CENTURY FOR CURR PLUS 1 BUSINESS DAY
C/+Y/  /            ***HMCAG    ** YEAR FOR CURRT PLUS 1 BUSINESS DAY
C/=M/  /                        ** MONTH FOR CURRENT PLUS 2 CAL DAY
C/=D/  /                        ** DAY FOR CURRENT PLUS 2 CAL DAY
C/=Z/  /                        ** CENTURY FOR CURRENT PLUS 2 CAL DAY
C/=Y/  /                        ** YEAR FOR CURRENT PLUS 2 CAL DAY

I need to find out whether there is any line present with
HTML Code:
*D or *X or *M
since there is a *, i am using fgrep to find exact match when i am searching for just one match
Code:
$fgrep "*M"  $SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD
C/*M/  /            ***HMACCT   ** MONTH FOR CURRENT MINUS 14 CAL DAY

however when i am trying to search for multiple matches
Code:
fgrep "*M |*D" $SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD

it does not work..no result
Any suggestions please !!
# 2  
Old 01-07-2016
Hello gotamp,

You could try following solutions for same.
1st Solution:
Code:
grep -e '\*M' -e '\*D' -e '\*X'  Input_file

Output will be as follows.
Code:
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

2nd Solution:
Code:
awk '/\*M/||/\*D/||/\*X/'  Input_file

Output will be as follows.
Code:
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

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 01-07-2016
This should suffice:
Code:
grep '*[DXM]' file

Given the other character patterns on the lines, it may be better to anchor it to the two forward slashes:
Code:
grep '/\*[DXM]/' file

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 01-07-2016
Hi Ravinder,

Thanks for the help.
however this does not work for me..

Code:
aspsun11:dbh0000$grep -e '\*M' -e '\*D' -e '\*X' $SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD
grep: illegal option -- e
Usage: grep -hblcnsviw pattern file . . .

---------- Post updated at 07:50 AM ---------- Previous update was at 07:45 AM ----------

Quote:
Originally Posted by Scrutinizer
This should suffice:
Code:
grep '*[DXM]' file

Given the other character patterns on the lines, it may be better to anchor it to the two forward slashes:
Code:
grep '/\*[DXM]/' file


Many thanks Scrutinizer..
this is working absolutely fine.
Smilie
# 5  
Old 01-07-2016
Hello gotamp,

Glad that Scrutinizer's solution worked for you. Seems to be you have Solaris OS, it is always good practice to mention your OS details in your posts, you could try awk solution too which I have posted in POST#2. But on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk, or nawk.

Thanks,
R. Singh
# 6  
Old 01-07-2016
Yes it's Solaris, where you need the /usr/xpg4/bin/grep for the -e option.
Ravinder's awk solution works even with the old /usr/bin/awk
BTW it uses awk's || operator outside the /pattern/, where spaces are allowed for readability
Code:
awk '/\*M/ || /\*D/ || /\*X/' file

It is also possible to use the ERE (awk, egrep)-style | operator within the pattern
Code:
awk '/\*M|\*D|\*X/' file

or use a character set (like in any grep/egrep)
Code:
awk '/\*[MDX]/' file

But because awk uses the / as a pattern-delimiter, a literal / must be escaped in a pattern. That lowers readability
Code:
awk '/\/\*M\// || /\/\*D\// || /\/\*X\//' file

or
Code:
awk '/\/\*M\/|\/\*D\/|\/\*X\//' file

or
Code:
awk '/\/\*[MDX]\//' file

Last but not least there is the ERE-style ( ) sub-expression
Code:
awk '/\/\*(M|D|X)\//' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

fgrep command: Perl programming help needed..Kindly advise

Hi, I am novice in PERL enviornment. I have a text files withso many entries in rows and columns. I have to pick up entries named as "Uniprot ID" in the file and create a new text file with list of particular Uniprot ID entries. Can anybody guide regarding this.. I came to know abut fgrep... (1 Reply)
Discussion started by: manigrover
1 Replies

2. Shell Programming and Scripting

Help with egrep command

Hello folks, Here's how my current egrep command works: 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: NY DC LA VA MD egrep "`cat state_names.lookup`"... (6 Replies)
Discussion started by: calredd
6 Replies

3. 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

4. Shell Programming and Scripting

Problem in Using fgrep Command with pattern file option

Hi, i am using fgrep command with following syntax fgrep -v -f pattern_file_name file file contains few line and have the pattern which i am giving in pattern file. My Problem is : its is not giving any output. while i am using fgrep -f pattern_file_name file it is showing all... (4 Replies)
Discussion started by: emresearch
4 Replies

5. Shell Programming and Scripting

grep/fgrep/egrep for a very large matrix

All, I have a problem with grep/fgrep/egrep. Basically I am building a 200 times 200 correlation matrix. The entries of this matrix need to be retrieved from another very large matrix (~100G). I tried to use the grep/fgrep/egrep to locate each entry and put them into one file. It looks very... (1 Reply)
Discussion started by: realwindfly
1 Replies

6. Shell Programming and Scripting

fgrep command

How can we use fgrep command to search pattern on perticular field on a file. eg : I have a parren file having format cat patternfile SPA16S199982 SPA5S26330 I want to seach these pattern on FIRST field of File2(since other field has same data) and that too maching entirely. ... (10 Replies)
Discussion started by: morbid_angel
10 Replies

7. 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

8. UNIX Desktop Questions & Answers

Difference grep, egrep and fgrep

Hi All, Can anyone please explain me the difference between grep, egrep and fgrep with examples. I am new to unix environment.. Your help is highly appreciated. Regards, ravi (2 Replies)
Discussion started by: ravind27
2 Replies

9. UNIX for Dummies Questions & Answers

Usage of fgrep -f command in kshell

Hi, Can I know the usage of fgrep -f command in kshell. Reqirement: Need to search for list of patterns in list of files and display all the files which are having atleast one in the patterns list. I hope fgrep -f will serve this purpose within a loop. But I am not successfull in doing this.... (0 Replies)
Discussion started by: divv
0 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