📄 ch35_16.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 35] 35.16 Make Columns Automatically with cols </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:48:18Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch35_01.htm"TITLE="35. You Can't Quite Call This Editing"><LINKREL="prev"HREF="ch35_15.htm"TITLE="35.15 Cutting Columns with colrm "><LINKREL="next"HREF="ch35_17.htm"TITLE="35.17 Making Text in Columns with pr "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch35_15.htm"TITLE="35.15 Cutting Columns with colrm "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 35.15 Cutting Columns with colrm "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 35<BR>You Can't Quite Call This Editing</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch35_17.htm"TITLE="35.17 Making Text in Columns with pr "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 35.17 Making Text in Columns with pr "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-4240">35.16 Make Columns Automatically with cols </A></H2><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="cols">cols</A><BR></TH><TDVALIGN="TOP"> If the output from some program runs down the left-hand side ofyour screen and takes more than one screen to display,you can pipe the program output to a<SPANCLASS="link">pager (<ACLASS="linkend"HREF="ch25_03.htm"TITLE="Using more to Page Through Files ">25.3</A>, <ACLASS="linkend"HREF="ch25_04.htm"TITLE='The "less" Pager: More than "more"'>25.4</A>)</SPAN>.If the lines of text are short, you can see more of that text on the screenat once by reformatting the text into columns.The <EMCLASS="emphasis">pr</EM> command can<SPANCLASS="link">make columns (<ACLASS="linkend"HREF="ch35_17.htm"TITLE="Making Text in Columns with pr ">35.17</A>)</SPAN>.But it's not easy to use if you want the input text to be ordered downa column instead of across columns.And it's tough to use if you want as many columns as will fit acrossyour screen - you have to find the widest piece of data before you canfigure each column's width.</TD></TR></TABLE><PCLASS="para">Some UNIX systems have a program specifically for making data intocolumns - but many don't.The <EMCLASS="emphasis">cols</EM> script takes care of that.It reads your text, finds the widest piece, and chooses <EMCLASS="emphasis">pr</EM> optionsto make as many columns as will fit on the screen.<EMCLASS="emphasis">cols</EM> also has seven other names-<SPANCLASS="link">links (<ACLASS="linkend"HREF="ch18_03.htm"TITLE="Files with Two or More Names">18.3</A>)</SPAN>named <EMCLASS="emphasis">c2</EM>, <EMCLASS="emphasis">c3</EM>, <EMCLASS="emphasis">c4</EM>, <EMCLASS="emphasis">c5</EM>, <EMCLASS="emphasis">c6</EM>, <EMCLASS="emphasis">c7</EM>, and<EMCLASS="emphasis">c8</EM>-that make output in 2, 3, 4, 5, 6, 7, or 8 columns.If you call the script with one of those names, it will fill the screenwith that number of columns.</P><PCLASS="para">For example, to list misspelled words in columns:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>spell somefile | cols</B></CODE>word1 word2 word3 word4 word5 word6 word7 word8word9 word10 word11 word12 word13 word14 word15 word16</PRE></BLOCKQUOTE></P><PCLASS="para">By default, like the example above,<EMCLASS="emphasis">cols</EM> and the others order the input words across the screen; that'sthe fastest way.If you need output going down each column, use the <EMCLASS="emphasis">-d</EM> option.Then the scripts will calculate column length and order items down the screen:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>spell somefile | cols -d</B></CODE>word1 word3 word5 word7 word9 word11 word13 word15word2 word4 word6 word8 word10 word12 word14 word16</PRE></BLOCKQUOTE></P><PCLASS="para">The script will read from files you name; otherwise it reads standard input.It figures your screen width from the <EMCLASS="emphasis">COLUMNS</EM> environment variableif it's set; otherwise, it calls<SPANCLASS="link"><EMCLASS="emphasis">tcap</EM> (<ACLASS="linkend"HREF="ch41_10.htm"TITLE="Using terminfo Capabilities in Shell Programs ">41.10</A>)</SPAN>to read your <EMCLASS="emphasis">termcap</EM>.(On <EMCLASS="emphasis">terminfo</EM> systems, use<SPANCLASS="link"><EMCLASS="emphasis">tput</EM> (<ACLASS="linkend"HREF="ch41_10.htm"TITLE="Using terminfo Capabilities in Shell Programs ">41.10</A>)</SPAN>instead of <EMCLASS="emphasis">tcap</EM>.)If you use a windowing system with adjustable-width windows, the scriptcould be hacked to check the output of <EMCLASS="emphasis">stty size</EM> or<SPANCLASS="link"><EMCLASS="emphasis">stty -g</EM> (<ACLASS="linkend"HREF="ch42_04.htm"TITLE="Checklist for Resetting a Messed Up Terminal ">42.4</A>)</SPAN>.</P><PCLASS="para">A few programming details:the number of columns, <EMCLASS="emphasis">nc</EM>, is taken from the script name(<EMCLASS="emphasis">c</EM><BCLASS="emphasis.bold">2</B>, etc.)-or, if you call <EMCLASS="emphasis">cols</EM>,the script uses<SPANCLASS="link"><EMCLASS="emphasis">awk</EM> (<ACLASS="linkend"HREF="ch33_11.htm"TITLE="Quick Reference: awk ">33.11</A>)</SPAN>to find the longest input line and calculate the number of columns.(A<SPANCLASS="link"><EMCLASS="emphasis">case</EM> statement (<ACLASS="linkend"HREF="ch44_05.htm"TITLE="Test String Values with Bourne Shell case ">44.5</A>)</SPAN>tests<SPANCLASS="link"><CODECLASS="literal">$0</CODE> (<ACLASS="linkend"HREF="ch44_22.htm"TITLE="Finding a Program Name; Multiple Program Names ">44.22</A>)</SPAN>to decide.)<SPANCLASS="link"><EMCLASS="emphasis">expr</EM> (<ACLASS="linkend"HREF="ch45_28.htm"TITLE="Quick Reference: expr ">45.28</A>)</SPAN>does other calculations.Without the <EMCLASS="emphasis">-d</EM> flag, the <EMCLASS="emphasis">pr</EM> command line for making thecolumns is simple:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">pr -$nc -t -w$width -l1 $temp</PRE></BLOCKQUOTE></P><PCLASS="para">The <CODECLASS="literal">$temp</CODE> file holds the input text.With <EMCLASS="emphasis">-d</EM>, the command line is more complicated.It uses<SPANCLASS="link"><EMCLASS="emphasis">wc -l</EM> (<ACLASS="linkend"HREF="ch29_06.htm"TITLE="Counting Lines, Words, and Characters: wc ">29.6</A>)</SPAN>to count the number of input lines, then <EMCLASS="emphasis">expr</EM>to divide by the number of columns and add 1:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">pr -$nc -t -w$width -l`expr \( \`wc -l < $temp\` / $nc \) + 1` $temp</PRE></BLOCKQUOTE></P><PCLASS="para"> The<SPANCLASS="link">escaped backquotes (<CODECLASS="literal">\`</CODE>) (<ACLASS="linkend"HREF="ch45_31.htm"TITLE="Nested Command Substitution ">45.31</A>)</SPAN>mean that<CODECLASS="literal">wc -l < $temp</CODE>will run first.The line count from <EMCLASS="emphasis">wc</EM> will be substituted onto the <EMCLASS="emphasis">expr</EM> commandline.The result from <EMCLASS="emphasis">expr</EM> will be glued after the <CODECLASS="literal">-l</CODE> to completethe <EMCLASS="emphasis">pr</EM> page length option.If you don't like condensing the command line that much, you can move the<EMCLASS="emphasis">wc</EM> and <EMCLASS="emphasis">expr</EM> commands to other lines and pass the values withshell variables.</P><PCLASS="para">You can install this script from the CD-ROMor from the<SPANCLASS="link">online archive (<ACLASS="linkend"HREF="ch52_07.htm"TITLE="Other Ways to Get the Software ">52.7</A>)</SPAN>.If you get it from the archive,ask tar to install <EMCLASS="emphasis">cols</EM> and its seven other links:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>tar xvf </B></CODE><CODECLASS="replaceable"><I>archive.tar</I></CODE><CODECLASS="userinput"><B> cols c2 c3 c4 c5 c6 c7 c8</B></CODE>x cols, 2160 bytes, 5 tape blocksc2 linked to colsc3 linked to cols ...</PRE></BLOCKQUOTE></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch35_15.htm"TITLE="35.15 Cutting Columns with colrm "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 35.15 Cutting Columns with colrm "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch35_17.htm"TITLE="35.17 Making Text in Columns with pr "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 35.17 Making Text in Columns with pr "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">35.15 Cutting Columns with colrm </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">35.17 Making Text in Columns with pr </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -