LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-28-2013, 06:10 AM   #16
sparky90ful
Member
 
Registered: Sep 2013
Posts: 30

Original Poster
Rep: Reputation: Disabled

Quote:
Originally Posted by Firerat View Post
simpler grep and cut

Code:
KEY=foo
grep "^${KEY}=" sh.db | cut -d= -f2
it still find me one result where the word searched is exactly the same
i want to print the value fore anyone key which include the foo string but if the foo string is a value i don't want it do you understand what i need?
the keys could be more could be foo foor foore cofoo drooofoo i want to print the values of them but if a key pppp= food i dont want it
 
Old 09-28-2013, 06:16 AM   #17
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
yes,

grep "^${KEY}="

should do that.

the shell should expand it to
"^foo="


the ^ means "the beginning of the line"
so it should only show lines that start

foo=


Edit to follow:

grep "^.*${KEY}.*="

again, ^ works the same
. represent any character
* is the previous character repeated 0 or more times
so it would match

foo=
foobar=
barfooooo=

Last edited by Firerat; 09-28-2013 at 06:22 AM.
 
Old 09-28-2013, 06:18 AM   #18
sparky90ful
Member
 
Registered: Sep 2013
Posts: 30

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
yes,

grep "^${KEY}="

should do that.

the shell should expand it to
"^foo="


the ^ means "the beginning of the line"
so it should only show lines that start

foo=
and if my line start
dfoo=
i want that value to be printed too
 
Old 09-28-2013, 06:26 AM   #19
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Quote:
Originally Posted by sparky90ful View Post
and if my line start
dfoo=
i want that value to be printed too
see edit
 
Old 09-28-2013, 07:16 AM   #20
sparky90ful
Member
 
Registered: Sep 2013
Posts: 30

Original Poster
Rep: Reputation: Disabled
I wrealy cant't do it please help me give me un example for this plese
Code:
the thing is for example i want to serch 1 so i want cuz he find me the lines where is 1 but before " = " so 
i dont wnt cuz he find <a=1> or <ddd=321> 
i wat to find <1=ghfh> or <211=op14> or <421=kj5417> and when he find this print what is after " = " 
so the result will be " ghf " " op14 " " kj5417 " and that is  i hope now is god explained
please please please
 
Old 09-28-2013, 07:36 AM   #21
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Code:
cat > Test.db << EOF
a=1
ddd=321 
1=ghfh
211=op14
421=kj5417
EOF

KEY="1"
echo "absolute Match to field 1" ;grep "^${KEY}=" Test.db
echo "field 1 contains the string in \$KEY" ;grep ".*${KEY}.*=" Test.db
echo "field 1 does not contain the string in \$KEY"; grep -v ".*${KEY}.*=" Test.db
 
1 members found this post helpful.
Old 09-28-2013, 08:01 AM   #22
sparky90ful
Member
 
Registered: Sep 2013
Posts: 30

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
Code:
cat > Test.db << EOF
a=1
ddd=321 
1=ghfh
211=op14
421=kj5417
EOF

KEY="1"
echo "absolute Match to field 1" ;grep "^${KEY}=" Test.db
echo "field 1 contains the string in \$KEY" ;grep ".*${KEY}.*=" Test.db
echo "field 1 does not contain the string in \$KEY"; grep -v ".*${KEY}.*=" Test.db


i dont know how to thanks you man you are amazing im looking for this since 6 hours i realy realy thank you
 
  


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
How to delete single line from file using line number. gajananh999 Linux - Newbie 1 08-13-2012 06:25 AM
script to read and delete a line from a file, if the line is 30 days old freakin.raja Linux - Newbie 7 09-17-2011 09:52 AM
Perl question: delete line from text file with duplicate match at beginning of line mrealty Programming 7 04-01-2009 07:46 PM
delete a line containing a pattern and the next line of a text file powah Programming 3 01-31-2007 06:34 PM
Delete line from file nebbus Linux - Newbie 3 06-03-2006 01:46 PM


All times are GMT -5. The time now is 08:28 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration