Oops... with the original problem. -----Original Message----- From: Wyatt Song Sent: Wednesday, 9 October 2002 11:40 AM To: sunmanagers@sunmanagers.org Subject: SUMMARY: Processing of text Thanks for all the help! The following people provided good insight which allowed me to learn a lot. The solutions are listed, and they illustrate the possibility of using different approaches to the same problem. Thanks Wyatt ====== Acknowledgement ====== John Leadeham [jl@lammtarra.fslife.co.uk] Scott Howard [scott@doc.net.au] Steve Sandau [ssandau@bath.tmac.com] Mark Donaldson [Mark.Donaldson@experianems.com] Fabrice Guerini [fabrice@life.net] Baumler Julie L [julie.x.baumler@co.multnomah.or.us] ======== Original Problem ========= > Take a man page as an example, text are divided into sub-sections like > > NAME, SYNOPSIS, DESCRIPTION...FILES; SEE ALSO; NOTES; BUGS etc. > > What is the best way to filter out text for one particular section. Say if > I wanted to only look at SEE ALSO section of a man page without scrolling > down to the bottom. I thought of either: > 1. use an editor/viewer, for example: # man awk | less > 2. use a script to process the text. > > I'm really interested to find a solution using the 2nd method, but I'm not a > scripting expert, so any help is appreciated. ======== Solution 1 ========= set the environment variable PAGER to less, less takes options (either -p or / iirc) to search for a particular pattern on startup, so you could have a script/alias/shell-function called manalso or ma that does PAGER="less -p'SEE ALSO'" man whatever OLE_LINK1======== Solution 2 ========= perl -e '$/="\0"; $_=<>; print $1 if (m/^(DESCRIPTION.*?)^[A-Z]{5}/ms);' That will print from the title given (eg, DESCRIPTION in this case) up to the start of the next section (in particular, the next line which has at least 5 uppercase chars at the start of the line). eg [18:26:23][@milliways /tmp]$ man printf | perl -e '$/="\0"; $_=<>; print $1 if ( m/^(DESCRIPTION.*?)^[A-Z]{5}/ms);' Reformatting page. Please Wait... done DESCRIPTION The printf command writes formatted operands to the standard output. The argument operands are formatted under control of the format operand. OLE_LINK2======== Solution 3 ========= 'man' command invokes a paginator on its own; you do not need to call less explicitly. running 'man awk' and then hitting '/' to search and typing the text you want to search for will work. As for a more elegant solution, there is a way to runn troff or nroff to reformat the page wihout the inclusion of a paginator. You could then pipe the output of that to sed and ask it to display the text beginning with the section you want to view: 'nroff -???? /usr/man/sman1/awk.1 | sed -n /"SEE ALSO"/./$/p | less' OLE_LINK3 ======== Solution 4 ========= awk 'BEGIN {flag=0} { if ( $0~/STARTPAT/ ) {flag=1} if ( $0~/ENDPAT/ ) {flag=0} if ( flag==1 ) {print} } ' inputfile This'll print the lines between STARTPAT and ENDPAT for the inputfile. the "STARTPAT" line is included in the output but the "ENDPAT" line is not. you can include the ENDPAT line by moving it below the print line if you wish. ======== Solution 5 ========= man ls | sed -e 's/.^H//g' | sed -e '1,/SEE ALSO/d' -e '/^[A-Z]/,$d' (make sure to replace the "^H" with an actual backspace) ======== Solution 6 ========= I like to read the special install instructions in patches (which is usually the last thing there) to do so, I use the following command: sed -n '/Special Install Instructions:/,$ p' <file> For a man page, if you wanted to read from pattern1 (ie. SEE ALSO) to pattern 2 (ie. NOTES), you could do the following: man cmd | sed -n '/pattern1/,/pattern2/ p' Or in your case: man ls | sed -n '/SEE ALSO/,/NOTES/ p' This does include the line that says NOTES, you can do some further scripting to remove that, but this is short enough to remember. _______________________________________________ sunmanagers mailing list sunmanagers@sunmanagers.org http://www.sunmanagers.org/mailman/listinfo/sunmanagersReceived on Tue Oct 8 21:53:33 2002
This archive was generated by hypermail 2.1.8 : Thu Mar 03 2016 - 06:42:56 EST