Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Check a specific word whether is in file

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-21-2011   -   Original Discussion by alvin0618
alvin0618's Unix or Linux Image
alvin0618 alvin0618 is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 25 March 2011, 3:46 AM EDT
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Check a specific word whether is in file

I want to valid a specific word whether it is contained in the text file. For example:

family.txt
father
mother
son
sister

Code:
if ("father" contain in family.txt)
     do some process
else
    do other process

How shell scripting can accomplish this? Anyone can provide helps?
Sponsored Links
    #2  
Old Unix and Linux 03-21-2011   -   Original Discussion by alvin0618
anishkumarv's Unix or Linux Image
anishkumarv anishkumarv is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 14 May 2013, 12:44 AM EDT
Posts: 96
Thanks: 15
Thanked 2 Times in 2 Posts
Hi dude,


Quote:
#/bin/bash
cat family.txt | grep father
s=`echo $?`
if [ $s = 1 ]
then
echo "the command is correct"
else
echo "the command is not correct"
fi
With Regards
Anish Kumar.v

Last edited by anishkumarv; 03-21-2011 at 04:41 AM..
Sponsored Links
    #3  
Old Unix and Linux 03-21-2011   -   Original Discussion by alvin0618
Aia's Unix or Linux Image
Aia Aia is offline
Registered User
 
Join Date: May 2008
Last Activity: 20 November 2017, 9:27 AM EST
Posts: 1,673
Thanks: 49
Thanked 641 Times in 601 Posts
Quote:
Originally Posted by alvin0618 View Post
I want to valid a specific word whether it is contained in the text file. For example:

family.txt
father
mother
son
sister

Code:
if ("father" contain in family.txt)
     do some process
else
    do other process

How shell scripting can accomplish this? Anyone can provide helps?


Code:
if [[ -n `grep -m 1 'father' family.txt` ]]; then
     # do some process
else
     # do some other process
fi

    #4  
Old Unix and Linux 03-21-2011   -   Original Discussion by alvin0618
alvin0618's Unix or Linux Image
alvin0618 alvin0618 is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 25 March 2011, 3:46 AM EDT
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Aia View Post

Code:
if [[ -n `grep -m 1 'father' family.txt` ]]; then
     # do some process
else
     # do some other process
fi

This is what i got

Code:
grep: illegal option -- m
Usage: grep -hblcnsviw pattern file . . .

May i know where did i do wrong?
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
C++ > check if a file contains a word... hakermania Programming 1 06-22-2010 11:42 PM
Search a specific word from any one of the file. ritusubash UNIX for Dummies Questions & Answers 1 05-17-2010 09:41 PM
Exclude specific word from input file problem asking... patrick87 Shell Programming and Scripting 5 05-16-2010 10:02 PM
To search a file for a specific word in a file using shell script girish.raos Shell Programming and Scripting 8 09-01-2009 07:51 AM
Remove a non-specific word froma file apjneeraj Shell Programming and Scripting 2 05-13-2009 01:36 AM



All times are GMT -4. The time now is 09:14 AM.