nixCraft Linux / UNIX / Shell Scripting Forum

nixCraft

Linux / UNIX Tech Support Forum

Unix Shell: Check if Word exist in file or not

This is a discussion on Unix Shell: Check if Word exist in file or not within the Shell scripting forums, part of the Development/Scripting category; Hello, I want to check if some word exist or not on some file By Example : word is : ...


Register free or login to your account to remove all advertisements.

Go Back   nixCraft Linux / UNIX / Shell Scripting Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

Thread Tools Display Modes
  #1 (permalink)  
Old 7th November 2008, 01:37 AM
Junior Member
 
Join Date: Oct 2008
OS: Debian
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
LinuxCommandos is on a distinguished road
Default Unix Shell: Check if Word exist in file or not

Hello,
I want to check if some word exist or not on some file

By Example :
word is : nixcraft
file called : /root/shell.txt

and i want to check if nixcraft word exist on /root/shell.txt file with if statement or another tool

Any Ideas
Reply With Quote
  #2 (permalink)  
Old 7th November 2008, 02:50 AM
nixcraft's Avatar
Never say die
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash, Perl, Python
Posts: 3,195
Thanks: 13
Thanked 394 Times in 292 Posts
Rep Power: 10
nixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond repute
Default

try something as follows:
Code:
#!/bin/bash
FILE=/root/shell.txt

grep -w "nixcraft" $FILE >/dev/null

if [ $? -eq 0 ]
then
   echo "Word found!"
else
   echo "Word NOT found!"
fi
__________________
Vivek Gite
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Do you run a Linux? Let's face it, you need help!
Cricket & IPL News Blog
Reply With Quote
Reply

Tags
if command, linux, shell scripting, unix


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
find a word in a file, and change a word beneath it ?? vikas027 Shell scripting 3 10th June 2010 09:38 AM
Unix command to check memory usage sweta HP-UX 3 24th February 2010 10:54 PM
shell script to search specific file from txt file inside zip file and extract it aasif.shaikh Shell scripting 2 31st May 2008 06:44 PM
word search in file chandanperl Coding in General 2 11th March 2008 10:16 AM
HP unix check filesystem for errors jerry HP-UX 0 9th June 2007 01:49 AM


All times are GMT +5.5. The time now is 08:51 PM.


Powered by vBulletin® Version 3.8.6 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40