- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: matching a line with a string at start and end
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2008 01:53 AM
04-29-2008 01:53 AM
1) Starts with "#printer"
- may have spaces/tabs between # and printer
2) ends with in.lpd
Here's my code...I don't like it and want to improve on it....looking for ideas.
if [ ${isLP} -eq 0 ] ; then
/usr/xpg4/bin/grep "printer" ./string.txt | \
/usr/xpg4/bin/grep "in.lpd" | \
/usr/xpg4/bin/grep "^#"
isLPDset=$? # If = 0 then line found
fi
Solved! Go to Solution.
- Tags:
- grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2008 02:21 AM
04-29-2008 02:21 AM
Re: matching a line with a string at start and end
Something like :
FOUND=$(egrep -c "^#[[:space:]]*printer.*in\.lpd$" ./string.txt)
if [ $FOUND -eq 0 ]
then echo not found
else echo found $FOUND time(s)
fi
Eric
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2008 02:36 AM
04-29-2008 02:36 AM
Re: matching a line with a string at start and end
Why use the egrep hammer? grep should work fine:
grep -q "^#[ ]printer.*in\.lpd" string.txt
(Inside the [ ] is a space and a tab.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2008 02:43 AM
04-29-2008 02:43 AM
Re: matching a line with a string at start and end
grep -q "^#[ ]printer.*in\.lpd$" string.txt
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2008 02:45 AM
04-29-2008 02:45 AM
Re: matching a line with a string at start and end
It's just not working. Tried on HP-UX and Solaris.
Fyi if on Solaris need to use /usr/xpg4/bin/grep for the -q option to work.
I've done this before with grep which is the frustrating thing.
Will post solution when I figure this out.
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2008 02:53 AM
04-29-2008 02:53 AM
Re: matching a line with a string at start and end
Here's the data I'm trying to match
#printer stream tcp6 nowait root /usr/lib/print/in.lpd in.lpd
Maybe I miss represented something?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2008 02:53 AM
04-29-2008 02:53 AM
Re: matching a line with a string at start and end
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2008 03:02 AM
04-29-2008 03:02 AM
Re: matching a line with a string at start and end
You are confused. :-) The -q option works fine on HP-UX. If you mention Solaris, the thread gets deleted.
>groan...misrepresented
Did you find what you needed?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2008 03:05 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2008 03:46 AM
04-29-2008 03:46 AM
Re: matching a line with a string at start and end
I would say why not ?! It doesn't matter a lot, no ? I am a bit lazy, so I always use egrep. I have totally forgotten that grep exists ;-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2008 03:47 AM
04-29-2008 03:47 AM
Re: matching a line with a string at start and end
How about this:
# LINE=" # printer kevins.lpd"
...where there are spaces and tabs wherever you see whitespace.
# echo ${LINE}}|perl -ne 'exit !(m/\s*#\s*printer.*\.lpd$/)';echo $?
...where the return status of the Perl snippet is 0 for a match and 1 for no match (just as you wanted).
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2008 03:52 AM
04-29-2008 03:52 AM
Re: matching a line with a string at start and end
Oops, I forgot to have this post toggle the retain formatting so the whitespaces are lost!
# LINE=" # printer kevins.lpd"
...where there are spaces and tabs wherever you see whitespace.
# echo ${LINE}}|perl -ne 'exit !(m/\s*#\s*printer.*\.lpd$/)';echo $?
...where the return status of the Perl snippet is 0 for a match and 1 for no match (just as you wanted).
Now, too, if by the "end of the line" you mean a "sloppy" one with trailing whitespace, just do:
# echo ${LINE}}|perl -ne 'exit !(m/\s*#\s*printer.*\.lpd\s*/)';echo $?
...that is replace the '$' anchor with '\s*' meaning zero or more whitespace characters.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2008 03:56 AM
04-29-2008 03:56 AM
Re: matching a line with a string at start and end
"^#[[:space:]]*printer.*in\.lpd$"
or
"^#[ ]*printer.*in\.lpd$"
(Inside the [ ] is a space and a tab.)
should match on HP-UX ... if Kevin gave us the exact line.
Well, assuming there is an error, I think it could be at the end of the line : trailer spaces ...
So, Kevin, try this regexp :
"^#[[:space:]]*printer.*in\.lpd[[:space:]]*$"
with or without "egrep" ;-), with or without "-q", and tell us ...
Eric
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2008 04:09 AM
04-29-2008 04:09 AM
Re: matching a line with a string at start and end
Dennis and Eric I combined your feed back into the answer!!
grep -q "^#[[:space:]]*printer.*in\.lpd$" string.txt
Then I simply capture the return code into a variable and can do logic based on whether the line is present.
Thank you all very much
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2008 04:12 AM
04-29-2008 04:12 AM
Re: matching a line with a string at start and end
grep "^#[ ]*printer.*in\.lpd$" string.txt
#printer stream tcp6 nowait root /usr/lib/print/in.lpd in.lpd
The last one from Eric also works correctly - but use without -q
grep "^#[[:space:]]*printer.*in\.lpd[[:space:]]*$" string.txt
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2008 11:19 AM
04-29-2008 11:19 AM
Re: matching a line with a string at start and end
There might be performance issues of lots of strings and lines. In this case it probably doesn't matter.
>Anyway, the regexp "^#[[:space:]]*printer.*in\.lpd$"
should match on HP-UX
Yes, your initial pattern works for grep. I typically don't use character class often enough to remember how to spell them.