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?
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 When I have a something that requires more thinking I will be sure to post data.