Stupid "cat" tricks that work.
by Mike Chirico
 
The "cat" command is very powerful and fast.  These may be some tips
you haven't seen before, such as "summing" each row of numbers, converting
a random number of columns to a specific number N,  or getting
the contents of multiple files from one list.
 
These tips are simple,  fast and worth a quick look.
 
 
TIP 43:
    
     "cat" the Contents of Files Listed in a File, in That Order.

       SETUP (Assume you have the following)

              $ cat file_of_files
          file1
          file2

                  $ cat file1
          This is the data in file1

            $ cat file 2
          This is the data in file2

       So there are 3 files here "file_of_files" which contains the name of
       other files.  In this case "file1" and "file2". And the contents of
       "file1" and "file2" is shown above.

               $ cat file_of_files|xargs cat
    This is the data in  file1
    This is the data in  file2



TIP 44:

     Columns and Rows -- getting anything you want.

     Assume you have the following file.

        $ cat data
           1  2    3       
   4 5         
   6 7 8 9 10  
   11 12       
   13 14       

     How to you get everything in  2 columns?

        $ cat data|tr ' ' '\n'|xargs -l2
   1 2 
   3 4 
   5 6 
   7 8 
   9 10
   11 12
   13 14

    Three columns?

        $ cat data|tr ' ' '\n'|xargs -l3
   1 2 3  
   4 5 6  
   7 8 9  
   10 11 12
   13 14  

    What's the row sum of the "three columns?"

        $ cat data|tr ' ' '\n'|xargs -l3|tr ' ' '+'|bc      
   6
   15
   24
   33
   27
 
  Note, thanks to, Steven Heiner (http://www.shelldorado.com/).  The above can be
   shortened.
 
       $ tr ' ' '\n' < data|xargs -l3|tr ' ' '+'|bc
 
 
TIP 62:


     If you have multiple blank lines, squeeze these lines down to one,
     then, try the following:

          $ cat -s <file>

     Want to number the lines?

          $ cat -n <file>

     Want to see all the ctl characters?
 
     See the following program:
               http://souptonuts.sourceforge.net/code/ctlgen.c.html
      Which is used to generate "mout" used below


 $ cat -v mout|tail
         test M-v  
test M-w  
test M-x  
test M-y  
test M-z  
test M-{  
test M-|  
test M-}  
test M-~  
test M-^? 
 
NOTE!  The Useless Use of Cat Award   NOTE THE FOLLOWING:
 
 

In nearly all cases  
 
       $  cat file | some_command and its args ...

can be rewritten as:

    $  <file some_command and its args ...

So
 
        $ cat data|tr ' ' '\n'|xargs -l2
Can be rewritten as:
 
        $ tr ' ' '\n' < data |xargs -l2
 
 


Other Tutorials

Linux System Admin Tips: There are over 200 Linux tips and tricks in this article. That is over 150 pages covering topics from setting and keeping the correct time on your computer, permanently deleting documents with shred, making files "immutable" so that root cannot change or delete, setting up more than one IP address on a single NIC, monitering users and processes, setting log rotate to monthly with 12 months of backups in compressed format, creating passwords for Apache using the htpasswd command, common Perl commands, using cfengine, adding users to groups, finding out which commands are aliased, query program text segment size and data segment size, trusted X11 forwarding, getting information on the hard drive including the current temperature, using Gnuplot, POVRAY and making animated GIFs, monitoring selective traffic with tcpdump and netstat, multiple examples using the find command, getting the most from Bash, plus a lot more. You can also down this article as a text document here for easy grepping.

Linux Quota Tutorial: This tutorial walks you through implementing disk quotas for both users and groups on Linux, using a virtual filesystem, which is a filesystem created from a disk file. Since quotas work on a per-filesystem basis, this is a way to implement quotas on a sub-section, or even multiple subsections of your drive, without reformatting. This tutorial also covers quotactl, or quota's C interface, by way of an example program that can store disk usage in a SQLite database for monitoring data usage over time.

Breaking Firewalls with OpenSSH and PuTTY: If the system administrator deliberately filters out all traffic except port 22 (ssh), to a single server, it is very likely that you can still gain access other computers behind the firewall. This article shows how remote Linux and Windows users can gain access to firewalled samba, mail, and http servers. In essence, it shows how openSSH and Putty can be used as a VPN solution for your home or workplace.

Create a Live Linux CD - BusyBox and OpenSSH Included : These steps will show you how to create a functioning Linux system, with the latest 2.6 kernel compiled from source, and how to integrate the BusyBox utilities including the installation of DHCP. Plus, how to compile in the OpenSSH package on this CD based system. On system boot-up a filesystem will be created and the contents from the CD will be uncompressed and completely loaded into RAM -- the CD could be removed at this point for boot-up on a second computer. The remaining functioning system will have full ssh capabilities. You can take over any PC assuming, of course, you have configured the kernel with the appropriate drivers and the PC can boot from a CD. This tutorial steps you through the whole processes.

SQLite Tutorial : This article explores the power and simplicity of sqlite3, first by starting with common commands and triggers, then the attach statement with the union operation is introduced in a way that allows multiple tables, in separate databases, to be combined as one virtual table, without the overhead of copying or moving data. Next, the simple sign function and the amazingly powerful trick of using this function in SQL select statements to solve complex queries with a single pass through the data is demonstrated, after making a brief mathematical case for how the sign function defines the absolute value and IF conditions.

The Lemon Parser Tutorial: This article explains how to build grammars and programs using the lemon parser, which is faster than yacc. And, unlike yacc, it is thread safe.

How to Compile the 2.6 kernel for Red Hat 9 and 8.0 and get Fedora Updates: This is a step by step tutorial on how to compile the 2.6 kernel from source.

Virtual Filesystem: Building A Linux Filesystem From An Ordinary File. You can take a disk file, format it as ext2, ext3, or reiser filesystem and then mount it, just like a physical drive. Yes, it then possible to read and write files to this newly mounted device. You can also copy the complete filesystem, sinc\ e it is just a file, to another computer. If security is an issue, read on. This article will show you how to encrypt the filesystem, and mount it with ACL (Access Control Lists), which give you rights beyond the traditional read (r) write (w) and execute (x) for the 3 user groups file, owner and other.

Working With Time: What? There are 61 seconds in a minute? We can go back in time? We still tell time by the sun?





Chirico img Mike Chirico, a father of triplets (all girls) lives outside of Philadelphia, PA, USA. He has worked with Linux since 1996, has a Masters in Computer Science and Mathematics from Villanova University, and has worked in computer-related jobs from Wall Street to the University of Pennsylvania. His hero is Paul Erdos, a brilliant number theorist who was known for his open collaboration with others.


Mike's notes page is souptonuts. For open source consulting needs, please send an email to mchirico@comcast.net. All consulting work must include a donation to SourceForge.net.

SourceForge.net Logo

SourceForge.net Logo