# 05-D.7.3: Handling Text Files - cut/paste

## The cut Command

The cut command cuts out the sections from each line of files and writes the result to standard output. It can be used to cut parts of a line by byte position, character and field. Basically the cut command slices a line and extracts the text. Specify an option with the command otherwise it throws an error. If more than one file name is provided then data from each file is not preceded by its file name.

Syntax:

cut [ OPTION ] [FILE]...


Command Options:

Options Option Meaning
-b, --bytes=,LIST/ select only these bytes
-c, --characters=,LIST/ select only these characters
-d, --delimiter=,DELIM/ use DELIM instead of TAB for field delimiter - VALID ONLY FOR FIELDS
-f, --fields=,LIST/ select only these fields; also print any line that contains no delimiter character, unless the -s option is specified
-s, --only-delimited do not print lines not containing delimiters

So cut can be used look at a file by the byte, the character or fields. In the example below we have a file statecap.txt - it is three fields that are separated by a colon.

pbmac@pbmac-server $cat statecap.txt Montana:Helena:MT Arizona:Phoenix:AZ South Dakota:Pierre:SD Missouri:Jefferson City:MO Alaska:Juneau:AK Wyoming:Cheyenne:WY Nevada:Carson City:NV We can cut a range of bytes/characters/fields. The first example we cut only the first 3 bytes of the file. In the second we cut the first eight characters. (The bytes and characters are often the same - but bytes will pick up non-characters). In the third example we specify the colon delimiter and the third field - so we get the state abbreviation. pbmac@pbmac-server$ cut -b 1-3 statecap.txt
Mon
Ari
Sou
Mis
Ala
Wyo
Nev
pbmac@pbmac-server $cut -c 1-8 statecap.txt Montana: Arizona: South Da Missouri Alaska:J Wyoming: Nevada:Cpbmac@pbmac-server$ cut -d':' -f3 statecap.txt
MT
AZ
SD
MO
AK
WY
NV


As with most commands we can combine cut with other command input/output to form more powerful command string. We cat the file, cut the first field, then sort it in alphabetic order.

pbmac@pbmac-server $cat statecap.txt | cut -d':' -f 1 | sort - Alaska Arizona Missouri Montana Nevada South Dakota Wyoming  ## The paste Command The paste command is used to join files horizontally (parallel merging) by outputting lines consisting of lines from each file specified, separated by tab as delimiter, to the standard output. When no file is specified, or a dash (“-“) is inserted instead of file name, paste reads from standard input and gives output as it is until a interrupt command [Ctrl-c] is given. Syntax: paste [ OPTIONS ] [FILES]...  Command Options:  Options Option Meaning -d, --delimiters=, LIST/ reuse characters from LIST instead of TAB -s, --serial paste one file at a time instead of in parallel -z, --zero-terminated line delimiter is NUL, not newline The paste command will take each specified element from each file (in this case the first line of each file) and concatenate them together on a single line. Obviously the files must be in sync with one another otherwise the output would be gibberish. pbmac@pbmac-server$ paste state.txt capital.txt abbrev.txt
Montana    Helena    MT
Arizona    Phoenix    AZ
South Dakota    Pierre    SD
Missouri    Jefferson City    MO