LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 02-15-2013, 05:45 AM   #1
ip_address
Member
 
Registered: Apr 2012
Distribution: RedHat
Posts: 42

Rep: Reputation: 2
grep for a variable with boundary conditions


Hello everyone,

How could I grep for a variable curr_line with boundary conditions? Thanks!

# variable named curr_line

Code:
curr_line=$(sed -n "$counter"p "$file")
# want to grep for exact match with boundary conditions

Code:
grep -w "^$curr_line$" "$index_file"
 
Old 02-15-2013, 05:52 AM   #2
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Is what you have there not working?
 
Old 02-15-2013, 05:54 AM   #3
ip_address
Member
 
Registered: Apr 2012
Distribution: RedHat
Posts: 42

Original Poster
Rep: Reputation: 2
Code:
grep -w "^$curr_line$" "$index_file"
doesn't work
 
Old 02-15-2013, 06:45 AM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

How about -x flag (man grep):
Quote:
-x, --line-regexp
Select only those matches that exactly match the whole line. (-x is specified by POSIX.)
Beware however that $curr_line may contain special symbols such as `.' (any character in regular expressions syntax) which may lead to misbehaviour.

Last edited by firstfire; 02-15-2013 at 06:49 AM.
 
Old 02-17-2013, 05:51 PM   #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
"Doesn't work" tells us nothing. In what way doesn't it work? Do you get any errors? Output? Have you tried any variations? What exactly did you run, and what exactly did you get?

Also, we really need to see an example of the input, including the line you want to match and at least a few of the surrounding ones.

In short, the more information you provide, the more likely you are to get real help.
 
Old 02-18-2013, 02:09 AM   #6
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
I remember having problems with such a construct: "^$curr_line$", but using "^$curr_line"$ worked.
 
Old 02-18-2013, 06:11 AM   #7
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by ip_address View Post
Code:
curr_line=$(sed -n "$counter"p "$file")
Please don't tell my you are using a counter and a repeated invocation of sed to read a file line by line… If you do, you just changed your script to be O(n²) instead of O(n). Consider instead:
Code:
while read curr_line; do
    # …
done <$file
Quote:
Originally Posted by ip_address View Post
Code:
grep -w "^$curr_line$" "$index_file"
Second dollar sign should be escaped with a slash, ie. "^$curr_line\$".

Finally, in addition to -x you may also be interested in -F switch.
 
1 members found this post helpful.
  


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 variable question graphicsmanx1 Programming 11 12-06-2012 02:16 PM
using Variable in grep in perl KManepalli Linux - Newbie 5 08-10-2011 08:13 AM
How do you grep a variable? dbrazeau Programming 12 03-11-2010 08:57 PM
Grep variable with space brainlesseinstein Linux - General 3 08-09-2009 11:28 PM
Using a variable containing a filename in grep TrumpetMan258 Programming 2 03-01-2008 01:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:15 AM.

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