Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 08-30-2005, 07:49 PM   #1
Registered: Feb 2004
Posts: 34

Rep: Reputation: 15
Bash Question

I am trying to create a bash script that will search through a file for 2 keywords and then capture the information listed after that keyword.

I want to search the file for the words Product Names: and then capture the information listed to the right of that. Is it also possible to only find the first instance of this?

I have done a bit of searching but can't really find what i'm looking for. Any help would be appreciated.
Old 08-30-2005, 07:56 PM   #2
Senior Member
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 50
this will print the whole line:
grep -m 1 'Product Names:' <file>
Old 08-30-2005, 08:10 PM   #3
Registered: Feb 2004
Posts: 34

Original Poster
Rep: Reputation: 15
Thanks for that spooon. How would I actually get that into a bash script?
How do I say: if the output of grep -m 1 'Product Names:' <file> is = to something then do <something> but if it is = to something else then do <something else>??
Old 08-30-2005, 09:49 PM   #4
Registered: Feb 2004
Posts: 34

Original Poster
Rep: Reputation: 15
Ok, I'm making some progress. So far I have:


hwmodel=$(grep -m 1 Product /tmp/data.txt)

if [ "$hwmodel" = "Product Name: <product name>" ]
  echo "<product name>"

exit 0
It's not actually coming back with my product name .. it returns nothing. When I echo back "-$hwmodel-" it appears to have a tab or a series of spaces before and after the product name. Is there anyway to get rid of those??
Old 08-31-2005, 04:29 AM   #5
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 5,390

Rep: Reputation: 409Reputation: 409Reputation: 409Reputation: 409Reputation: 409
Maybe try :
hwmodel=$(grep -m 1 Product /tmp/data.txt | sed -e 's/\s//g')
Assuming there is no space in your product name (?)
Old 08-31-2005, 04:33 AM   #6
Senior Member
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 200Reputation: 200Reputation: 200
How about a bit of sed?

little sed script
$ cat mbjunior
#!/usr/bin/sed -nf

# -n turns off default print, which would output all other lines

/Product Name:/ {            # search for this
    s///                     # substitute searched item for no string
    p                        # print what's left
$ cat 2
# a list of products
# ==================
Product Name: shoes
Product Name: large fishes
Product Name: rabbit trousers
$ mbjunior < 2
 large fishes
 rabbit trousers


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
A question about BASH operand Linux - Newbie 2 11-27-2005 09:49 PM
BASH question robscott Linux - Software 3 11-23-2005 09:39 AM
Bash question lnxduck Programming 3 09-30-2005 08:50 AM
bash question? shanenin Linux - Software 1 11-26-2004 12:22 PM
little bash question gplacek Linux - General 2 09-01-2004 12:10 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:37 PM.

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