Searching for a specific string in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching for a specific string in a file
# 1  
Old 06-27-2011
Searching for a specific string in a file

Hi

I am trying to search for a certain set of patterns within a file, and then perform other commands based on output.

testfile contents:
Code:
password    requisite     pam_cracklib.so lcredit=-1 ucredit=-1 ocredit=-1

script:
Code:
D="dcredit=-1"

if [ "cat testfile | grep -w "$D"" ]
then
echo $D exists
else
echo $D doesnt exist
fi

Tried executing, but it always outputs true regardless of whether $D is present or not.

Can someone please assist?

Thank you
# 2  
Old 06-27-2011
Try something like this:

Code:
if grep -q "$D" testfile 
then
   echo "there"
else
   echo "not there"
fi

You don't need to cat to grep; grep will read the file. You also don't want to test the output string like that. Test the return code from grep which will be 'success' if the pattern was matched.
This User Gave Thanks to agama For This Post:
# 3  
Old 06-27-2011
Try:
Code:
if grep -wq "$D" testfile
then
echo $D exists
else
echo $D doesnt exist
fi

This User Gave Thanks to yazu For This Post:
# 4  
Old 06-27-2011
Thank you for clearing this.

I tried with this
Code:
[ "`cat testfile | grep -w "$D"`" ]

and it worked too.

I am also trying at a later stage to get multiple grep conditions checked. Such as ,

Code:
if  grep -qw '"$D"|"$L"' testfile

Dont think im doing it right here. Does this need to be enclosed in [ ] ?

Thanks very much
# 5  
Old 06-27-2011
Quote:
Originally Posted by bludhemn
I am also trying at a later stage to get multiple grep conditions checked. Such as ,

Code:
if  grep -qw '"$D"|"$L"' testfile

Dont think im doing it right here. Does this need to be enclosed in [ ] ?

Thanks very much
You'll need to change things a wee bit.

First, I think, you'll need egrep if you are going to use 'pattern|pattern'. At least my version of grep won't do this, but egrep will.

Second, your variables won't expand inside of single quotes. Put both patterns into one set of double quotes.

Finally, you shouldn't put the command inside of brackets. Something like this should work:

Code:
if egrep -qw "$D|$L" testfile
then
    execute some command
fi

This User Gave Thanks to agama For This Post:
# 6  
Old 06-27-2011
Quote:
if grep -qw '"$D"|"$L"' testfile
It is hard to guess what you intended from faulty code. Please describe the condition in words.
Things to bear in mind.
Variables enclosed by single quote characters are not expanded.
The "grep" command does not recognise "|" as meaning "or". The "egrep" command does.
A command sequence in an "if" statement does not execute automatically. It needs either backticks or $( ) command syntax.
# 7  
Old 06-27-2011
Quote:
Originally Posted by methyl
A command sequence in an "if" statement does not execute automatically. It needs either backticks or $( ) command syntax.
Beg to differ, but at least in Kshell and bash a command executes without $(..) or `..` when placed on an if.

Code:
spot: ls >/tmp/x
spot: if grep -q foo /tmp/x
> then
> echo true
> fi
true


Last edited by agama; 06-27-2011 at 01:47 AM.. Reason: typo
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Searching for file types by count in specific folder in RHEL 6

So I'm trying to search for the top 10 or 15 items under a directory by file type. I want to run a command on a directory and get something like the following: Example of expected output.. .PDF: 100, .txt: 95, .word: 80.. What would be the best way of going about this? I've searched around... (2 Replies)
Discussion started by: shackle101
2 Replies

2. UNIX for Dummies Questions & Answers

Search for a specific String in a log file for a specific date range

Hi, I have log file which rolls out every second which is as this. HttpGenRequest - -<!--OXi dbPublish--> <created="2014-03-24 23:45:37" lastMsgId="" requestTime="0.0333"> <response request="getOutcomeDetails" code="114" message="Request found no matching data" debug="" provider="undefined"/>... (3 Replies)
Discussion started by: karthikprakash
3 Replies

3. Shell Programming and Scripting

Searching a string in a particular file name

Hello, I have a file name like FIRST_DPF_DAILY_CUST_0826152322.txt i need to extract the string after the third "_" underscore upto timestamp ends i.e CUST_0826152322 can anyone help me with the code Thank you! Regards Srikanth Sagi (3 Replies)
Discussion started by: srikanth_sagi
3 Replies

4. Red Hat

Moving of file content to another two files after searching with specific pattern

Hello, Please help me with this!! Thanks in advance!! I have a file named file.gc with the content: 1-- Mon Sep 10 08:53:09 CDT 2012 2revoke connect from FR2261; 3delete from mkt_allow where grantee = 'FR2261'; 4grant connect to FR2261 with '******'; 5alter user FR2261 comment... (0 Replies)
Discussion started by: raosr020
0 Replies

5. Shell Programming and Scripting

searching each file for a string

Hi Guys... I want to search for each file that contains a particular string. e.g find . -print | xargs grep -i string_name Now my issue is the files that I search in are gzipped. Will I be able to find the string, using the above commands, even if the files are gzipped? Please... (2 Replies)
Discussion started by: Phuti
2 Replies

6. Shell Programming and Scripting

Searching a specific line in a large file

Hey All Can any one please suggest the procedure to search a part of line in a very large file in which log entries are entered with very high speed. i have trued with grep and egrep grep 'text text text' <file-name> egrep 'text text text' <file-name> here 'text text text' is... (4 Replies)
Discussion started by: NIMISH AGARWAL
4 Replies

7. UNIX for Dummies Questions & Answers

searching for a string in a file

I need to search for a specific string in a file and if this string exist I need to replace it with something else. I am not sure how I could do this, using an if statement. (2 Replies)
Discussion started by: ROOZ
2 Replies

8. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

9. Shell Programming and Scripting

grep - searching for a specific string

ppl, this is my "file" with fields orderno orderdate orderdesc telno street city 1 01/04/2006 abc 123 100 tampa 2 01/04/2006 abc 123 100 tampa 3 01/04/2006 abc 123 100 tampa 4 01/04/2006 abc ... (2 Replies)
Discussion started by: manthasirisha
2 Replies

10. Shell Programming and Scripting

Searching for a specific string in an argumnet

I want to check the second argument for a specific string . The code below is what I am trying, but I get: UX:test (./test): ERROR: { if ($0 ~ /StringImLooking4/) {print $1} }: Unknown operator I want to test if the second argument contains the string StringImLooking4 Unixware 7 ... (1 Reply)
Discussion started by: dinplant
1 Replies
Login or Register to Ask a Question