Grep multiple words in a file with help of fixed string switch


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Grep multiple words in a file with help of fixed string switch
# 1  
Old 12-12-2019
Grep multiple words in a file with help of fixed string switch

I have multiple strings in a file which have special character $, when i search strings by ignoring $ with \ using single quotes it returns empty results.

My search strings are set char_1($lock) and set new_char_clear_3($unlock)

I tried searching with

Quote:
fgrep 'set char_1($lock)\|set new_char_clear_3($unlock)' filename.txt
but it returns empty results.However the following command gives results for 1st string

Code:
fgrep 'set char_1($lock)' filename.txt

Tried with grep -F as well which return same results as above.

How do i get both results with single command.

Last edited by rbatte1; 12-12-2019 at 10:09 AM.. Reason: Added ICODE tags
# 2  
Old 12-12-2019
Not with fgrep nor grep -F. man grep:
Quote:
-F, --fixed-strings
Interpret PATTERNS as fixed strings, not regular expressions.
.
.
.

Alternation
Two regular expressions may be joined by the infix operator |; the resulting regular expression matches any string matching either alternate expression.

You see that fgrep looks for the entire fixed string including the pipe char. Try plain grep instead.
# 3  
Old 12-12-2019
Or try with -e:
Code:
grep -Fe 'set char_1($lock)' -e 'set new_char_clear_3($unlock)' filename.txt


---
With grep -E you need to escape both the dollarsigns and the parameters
Code:
grep -E 'set char_1\(\$lock\)|set new_char_clear_3\(\$unlock\)' filename.txt


Last edited by Scrutinizer; 12-12-2019 at 04:17 PM..
# 4  
Old 12-12-2019
Either two -e
Code:
fgrep -e 'set char_1($lock)' -e 'set new_char_clear_3($unlock)' filename.txt

or two lines
Code:
fgrep 'set char_1($lock)
set new_char_clear_3($unlock)' filename.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep only words containing specific string

Hello, I have two files. All urls are space seperated. source http://xx.yy.zz http://df.ss.sd.xz http://09.09.090.01 http://11.22.33 http://canada.xx.yy http://01.02.03.04 http://33.44.55 http://98.87.76.65 http://russia.xx.zz http://aa.tt.xx.zz http://1w.2e.3r.4t http://china.rr.tt ... (4 Replies)
Discussion started by: baris35
4 Replies

2. Shell Programming and Scripting

Break one long string into multiple fixed length lines

This is actually a KSH under Unix System Services (Z/OS), but hoping I can get a standard AIX/KSH solution to work... I have a very large, single line file in Windows, that we download via FTP, with the "SITE WRAP" option, into a Z/OS file with an LRECL of 200. This essentially breaks the single... (4 Replies)
Discussion started by: bubbawuzhere
4 Replies

3. Shell Programming and Scripting

Search string or words in logs without using Grep

I'm in need of some kind of script that will search for a string in each logfile in a directory but we don't want to use GREP. GREP seems to use up to much of our memory causing the server to use up a lot of swap space. Our log files are bigger than 500M on a daily basis. We lately started... (8 Replies)
Discussion started by: senormarquez
8 Replies

4. Shell Programming and Scripting

Confused with grep for multiple words

Hi guys and gals, I have many files that contains many lines of data. I am trying to find a needle in a haystack in that I'm looking only for files that contain word1 AND word2. I'm using ... ... but this is finding files that contains word1 OR word2. No good for me. How can I grep to... (7 Replies)
Discussion started by: bbbngowc
7 Replies

5. Shell Programming and Scripting

Grep multiple words with not null value

Hi, I want to grep a file if any one (GH, IJ, KL) is not null. If it is null i dont want to pull anything. cat file | awk '{print ($1)}' Parameters are : AB=123;CD=456;EF=6789; cat file | awk '{print ($2)}' GH=456;IJ=789;KL=1011 eg: Contents in file: Parameters are :... (10 Replies)
Discussion started by: Neethu
10 Replies

6. Shell Programming and Scripting

Grep multiple words in a single file

Hello All, I'm a newbie/rookie in Shell scipting. I've done oracle export of a table using Export utility. When I do export, it generates 2 files. 1> .dmp file 2> .dmp.log file. In .dmp.log file I have to search for a sentence which goes like '0 records have been inserted' and then... (2 Replies)
Discussion started by: samfisher
2 Replies

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

8. Shell Programming and Scripting

grep multiple words in a single line

Hi.. How to search for multiple words in a single line using grep?. Eg: Jack and Jill went up the hill Jack and Jill were best friends Humpty and Dumpty were good friends too ---------- I want to extract the 2nd statement(assuming there are several statements with... (11 Replies)
Discussion started by: anduzzi
11 Replies

9. UNIX for Dummies Questions & Answers

search multiple words using grep

Hi frnds i want to desplay file names that should be word1 and word2 ex : i have 10 *.log files 5 files having word1 and word2 5 files having only word1, i have used below command egrep -l 'word1|word2' *.log its giving all 10 files, but i want to display only 5... (20 Replies)
Discussion started by: pb18798
20 Replies

10. UNIX for Dummies Questions & Answers

problem with grep on search string in a txt file over multiple files

I have a couple of things I got stuck on 1) I have a text file containing 25k search string that I need to search against compressed file. I have used this command but somehow it doesn't seems to use all the search terms. I have put one search string per line in the txt file (I clean up... (2 Replies)
Discussion started by: m00
2 Replies
Login or Register to Ask a Question