LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-01-2007, 02:07 AM   #1
dhanabalanb
LQ Newbie
 
Registered: Jul 2007
Posts: 6

Rep: Reputation: 0
comparing 2 strings in shell script


Hi,

My file has entries as follows:

root:x:678:79789:home
welcome:x:698:79489:temp
welcometomyhome:x:698:79489:tmp

And I have a sting welcome stored in a variable called variable. I want to check for the string stored in variable through shell scipt. It should only give me welcome from my file. It should not give welcometomyhome (but this also has welcome in it). Can anyone please help me in this regard?

Thank you,
Dhanabalan.
 
Old 08-01-2007, 03:03 AM   #2
ljbuturovic
LQ Newbie
 
Registered: Jan 2006
Posts: 3

Rep: Reputation: 0
Assuming your text file is named `welcome.txt',
create a script named welcome.sh:

#!/bin/sh
variable="welcome"
export string=$variable
awk -F: '$1 == ENVIRON["string"]' welcome.txt

After making the script executable:

$ ./welcome.sh
welcome:x:698:79489:temp

Ljubomir
 
Old 08-01-2007, 05:58 AM   #3
gjagadish
Member
 
Registered: Oct 2005
Location: India
Distribution: Fedora Core
Posts: 88

Rep: Reputation: 15
The below code will print the exact line you wanted

------------------------------
#!/bin/bash
variable="welcome"
grep "^$variable\:" welcome.txt
--------------------------------
 
Old 08-01-2007, 01:17 PM   #4
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
Hi.

Some versions of grep have a "word" search option, "-w":
Code:
#!/bin/sh

# @(#) s1       Demonstrate word-search in grep.

set -o nounset
echo
echo "GNU bash $BASH_VERSION" >&2
grep --version | head -1 >&2
echo

variable="welcome"

cat >data1 <<EOF
root:x:678:79789:home
welcometomyhome:x:698:79489:tmp
welcome:x:698:79489:temp
EOF

grep -w "$variable" data1

exit 0
producing:
Code:
% ./s1

GNU bash 2.05b.0(1)-release
grep (GNU grep) 2.5.1

welcome:x:698:79489:t
See man grep for details ... cheers, makyo

Last edited by makyo; 08-01-2007 at 01:20 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Shell script for comparing certain lines in two files mou5e Linux - Newbie 9 06-06-2007 01:40 PM
comparing timestamps using shell commands farkus888 Programming 6 05-03-2007 02:27 AM
Shell Script Strings - trim revof11 Programming 3 11-30-2006 09:46 AM
Shell script to compare blocks of strings? bruno buys Programming 10 04-15-2006 02:16 PM
Error comparing strings and acting on the comparison WindowsBurner Programming 4 10-21-2004 12:37 PM


All times are GMT -5. The time now is 07:42 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration