Let's look at how we can use the
join operations to edit and format text files.
Much like the "cutting" most of you are familiar with, the
cut command in UNIX takes the section from a file and outputs it to standard out. However, it does not delete any part of the file it extracts text from.
cut is powerful in that it may accept multiple files for its standard input.
With the options listed, there are several ways you can specify a
-foption. Use a specified delimiter rather than default tab.
You may only use one of the
-f options. Each of these options come with a list that is made up of an integer, range of integers, or multiple integer ranges separated by a comma. A list is defined as follows:
$ cat test.txt doh re me fa so 1 2 3 4 5 $ cut -f 3-5 test.txt me fa so 3 4 5 $ cut -f 1,3-4 test.txt doh me fa 1 3 4
paste command is used to merge lines of files together. With this command, you can add one or more columns (or fields) of text to a file. There are two options you should be aware of:
$ cat names.txt Billy Bob Chase Jon Jonathan $ cat birthdates.txt 09/21/1992 08/12/1982 05/24/1999 04/23/1974 08/09/2001 $ paste -d ',' names.txt birthdates.txt Billy,09/21/1992 Bob,08/12/1982 Chase,05/24/1999 Jon,04/23/1974 Jonathan,08/09/2001
If there are an unequal number of fields (5 rows in names.txt and only 3 in birthdates.txt), the bottom two names will not be matched with anything.
$ paste -d ',' 5names.txt 3birthdates.txt Billy,05/24/1999 Bob,04/23/1974 Chase,08/09/2001 Jon, Jonathan,
If you're familiar with a relational databases (and don't worry if you're not), the
join command should sound very familiar. In short,
join takes a common column between two tables, and joins them together based on that attribute.
Let's try a
join operation as an example.
$ cat birthdates.txt 05/24/1999,4 04/23/1974,2 08/09/2001,5 11/24/1991,3 01/23/1975,1 $ cat names.txt Billy,1 Bob,2 Chase,3 Jon,4 Jonathan,5
First, we must have both lists sorted according to the column we want to join on. names.txt is already sorted, but birthdates.txt is not. Refer to the sorting page to learn how the
sort operation works.
$ sort -t ',' -k 2 birthdates.txt > sortedBirthdates.txt $ join -t ',' -1 2 -2 2 names.txt sortedBirthdates.txt 1,Billy,01/23/1975 2,Bob,04/23/1974 3,Chase,11/24/1991 4,Jon,05/24/1999 5,Jonathan,08/09/2001
In some cases, you'll want to join two tables even though there are some rows without a corresponding value in the other row. Joining the right table with missing corresponding values is called a right outer join, and joining the left table with missing corresponding rows is called a left outer join.
A left outer join would have the option
-a1 and a right outer join would have option
In a full outer join, both table rows are included, even if they don't have a corresponding row. Expect many null cell values when using this option.
-a option for a full outer join.
Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.$$ Check price
Linux for Beginners doesn't make any assumptions about your background or knowledge of Linux. You need no prior knowledge to benefit from this book. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.$ Check price