The cut Command
The cut command is used to select text from a line, either based on character position or field number.
By default, when selecting by field, the tab character is assumed to be the field delimiter (separator).
- Example: cut -f1 file1
- Display the first field of each line in file1, which would be everything before the first <tab>
- Example: cut -f2,4 file1
- Display the second and fourth fields of each line of file1. This would mean displaying the data between the first and second <tab> and between the third and fourth <tab>
- Example: cut -d: -f1 file1
- Display the first field on each line of file1 using a colon(:) as a separator
- Example: cut -c1-10 file1
- Display the first ten characters on each line of file1
The paste Command
The paste command is used to merge two files. It joins the first lines together, the second lines together, etc with a tab between them.
- Example:paste file1 file2
- The first line of file1 and the first line of file2 are joined. Then then second lines are joined,...
- The -d options can be used to change the default <tab> to any other character.
The sort Command
The sort command can be used to sort the output of a command or the contents of a file.
- Whether it is case sensitive depends on if LC_COLLATE is set.
- If it is set: export LC_COLLATE=C then uppercase comes before lowercase.
- If not, then upper and lower are intermingled.
Like other commands, do not direct output back into the source file.
Options
- -n numeric sort
- -r reverse sort
- -f ignore case (even if LC_COLLATE=C)
- -u delete duplicate lines
- -o followed by output file name. This will put the output into the file listed. If you are using -o you may go back into the same file that you used for input.
The uniq Command
The uniq command removes duplicate adjacent lines. Usually input is sorted first.
Options
- -d Only list lines that were duplicated
- -c Count the frequency of each line
- -u Only list lines that are unique
- The commands “sort -u” and “sort | uniq” give the same output.