home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 43.9 Indenting Text for Printing Chapter 43
Printing
Next: 43.11 Big Letters: banner
 

43.10 Filename Headers Above Files Without pr

The pr command ( 43.7 ) displays your files with a nice header above them. But it can also add a bunch of blank lines to fill a page and break the file to add more headers in the middle if the file is longer than a page. This article shows alternatives to pr that print a single header followed by the entire file, with no extra blank lines or page breaks.

  1. When you redirect the output of more ( 25.3 ) (or pg ) somewhere besides a terminal, it doesn't stop at the end of a screenful. It prints a little header above each file and outputs all the files at once. Instead of redirecting the output to a file, you could pipe it to another program - like your print spooler:

    
    
    cat
     
    
    
    
    
    
    
    
    
    
    % 
    
    more file* > package
    
    
    % 
    
    cat
    
    
    
     package
    
    
    ::::::::::::::
    
    
    file1
    
    
    ::::::::::::::
    
    ...contents of file1....
    
    ::::::::::::::
    
    
    file2
    
    
    ::::::::::::::
    
    ...contents of file2....
    
    ...

    Another way to get similar headers is a feature of head ( 25.20 ) : when you give multiple filenames, it adds a header above each. To be sure head gives you all of your file (not just the head), use a line count bigger than any of your files, with a command like head -10000 .

  2. Bourne shell for loops with redirected output ( 45.22 ) let you combine a bunch of commands and grab the output of all of them at once. Here's a loop that runs ls -l on each file. It uses awk ( 33.11 ) to print just the file's permissions (field 1), last modification date (fields 6-8), and name (field 9, not including any name from a symbolic link). (You could pipe use more awk formatting to make a fancier heading - and get rid of the echo commands, too.) The output is redirected to a file named printme ; as already stated, a pipe to your printer program will also work.

    $ 
    
    for f in file*
    
    
    > 
    
    do
    
    
    > 
    
    echo =====================================
    
    
    > 
    
    ls -l $f | awk '{print $1, $6, $7, $8, $9}'
    
    
    > 
    
    echo =====================================
    
    
    > 
    
    cat $f
    
    
    > 
    
    done > printme
    
    
    $ 
    
    cat printme
    
    
    =============================================
    -rw-r----- Oct 28 07:28 
    
    file1
    
    
    =============================================
    
    ...contents of file1....
    
    =============================================
    -r--r--r-- Nov  3 09:35 
    
    file2
    
    
    =============================================
    
    ...contents of file2....
    
    ...

If you use those last two tricks a lot, you might put them into an alias, function, or shell script ( 10.1 ) .

- JP


Previous: 43.9 Indenting Text for Printing UNIX Power Tools Next: 43.11 Big Letters: banner
43.9 Indenting Text for Printing Book Index 43.11 Big Letters: banner

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System