finding the nth match

I have a file that has information for a person....each person gets 3 or more lines to describe them.

I was hoping to match person 1, then person 2.....BUT I do not know how to tell grep I only want the first (2nd, 3rd or nth) match.

The alternative is doing line by line logic, which is fine with me as long as I know that is what I am supposed to be doing.

OS: Solaris 10

help?

thanks,

cs

If you post your input file and sample expected output that would be more helpful.

not really...but here it goes.
Make up any scenario you want....how about where each line starts with a single digit.

1
5
9
7
5

I want grep to get the the nth match of a digit matched via regex
using solaris

Make up the scenario? Is this an actual problem or are you just quizzing us?

We often get questions about getting grep to cross multiple lines, but grep doesn't. But a 1-liner in awk might be able to do it.

We can't help you until we know how your data's organized because the logic depends on that. It's not psychic and neither are we.

1 Like

Yeah....I am a wanna be teacher and fulfill my fantasies by quizzing people on the internet. Go get a life....you are being difficult.

Maybe you answered my question....but you sound generally uninformed because you are being difficult: grep will not provide the nth match only.

can anyone else confirm?

If you'd posted your data you could have had an answer an hour ago...

I'll ignore the personal slur and explain a little more.

grep considers no logic but patterns when matching lines. It doesn't remember anything about previous lines and has no expression to carry information from previous lines across to next ones. The awk language is much better suited, since it deals with lines/patterns and has variables plus logical expressions. (I think sed does too in a fashion, but its expression syntax is rather convoluted. awk gives you straightforward variables with names, and straightforward expressions with if/else.)

I'm only asking you for your data. I'm not even the first one.

If you really want I'll give you a ridiculous solution like grep pattern file | tail -n +5 | head -n 1 to get match 4 which is of course a very silly solution and might not work in Solaris. A less silly solution would be nawk '/pattern/ {} NR==2' but you asked for grep, this may not suit your needs, and there may be even more efficient ways to deal with the data depending on what it actually is and what you're trying to do.

You slur...I slur back....that is how that first part went......anyways...

See....you did not need data to answer and there was no problem adding on the excellent commentary/lead about awk. I know a lot of people often ask for the answer to their specific issue and want YOU the other poster to do all the work for them (sometimes very practical). I am still at the general question looking to get more of a clue phase but if you want to do all my work for me let me know...I am certain you can do it fast.

I have something to work with and can make some progress (fingers crossed).

Thank you so much: )

---------- Post updated at 12:09 PM ---------- Previous update was at 12:03 PM ----------

had to change the line for Solaris:

grep pattern file | tail +5 | head -1

I am not working with large files here and performance is not an issue......

The line is easy to use and works in the Bash shell so....why is this a silly solution, performance, crossplatform issues?

thanks again.

If I lucked out into something that did exactly what you wanted the very first try, then I lucked out. Getting people to explain what they really want can be as difficult as -- well -- as difficult as getting you to post your data.

Simple questions shouldn't always be taken at face value, either

So frequent posters here develop some habitual questions. 1) Post your OS (thank you for that), 2) Post your data! It makes things a lot smoother all around.

You know the saying, a bird in the hand is worth two in the bush? It doesn't have to be either/or. I can make a simple example that works with your data without doing all your work for you, which will look more straighforward than one for random data I had lying around.

Running one program instead of two is much better, and if you can avoid any programs, that's better yet! External programs take time to start and stop, even for tiny files -- especially for tiny files, where most of the time would be spent loading/quitting instead of work. And pipes can waste time communicating with each other (grep reads, writes, tail reads, writes, head reads, writes.) Both of these can add up faster than you'd expect. One of my first shell programming experiments was a text linewrapper that ran at the blazing speed of one kilobyte per second.

This is also why I'm curious what your program actually is and how you're using this 'get line x' code, because if grep | tail | head is your way of loading individual lines of data from file, you might want to ask about better ways to organize your program.

lol....don't beat yourself up over getting it right the first time. Your answered showed that you are intuitive and knew the simple solution and other possible ones. Me as the guy with a lot of windows scripting experience knew I was not going to jerk everyone around by not providing the data because the answer was simple

A customer goes into a hardware store and asks for a hammer....the sales guys says "what type of hammer? We have 120 different kinds!" and proceeds to name at least a dozen......the customer looks all frazzled and leaves......"all I wanted to do was put a nail in a board".

I am very happy I got my hammer :slight_smile: When I have a something that requires more thinking I will be sure to post data.

thanks!

.