708 lines
9.1 KiB
HTML
708 lines
9.1 KiB
HTML
<HTML
|
|
><HEAD
|
|
><TITLE
|
|
>Text Viewing Tools </TITLE
|
|
><META
|
|
NAME="GENERATOR"
|
|
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
|
|
REL="HOME"
|
|
TITLE="GNU/Linux Command-Line Tools Summary"
|
|
HREF="book1.htm"><LINK
|
|
REL="UP"
|
|
TITLE="Text Related Tools"
|
|
HREF="c6435.htm"><LINK
|
|
REL="PREVIOUS"
|
|
TITLE="Text Related Tools"
|
|
HREF="c6435.htm"><LINK
|
|
REL="NEXT"
|
|
TITLE="Text Information Tools"
|
|
HREF="x6823.htm"></HEAD
|
|
><BODY
|
|
CLASS="SECT1"
|
|
BGCOLOR="#FFFFFF"
|
|
TEXT="#000000"
|
|
LINK="#0000FF"
|
|
VLINK="#840084"
|
|
ALINK="#0000FF"
|
|
><DIV
|
|
CLASS="NAVHEADER"
|
|
><TABLE
|
|
SUMMARY="Header navigation table"
|
|
WIDTH="100%"
|
|
BORDER="0"
|
|
CELLPADDING="0"
|
|
CELLSPACING="0"
|
|
><TR
|
|
><TH
|
|
COLSPAN="3"
|
|
ALIGN="center"
|
|
>GNU/Linux Command-Line Tools Summary</TH
|
|
></TR
|
|
><TR
|
|
><TD
|
|
WIDTH="10%"
|
|
ALIGN="left"
|
|
VALIGN="bottom"
|
|
><A
|
|
HREF="c6435.htm"
|
|
ACCESSKEY="P"
|
|
>Prev</A
|
|
></TD
|
|
><TD
|
|
WIDTH="80%"
|
|
ALIGN="center"
|
|
VALIGN="bottom"
|
|
>Chapter 11. Text Related Tools</TD
|
|
><TD
|
|
WIDTH="10%"
|
|
ALIGN="right"
|
|
VALIGN="bottom"
|
|
><A
|
|
HREF="x6823.htm"
|
|
ACCESSKEY="N"
|
|
>Next</A
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
><HR
|
|
ALIGN="LEFT"
|
|
WIDTH="100%"></DIV
|
|
><DIV
|
|
CLASS="SECT1"
|
|
><H1
|
|
CLASS="SECT1"
|
|
><A
|
|
NAME="TEXT-VIEWING-TOOLS"
|
|
></A
|
|
>Text Viewing Tools</H1
|
|
><P
|
|
></P
|
|
><DIV
|
|
CLASS="VARIABLELIST"
|
|
><DL
|
|
><DT
|
|
>head</DT
|
|
><DD
|
|
><P
|
|
><A
|
|
NAME="AEN6553"
|
|
></A
|
|
>With no options it shows <A
|
|
NAME="AEN6555"
|
|
></A
|
|
>the first ten lines of a text file. </P
|
|
><P
|
|
>Use<SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
> head -n <A
|
|
NAME="AEN6559"
|
|
></A
|
|
>x </I
|
|
></SPAN
|
|
>(where “x” is a number) to display <A
|
|
NAME="AEN6561"
|
|
></A
|
|
>the first x<SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
> </I
|
|
></SPAN
|
|
>lines. </P
|
|
><P
|
|
>Try <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
> head -F <A
|
|
NAME="AEN6566"
|
|
></A
|
|
></I
|
|
></SPAN
|
|
>to use a continually updated <A
|
|
NAME="AEN6568"
|
|
></A
|
|
>version <A
|
|
NAME="AEN6570"
|
|
></A
|
|
>of <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>head</I
|
|
></SPAN
|
|
> (if the file changes it will be reloaded <A
|
|
NAME="AEN6573"
|
|
></A
|
|
>and displayed), please note that using this option <A
|
|
NAME="AEN6575"
|
|
></A
|
|
>will run <A
|
|
NAME="AEN6577"
|
|
></A
|
|
><SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>head</I
|
|
></SPAN
|
|
> is a continuous <A
|
|
NAME="AEN6580"
|
|
></A
|
|
>loop so you'll need to use
|
|
<B
|
|
CLASS="KEYCAP"
|
|
>CTRL</B
|
|
>-<B
|
|
CLASS="KEYCAP"
|
|
>C</B
|
|
>
|
|
<A
|
|
NAME="AEN6585"
|
|
></A
|
|
>
|
|
to exit.<A
|
|
NAME="AEN6587"
|
|
></A
|
|
></P
|
|
><P
|
|
>For example:</P
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>head -n 20 somelog.txt </PRE
|
|
><P
|
|
>Will display the top 20 entries <A
|
|
NAME="AEN6592"
|
|
></A
|
|
>of the file “somelog.txt”.</P
|
|
></DD
|
|
><DT
|
|
>tail</DT
|
|
><DD
|
|
><P
|
|
><A
|
|
NAME="AEN6598"
|
|
></A
|
|
>With no options it shows <A
|
|
NAME="AEN6600"
|
|
></A
|
|
>the last <A
|
|
NAME="AEN6602"
|
|
></A
|
|
>ten lines of a file. </P
|
|
><P
|
|
>Use<SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
> tail -n <A
|
|
NAME="AEN6606"
|
|
></A
|
|
>x </I
|
|
></SPAN
|
|
>(where “x” is a number) to display the last <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>x </I
|
|
></SPAN
|
|
>lines. </P
|
|
><P
|
|
>Try<SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
> tail -F <A
|
|
NAME="AEN6611"
|
|
></A
|
|
></I
|
|
></SPAN
|
|
>to use a continually updated <A
|
|
NAME="AEN6613"
|
|
></A
|
|
>version <A
|
|
NAME="AEN6615"
|
|
></A
|
|
>of <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>tail</I
|
|
></SPAN
|
|
> (if the file changes it will be reloaded <A
|
|
NAME="AEN6618"
|
|
></A
|
|
>and displayed), please note that using this option will run <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>tail</I
|
|
></SPAN
|
|
> is a continuous <A
|
|
NAME="AEN6621"
|
|
></A
|
|
>loop so you'll need to use
|
|
<B
|
|
CLASS="KEYCAP"
|
|
>CTRL</B
|
|
>-<B
|
|
CLASS="KEYCAP"
|
|
>C</B
|
|
>
|
|
<A
|
|
NAME="AEN6626"
|
|
></A
|
|
>
|
|
to exit.<A
|
|
NAME="AEN6628"
|
|
></A
|
|
></P
|
|
><P
|
|
>For example:</P
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>tail -n 20 somelog.txt</PRE
|
|
><P
|
|
>Will display <A
|
|
NAME="AEN6633"
|
|
></A
|
|
>the last 20 entries <A
|
|
NAME="AEN6635"
|
|
></A
|
|
>of the file “somelog.txt”.</P
|
|
></DD
|
|
><DT
|
|
>less</DT
|
|
><DD
|
|
><P
|
|
><A
|
|
NAME="AEN6641"
|
|
></A
|
|
>Views text, can scroll <A
|
|
NAME="AEN6643"
|
|
></A
|
|
>backwards and forwards.<A
|
|
NAME="AEN6645"
|
|
></A
|
|
> Has many different options which are all described <A
|
|
NAME="AEN6647"
|
|
></A
|
|
>in the manual <A
|
|
NAME="AEN6649"
|
|
></A
|
|
>page.<A
|
|
NAME="AEN6651"
|
|
></A
|
|
> </P
|
|
><P
|
|
>When <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>less</I
|
|
></SPAN
|
|
> is already running,<A
|
|
NAME="AEN6655"
|
|
></A
|
|
> use :n<A
|
|
NAME="AEN6657"
|
|
></A
|
|
> and :p<A
|
|
NAME="AEN6659"
|
|
></A
|
|
> (type a colon <A
|
|
NAME="AEN6661"
|
|
></A
|
|
>then the character) to move <A
|
|
NAME="AEN6663"
|
|
></A
|
|
>to the next <A
|
|
NAME="AEN6665"
|
|
></A
|
|
>and previous <A
|
|
NAME="AEN6667"
|
|
></A
|
|
>files (when there are multiple <A
|
|
NAME="AEN6669"
|
|
></A
|
|
>open <A
|
|
NAME="AEN6671"
|
|
></A
|
|
>files).</P
|
|
><P
|
|
>Command syntax:</P
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>less filename.txt</PRE
|
|
><P
|
|
>Or using a tool (in this example <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>cat</I
|
|
></SPAN
|
|
>):</P
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>cat file.txt | less</PRE
|
|
></DD
|
|
><DT
|
|
>more</DT
|
|
><DD
|
|
><P
|
|
>Displays text, one page full at a time,<A
|
|
NAME="AEN6682"
|
|
></A
|
|
> more limited than <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>less</I
|
|
></SPAN
|
|
>. In this case <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>less</I
|
|
></SPAN
|
|
> is better than <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>more</I
|
|
></SPAN
|
|
>.</P
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>more filename.txt</PRE
|
|
><P
|
|
>Or using a tool (is this example cat):</P
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>cat file.txt | more</PRE
|
|
></DD
|
|
><DT
|
|
>cat<A
|
|
NAME="CAT"
|
|
></A
|
|
></DT
|
|
><DD
|
|
><P
|
|
>Combines <A
|
|
NAME="AEN6695"
|
|
></A
|
|
>(concatenates) multiple <A
|
|
NAME="AEN6697"
|
|
></A
|
|
>documents <A
|
|
NAME="AEN6699"
|
|
></A
|
|
>into one document.<A
|
|
NAME="AEN6701"
|
|
></A
|
|
> Can be used on individual <A
|
|
NAME="AEN6703"
|
|
></A
|
|
>files as well.</P
|
|
><P
|
|
>Some useful options:<A
|
|
NAME="AEN6706"
|
|
></A
|
|
></P
|
|
><P
|
|
></P
|
|
><UL
|
|
><LI
|
|
><P
|
|
><SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>-b</I
|
|
></SPAN
|
|
><A
|
|
NAME="AEN6712"
|
|
></A
|
|
> --- number <A
|
|
NAME="AEN6714"
|
|
></A
|
|
>all non-blank <A
|
|
NAME="AEN6716"
|
|
></A
|
|
>lines</P
|
|
></LI
|
|
><LI
|
|
><P
|
|
><SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>-n<A
|
|
NAME="AEN6721"
|
|
></A
|
|
></I
|
|
></SPAN
|
|
> --- number all lines. </P
|
|
></LI
|
|
></UL
|
|
><P
|
|
>Also try using <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>nl</I
|
|
></SPAN
|
|
> to number lines (it can do more complex <A
|
|
NAME="AEN6725"
|
|
></A
|
|
>numbering), you will find it under under this section,<A
|
|
NAME="AEN6727"
|
|
></A
|
|
> <A
|
|
HREF="x6993.htm"
|
|
>the Section called <I
|
|
>Text manipulation tools</I
|
|
></A
|
|
></P
|
|
><P
|
|
>Example:</P
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>cat filepart1 filepart2 filepart3 > wholefile.txt</PRE
|
|
><P
|
|
>This will combine <A
|
|
NAME="AEN6733"
|
|
></A
|
|
>(concatenate) filepart1,<A
|
|
NAME="AEN6735"
|
|
></A
|
|
> filepart2 <A
|
|
NAME="AEN6737"
|
|
></A
|
|
>and filepart3 <A
|
|
NAME="AEN6739"
|
|
></A
|
|
>into the single file “wholefile.txt”.</P
|
|
></DD
|
|
><DT
|
|
>tac</DT
|
|
><DD
|
|
><P
|
|
><A
|
|
NAME="AEN6745"
|
|
></A
|
|
>Combines (concatenates) multiple <A
|
|
NAME="AEN6747"
|
|
></A
|
|
>documents <A
|
|
NAME="AEN6749"
|
|
></A
|
|
>into one document <A
|
|
NAME="AEN6751"
|
|
></A
|
|
>and outputs<A
|
|
NAME="AEN6753"
|
|
></A
|
|
> them in reverse order.<A
|
|
NAME="AEN6755"
|
|
></A
|
|
> Can also be used on individual <A
|
|
NAME="AEN6757"
|
|
></A
|
|
>files. Notice that <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>tac</I
|
|
></SPAN
|
|
> is <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>cat</I
|
|
></SPAN
|
|
> written backwards. </P
|
|
><P
|
|
>Example:</P
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>tac filepart1 filepart2 filepart3 > wholefile.txt</PRE
|
|
><P
|
|
>This will combine <A
|
|
NAME="AEN6764"
|
|
></A
|
|
>(concatenate) filepart1,<A
|
|
NAME="AEN6766"
|
|
></A
|
|
> filepart2 <A
|
|
NAME="AEN6768"
|
|
></A
|
|
>and filepart3 <A
|
|
NAME="AEN6770"
|
|
></A
|
|
>into the single file but have each of the files written in reverse.<A
|
|
NAME="AEN6772"
|
|
></A
|
|
></P
|
|
></DD
|
|
><DT
|
|
>z* commands</DT
|
|
><DD
|
|
><P
|
|
><A
|
|
NAME="AEN6778"
|
|
></A
|
|
>Many commands can be prefixed <A
|
|
NAME="AEN6780"
|
|
></A
|
|
>with a “z” to read/work within a gzip <A
|
|
NAME="AEN6782"
|
|
></A
|
|
>compressed<A
|
|
NAME="AEN6784"
|
|
></A
|
|
> file. </P
|
|
><P
|
|
>Some examples <A
|
|
NAME="AEN6787"
|
|
></A
|
|
>are <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>zcat, zless,<A
|
|
NAME="AEN6790"
|
|
></A
|
|
> zmore,<A
|
|
NAME="AEN6792"
|
|
></A
|
|
> zgrep,<A
|
|
NAME="AEN6794"
|
|
></A
|
|
> zcmp,<A
|
|
NAME="AEN6796"
|
|
></A
|
|
> zdiff<A
|
|
NAME="AEN6798"
|
|
></A
|
|
></I
|
|
></SPAN
|
|
>. </P
|
|
><P
|
|
>There are many utilities <A
|
|
NAME="AEN6801"
|
|
></A
|
|
>for working with text within compressed files without trying to manually de-compress them somewhere first...most begin with a “z”. You will find some of them mentioned over here, <A
|
|
HREF="x10181.htm"
|
|
>the Section called <I
|
|
>Compression</I
|
|
> in Chapter 15</A
|
|
>.</P
|
|
></DD
|
|
><DT
|
|
>bz* commands</DT
|
|
><DD
|
|
><P
|
|
><A
|
|
NAME="AEN6808"
|
|
></A
|
|
>There are also a few commands that prefixed <A
|
|
NAME="AEN6810"
|
|
></A
|
|
>with a “bz” to read/work within a file compressed <A
|
|
NAME="AEN6812"
|
|
></A
|
|
>with bzip2.<A
|
|
NAME="AEN6814"
|
|
></A
|
|
> </P
|
|
><P
|
|
>The tools are <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>bzcat, bzless,<A
|
|
NAME="AEN6818"
|
|
></A
|
|
> bzgrep<A
|
|
NAME="AEN6820"
|
|
></A
|
|
>.</I
|
|
></SPAN
|
|
> You will find some of them mentioned over here, <A
|
|
HREF="x10181.htm"
|
|
>the Section called <I
|
|
>Compression</I
|
|
> in Chapter 15</A
|
|
>.</P
|
|
></DD
|
|
></DL
|
|
></DIV
|
|
></DIV
|
|
><DIV
|
|
CLASS="NAVFOOTER"
|
|
><HR
|
|
ALIGN="LEFT"
|
|
WIDTH="100%"><TABLE
|
|
SUMMARY="Footer navigation table"
|
|
WIDTH="100%"
|
|
BORDER="0"
|
|
CELLPADDING="0"
|
|
CELLSPACING="0"
|
|
><TR
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="left"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="c6435.htm"
|
|
ACCESSKEY="P"
|
|
>Prev</A
|
|
></TD
|
|
><TD
|
|
WIDTH="34%"
|
|
ALIGN="center"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="book1.htm"
|
|
ACCESSKEY="H"
|
|
>Home</A
|
|
></TD
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="right"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="x6823.htm"
|
|
ACCESSKEY="N"
|
|
>Next</A
|
|
></TD
|
|
></TR
|
|
><TR
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="left"
|
|
VALIGN="top"
|
|
>Text Related Tools</TD
|
|
><TD
|
|
WIDTH="34%"
|
|
ALIGN="center"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="c6435.htm"
|
|
ACCESSKEY="U"
|
|
>Up</A
|
|
></TD
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="right"
|
|
VALIGN="top"
|
|
>Text Information Tools</TD
|
|
></TR
|
|
></TABLE
|
|
></DIV
|
|
></BODY
|
|
></HTML
|
|
> |