Operating System - Linux
1827880 Members
1514 Online
109969 Solutions
New Discussion

Re: matching a line with a string at start and end

 
SOLVED
Go to solution
OFC_EDM
Respected Contributor

matching a line with a string at start and end

I want to check for the presence of a line that

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
The Devil is in the detail.
15 REPLIES 15
Eric SAUBIGNAC
Honored Contributor

Re: matching a line with a string at start and end

Bonjour


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
Dennis Handly
Acclaimed Contributor

Re: matching a line with a string at start and end

>Eric: FOUND=$(egrep -c "^#[[:space:]]*printer.*in\.lpd$" ./string.txt)

Why use the egrep hammer? grep should work fine:
grep -q "^#[ ]printer.*in\.lpd" string.txt

(Inside the [ ] is a space and a tab.)
Dennis Handly
Acclaimed Contributor

Re: matching a line with a string at start and end

Oops, if you want it to end with exactly in.lpd, you need Eric's $ anchor:
grep -q "^#[ ]printer.*in\.lpd$" string.txt
OFC_EDM
Respected Contributor

Re: matching a line with a string at start and end

Dennis that's exactly where I started this morning.

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!
The Devil is in the detail.
OFC_EDM
Respected Contributor

Re: matching a line with a string at start and end

Tried the anchor too.

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?
The Devil is in the detail.
OFC_EDM
Respected Contributor

Re: matching a line with a string at start and end

groan...misrepresented
The Devil is in the detail.
Dennis Handly
Acclaimed Contributor

Re: matching a line with a string at start and end

>FYI: if on Solaris need to use /usr/xpg4/bin/grep for the -q option to work.

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?
Dennis Handly
Acclaimed Contributor
Solution

Re: matching a line with a string at start and end

Double oops, I forgot the * after the whitespace:
grep -q "^#[ ]*printer.*in\.lpd$" string.txt
Eric SAUBIGNAC
Honored Contributor

Re: matching a line with a string at start and end

Dennis "Why use the egrep hammer?"

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

James R. Ferguson
Acclaimed Contributor

Re: matching a line with a string at start and end

Hi:

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...
James R. Ferguson
Acclaimed Contributor

Re: matching a line with a string at start and end

Hi (again):

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



Eric SAUBIGNAC
Honored Contributor

Re: matching a line with a string at start and end

Anyway, the regexp

"^#[[: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

OFC_EDM
Respected Contributor

Re: matching a line with a string at start and end

James I didn't use perl this time as I'm handing this over to someone who's allergic to it :)

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
The Devil is in the detail.
Rasheed Tamton
Honored Contributor

Re: matching a line with a string at start and end

If you do not want perl, use the last one from Dennis but use it without the -q.

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

Dennis Handly
Acclaimed Contributor

Re: matching a line with a string at start and end

>Eric: I would say why not?! It doesn't matter a lot, no?

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.