LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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-30-2005, 07:49 PM   #1
mbjunior99
Member
 
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
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 48
this will print the whole line:
Code:
grep -m 1 'Product Names:' <file>
 
Old 08-30-2005, 08:10 PM   #3
mbjunior99
Member
 
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
mbjunior99
Member
 
Registered: Feb 2004
Posts: 34

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

Code:
#!/bin/bash

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

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

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
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282

Rep: Reputation: 66
Maybe try :
Code:
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
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,276

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

little sed script
Code:
$ 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
}
data
Code:
$ cat 2
# a list of products
# ==================
Product Name: shoes
Product Name: large fishes
Product Name: rabbit trousers
END
result
Code:
$ mbjunior < 2
 shoes
 large fishes
 rabbit trousers
 
  


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
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


All times are GMT -5. The time now is 04:51 AM.

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