Manipulate Columns: Cut, Paste



next up previous contents index
Next: Compare Files: diff Up: Unix Toolkit Previous: Pattern Search: grep

Manipulate Columns: Cut, Paste

Cutting and pasting columns of text or numbers is very useful to the computational scientist, for example, in preparing data for input to a graphics program or in preparing a manuscript for publication.gif The cut command extracts one or more columns using either a fixed charter count or a field in a file. We start in this example with the file data:
     


3.9737 8.93737 10.8833 9.6363 File data. 4.0567 9.26556 11.3576 9.7935 4.6345 9.73457 10.8833 9.8963
Let's say you want to extract the first and third columns for plotting. Since the lines all have the same number of characters for each column, we could tell cut to ``grep'' the first through the sixth characters in order to capture the first column, and then to grab the sixteenth through the twentieth characters to capture the second column. We grab the seventh character as well in order to get the blank which separates the columns:
$ cut -c1-7,16-22 data   	Cut 2 columns from data.   
3.9737 10.8833   	The output.   
4.0567 11.3576   	   
4.6345 10.8833   	   

Note that the -c option specifies which characters to cut. We could also use the fact that a blank separates the columns to tell cut that it should grab the first and third columns. The -d option tells cut what character separates the fields and the -f option tells cut which fields to grab. The -f and -c options cannot be mixed:

$ cut -d' ' -f1,3 data   	Cut first and third fields.   
3.9737 10.8833   	Same results as above.   
4.0567 11.3576   	   
4.6345 10.8833   	   

This example would also work if the columns were separated by tabs. The only change would be to give the option -t'[tab]'.

Problems may arise with the -d or -f options if the file is not uniformly formatted, for example, if the number of spaces between fields or the number of digits in each number varies. To remove multiple spaces, use the tr command, as shown above, and connect to the cut command via a pipe:

$ tr -s " " < data |cut -d" " -f1,3   	Squeeze out blanks, then cut.   
3.9737 10.8833   	   
4.0567 11.3576   	   
4.6345 10.8833   	   

The paste command combines columns from separate files into a single file. We can start with two data files and combine them so the columns are side by side:


data1 file & data2 file
7.81737 & 10.93737
8.24556 & 11.73567
9.17457 & 12.52343

$ paste data1 data2 > data3   	Combine files into data3.   
$ cat data3   	List data3.   
8.93737 10.93737   	   
8.24556 11.73567   	   
9.17457 12.52343   	   



next up previous contents index
Next: Compare Files: diff Up: Unix Toolkit Previous: Pattern Search: grep