Saturday, February 28, 2009

cat command examples

Concatenate Files and Number the Lines

cat -n file1 file2
When working with poems and source code, it's really nice to have numbered lines so that references are clear. If you want to generate line numbers when you use cat, add the -n option (or --number).

$ cat -n housman_-_rue.txt quarles_-_the_world.txt
1 WITH rue my heart is laden
2 For golden friends I had,
3 For many a rose-lipt maiden
4 And many a lightfoot lad.
5 By brooks too broad for leaping
6 The lightfoot boys are laid;
7 The rose-lipt girls are sleeping
8 In fields where roses fade.
9 The world's an Inn; and I her guest.
10 I eat; I drink; I take my rest.
11 My hostess, nature, does deny me
12 Nothing, wherewith she can supply me;
13 Where, having stayed a while, I pay
14 Her lavish bills, and go my way.

Line numbers can be incredibly useful, and cat provides a quick and dirty way to add them to a file.

Note

For a vastly better cat, check out dog (more information is available at http://opensource.weblogsinc.com/2005/02/17/why-dogs-are-betters-than-cats). Instead of local files, you can use dog to view the HTML source of web pages on stdout, or just a list of images or links on the specified web pages. The dog command converts all characters to lowercase or vice versa; converts line endings to Mac OS, DOS, or Unix; and even allows you to specify a range of characters to output (lines 525, for instance). Not to mention, the man page for dog is one of the funniest ever. This is one dog that knows a lot of new tricks!

source scott granemann

No comments:

Post a Comment