LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-22-2013, 06:55 AM   #1
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Rep: Reputation: 33
A question about grep string


Hi,

In my script I use the 'hostname' to get the hostname of the machine. But I have to check if the got name contains what I am looking for.

Assumed the 'hostname' returns s001ap12.mycompany.com and I want to greo the 's001ap12', how can I do this?

I did as follow but failed:

Code:
...
hostname|grep "s001ap12$" > /dev/null
if [[ $? -eq 0 ]]; then
  echo "this is S001ap12"
  RC=0
fi
Sometimes the hostname could be 'S001IS12', i.e. the grep should be not-case-sensitive.

Last edited by thomas2004ch; 04-22-2013 at 07:05 AM.
 
Old 04-22-2013, 07:16 AM   #2
david1941
Member
 
Registered: May 2005
Location: St. Louis, MO
Distribution: CentOS7
Posts: 267

Rep: Reputation: 58
try the -i flag to ignore case
 
1 members found this post helpful.
Old 04-22-2013, 08:17 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Why the trailing '$' in the search string?
 
1 members found this post helpful.
Old 04-22-2013, 08:29 AM   #4
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Original Poster
Rep: Reputation: 33
The problem is solved.
 
Old 04-23-2013, 09:52 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
There should be no need to use grep for this purpose. Just use your shell's built-in text string comparsion tests.


To start with, you haven't mentioned what scripting environment you're talking about. Some shells provide a built in $HOSTNAME envrionment variable for you already. But if not, capture the hostname command into to one.
Code:
host=$( hostname )
Next, there are several different ways to go about testing the string, but I pesonally recommend a case statement, which is fully portable and usually the most efficient.

Code:
case $host in		           #or $HOSTNAME, if available
    s001ap12.*|S001AP12.*)
        echo "this is S001ap12"
        RC=0
    ;;
esac
Another benefit is that you can add as many entries as you need, if you have to test multiple hosts.

If you're using bash, you can more easily make it case-insensitive by converting the input with the appropriate parameter substitution.

Code:
case ${host,,} in		#or $HOSTNAME, if available
    s001ap12.*)
        echo "this is S001ap12"
        RC=0
    ;;
esac
You can also use bash's [[..]] expanded test brackets, which support both globbing and regular expression matches.

http://mywiki.wooledge.org/BashFAQ/031
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep question - match exact string Panagiotis_IOA Linux - General 2 01-20-2014 04:34 AM
how to grep for [string] spezticle Linux - General 7 04-02-2012 12:19 AM
[SOLVED] Issue trying to grep a string, but keep a similar string Supp0rtLinux Linux - Software 7 10-04-2011 06:35 PM
grep a particular string john83reuben Linux - Newbie 3 12-07-2009 09:34 AM
grep string with space (2 word string) casperdaghost Linux - Newbie 7 08-24-2009 02:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration