Computing.Net > Forums > Unix > Unix Commad

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Unix Commad

Reply to Message Icon

Name: Janet
Date: December 27, 2002 at 21:45:57 Pacific
OS: Sun Solaris
CPU/Ram: P4/2GB
Comment:


Hi
Could any tell me how to retrieve a result (oranges) between two lines of a file

eg

Apples (first line)
Oranges (second line)
grapes (third line)

I want to be able to get the second line information in this case oranges

Many thanks



Sponsored Link
Ads by Google

Response Number 1
Name: Jimbo
Date: December 28, 2002 at 04:13:41 Pacific
Reply:

sed -n '2p' your_file

will give you the second line.

-jim


0

Response Number 2
Name: David Perry
Date: December 28, 2002 at 06:32:27 Pacific
Reply:

head -2 filename | tail -1


0

Response Number 3
Name: James Boothe
Date: December 28, 2002 at 07:51:58 Pacific
Reply:

Maybe Janet is wanting the second line shown, where those set of lines could occur anywhere in the file. If that is the case, it becomes quite a bit more difficult.

The following script would do it, and to keep this web page from losing my indentation, I use underscores, so you would have to change those underscores back to spaces.

#!/bin/sh
awk '{\
if ($1=="grapes"&&holdflag==1)
___{print hold;exit}
if (apples==1)
___{hold=$0;holdflag=1}
else
___holdflag=0
if ($1=="Apples")
___apples=1
else
___apples=0
}' filename
exit 0


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Unix Commad

need help for learning unix script www.computing.net/answers/unix/need-help-for-learning-unix-script/7745.html

UNIX (file executed date) www.computing.net/answers/unix/unix-file-executed-date/8232.html

UNIX/changing date format www.computing.net/answers/unix/unixchanging-date-format/8440.html