The paste command takes corresponding lines from a set of files and puts them together into lines in its output. The following examples should be enough to give you a general idea. The paste command creates a new file from the three files, filel, file2 and file3, with lines made up of corresponding lines from the three files, with white space between them.

[email protected]:~ > cat file2

[email protected]:~ > cat file3

paste file1 file2 file3

In the next example, by specifying -d: you have forced the delimiter in the output to be the colon, rather than the default spaces.

paste -d: file1 file2 file3

The join command takes two files with lines split into fields, and where a particular field is identical, it takes the other fields from both files and combines them. What follows is a simple example. (There are, of course, options to control which field is regarded as the key.)

[email protected]:~ > cat file1

001 beef

002 beer

003 pies [email protected]:~ > cat file2

001 water

002 wine

003 apples [email protected]:~ > join file1 file2

001 beef water

002 beer wine

003 pies apples awk is something rather bigger than the tools we have been discussing up to now; it is an entire language. awk is an interpreted scripting language; in other words, programs written in awk do not need to be compiled before they are run. We shall present a few simple uses of awk just as a command line here. You will see it used (also usually as a simple single line command) quite often in system shell scripts, and it is certainly useful to know about its existence. But if you want to do the kinds of things that awk does well (selecting and replacing text in text files according to rules that you program), you should consider whether the task could be done more simply and easily by another and more powerful scripting language (such as Python, Perl, or Ruby). On the other hand, awk is a much smaller program and is always available:

[email protected]:~ > cat foods boiled carrots fried potatoes grilled onions grated carrot [email protected]:~ > awk /carrot/ foods boiled carrots grated carrot

Was this article helpful?

0 0

Post a comment