Working With Text
10/18/2000Once you have your FreeBSD system up and running, what do you spend most of your time doing? Why, working with and creating files, of course. In today's article, I want to concentrate on manipulating text in files; we'll start with some useful commands that came with your FreeBSD system, then we'll examine some of the utilities in the ports collection.
One of the most useful utilities for quick manipulation of text files is the cat utility. One does have to be careful when using cat, especially if you're quick with the Enter key, as it is easy to destroy existing files if you're not careful. By default, cat will display the specified file to your screen. That is,
cat filename
will display the contents of a file named filename.
You can also use cat to display multiple files like so:
cat filename1 filename2 filename3
However, even the fastest speed-reader will miss most of the contents of the cat command if the contents of the file(s) are longer than one screen. To force cat to display its output one screen at a time, pipe its output to the more command like so:
cat file1 file2 | more
Or, save yourself some typing and let the more utility directly display the files for you like so:
more file1 file2
Both commands will get the job done, but if you use cat, it won't tell you when file1 ends and file2 begins; if you just use more, it will. I usually use the more utility to display files, unless I'm absolutely certain that the file is only a few lines long. If the file you wish to view has been gzipped and has a .gz extension, use the utilities zcat or zmore to view the file without having to gunzip it first.
You can also use cat as a quick and dirty editor to create a new file. By default, cat reads a file and sends its results to your terminal; to force it to instead read your input and send it to a file, you need to use the > redirector. Try this in your home directory:
cat > test
Notice that you've lost your prompt as the cat command is waiting for input to send to a file called test. Type what you want to appear in the file, and when you're finished, press Enter, then Ctrl-d.
This is a test file
with a couple of lines of text
and a blank line.
^d
You should now have your cursor back. To view this new file, use cat without the redirector:
cat test
Simple, wasn't it? However, this is where cat can be dangerous. If I already had a file named test in this directory, cat would happily overwrite that file without warning me first. I would be very unhappy if that file happened to be my thesis, or my resume, or any other file I may have been attached to -- something to keep in mind when using cat with the > redirector.
You can also use cat to join multiple files into one file. This command:
cat file1 file2 file3 > bigfile
will create a new file called bigfile out of the contents of file1, file2, and file3 in that order. If there already was a file called bigfile, it will be destroyed (there's that > redirector, again), but you will still have your original three files.
The cat utility also understands the >> redirector, which appends (adds to the end) of a file. If the file does not already exist, cat will create the file for you. So if you type:
cat file4 file5 >> bigfile
bigfile will add the contents of files 4 and 5 without destroying the earlier contents of files 1 to 3, while
cat file4 file5 >> newbigfile
will create a newbigfile that contains the contents of files 4 and 5. You'll notice that the >> redirector is much safer than the > redirector. However, if you're a fast typist, look before you press enter and make sure you really did type >> instead of >.
Remember back in high school when you had to write those 1500 word essays? There may still be times when you need to know how many words or lines are in a file. You don't need a fancy editor to do this for you, and fortunately, your FreeBSD system is much quicker at counting than you are. If I were to leave this article and type:
wc wo*
97 689 3717 working_with_text
the wc or word count command would tell me that this article has 97 lines, 689 words, and 3717 bytes of information. If I wanted to see how this compared to the other articles in my working directory, I could type this:
cd ~/articles
wc *
198 1326 8078 buildx.txt
361 2411 13447 change_prompt
431 2763 16147 cron_intro
245 1556 9146 desktop.txt
263 1768 10426 ethereal.txt
351 2104 12855 howto_rtfm_part1
374 2246 14018 howto_rtfm_part2
363 2642 15523 intro_to
334 1392 7987 loginshe.txt
267 1528 8837 mountfs.txt
321 2319 13712 networking_with_tcpip
305 1524 9235 nfs.txt
268 2101 11761 permissions_part1
367 2235 12373 permissions_part2
386 2063 12430 ppp.txt
257 1478 8979 sharity.txt
310 1738 9777 useful.txt
97 689 3717 working_with_text
5498 33883 198448 total
If I just need to know the number of words, I would type:
wc -w filename
and to just know the number of lines:
wc -l filename
Using wc is a lot quicker than opening up an editor, hunting for my mouse under my piles of scrap paper, and trying to find the correct menu option.
To actually number the lines in a file, you can use a switch with the cat utility. Let's say I'm teaching a friend how to write a simple script; it will be easier on us both if I can refer to a line number when pointing out syntax. This command:
cd ~/perlscripts
cat -n square
1 #!/usr/bin/perl -w
2
3 print shift() **2, "\n";
4
will show that this perlscript has four lines: two lines with text and two blank lines. If I'm only interested in numbering the lines that contain text, I would use this command instead:
cat -b square
1 #!/usr/bin/perl -w
2 print shift() **2, "\n";
I could now demonstrate to my friend the simply beauty of a perlscript; that one line of code (line 2 of the cat -b output) creates a script that will calculate the square of a number like so:
./square 43
1849
But you don't want to get me started on Perl; let's continue working with text files.
Pages: 1, 2 |
