📄 ch25_13.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 25] 25.13 pushin: Squeeze Out Extra White Space </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:43:25Z"><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="ch25_01.htm"TITLE="25. Showing What's in a File"><LINKREL="prev"HREF="ch25_12.htm"TITLE="25.12 Double Space, Triple Space ... "><LINKREL="next"HREF="ch25_14.htm"TITLE="25.14 How to Look at the End of a File: tail "></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="ch25_12.htm"TITLE="25.12 Double Space, Triple Space ... "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 25.12 Double Space, Triple Space ... "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 25<BR>Showing What's in a File</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch25_14.htm"TITLE="25.14 How to Look at the End of a File: tail "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 25.14 How to Look at the End of a File: tail "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-4520">25.13 pushin: Squeeze Out Extra White Space </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-27916"></A><ACLASS="indexterm"NAME="AUTOID-27918"></A><ACLASS="indexterm"NAME="AUTOID-27921"></A><ACLASS="indexterm"NAME="AUTOID-27924"></A><ACLASS="indexterm"NAME="AUTOID-27927"></A><ACLASS="indexterm"NAME="AUTOID-27930"></A>If you're viewing or printing a file with lines that are too long to read,you can use a program like <SPANCLASS="link"><EMCLASS="emphasis">fold</EM> (<ACLASS="linkend"HREF="ch43_08.htm"TITLE="Fixing Margins with pr and fold ">43.8</A>)</SPAN>to fold the lines.Or, if there's lots of white space in each line - multiple spaces and/orTABsnext to each other - you can use the script at the end of this article.The <EMCLASS="emphasis">pushin</EM> script replaces series of spaces and/or TABs with asinglespace, "pushing in" each line as much as it can.It reads from files or standard input and writes to standard output.</P><PCLASS="para">Here's an example of lines in a file that aren't too long (we can't printlong lines in this book, anyway) but that do have a lot of white space.Imagine how <EMCLASS="emphasis">pushin</EM> would help with longer lines:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat data</B></CODE>resistor 349-4991-02 23capacitor 385-2981-49 16diode 405-3951-58 8% <CODECLASS="userinput"><B>pushin data</B></CODE>resistor 349-4991-02 23capacitor 385-2981-49 16diode 405-3951-58 8</PRE></BLOCKQUOTE></P><PCLASS="para"> Here's the script:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"> #!/bin/sed -fs/[ ][ ]*/ /g</PRE></BLOCKQUOTE></P><PCLASS="para">Inside each pair of brackets, <CODECLASS="literal">[ ]</CODE>, the <EMCLASS="emphasis">sed</EM> substitute command has a space and a TAB.The replacement string is a single space.</P><PCLASS="para">That file doesn't use a shell; the kernel<SPANCLASS="link">starts <EMCLASS="emphasis">sed</EM> directly (<ACLASS="linkend"HREF="ch45_03.htm"TITLE="Don't Need a Shell for Your Script? Don't Use One ">45.3</A>)</SPAN>and gives it the script itself as the input file expected with the<EMCLASS="emphasis">-f</EM> option.If your UNIX can't execute files directly with <CODECLASS="literal">#!</CODE>, typein this versioninstead:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">exec sed 's/[ ][ ]*/ /g' ${1+"$@"}</PRE></BLOCKQUOTE></P><PCLASS="para">It starts a shell, then<SPANCLASS="link"><EMCLASS="emphasis">exec</EM> replaces the shell with <EMCLASS="emphasis">sed</EM> (<ACLASS="linkend"HREF="ch45_07.htm"TITLE="The exec Command ">45.7</A>)</SPAN>.The <CODECLASS="literal">${1+"$@"}</CODE> works around a<SPANCLASS="link">problem with argument handling (<ACLASS="linkend"HREF="ch46_07.htm"TITLE="Quoting and Command-Line Parameters ">46.7</A>)</SPAN>in some Bourne shells.</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="ch25_12.htm"TITLE="25.12 Double Space, Triple Space ... "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 25.12 Double Space, Triple Space ... "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="ch25_14.htm"TITLE="25.14 How to Look at the End of a File: tail "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 25.14 How to Look at the End of a File: tail "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">25.12 Double Space, Triple Space ... </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">25.14 How to Look at the End of a File: tail </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 + -