Viewing and Combining Text

Use these commands to read text files, pick out the parts you need, and combine or split them. None of them change the files they read; they write their result to standard output. The man page for each command lists every option.

The examples use sample file names such as names.txt. Create those files first, or substitute your own.

Read a file #

cat writes files to standard output one after another, which also combines them:

cat opening.txt details.txt > report.txt

cat -A shows tabs, line endings, and other invisible characters, and cat -n numbers the lines. For a long file, more shows one screen at a time: press Space for the next screen, / to search, and Q to stop. nl prints a numbered copy that leaves blank lines unnumbered, and tac prints lines from last to first.

Read the start or end #

Show the first two lines of a file:

head -n 2 names.txt

tail -n 2 progress.log shows the last two lines. Without -n, head shows 10 lines. To watch a log as it grows, use tail -f, which follows the file it opened. If a program replaces the log with a new file of the same name, tail -F follows the name instead and keeps going. Press Ctrl+C to stop. With several files, head and tail label each one, and the labels become part of the output.

Inspect raw bytes #

When a terminal would hide or reinterpret characters, od shows each byte as a number:

od -An -tx1 sample.bin

-tx1 shows one byte per hexadecimal value, and -An hides the offset column. Repeated rows are collapsed to * unless you add -v.

Select fields #

cut selects fields from each line, split at a delimiter:

cut -d : -f 1 counts.txt

A line without the delimiter passes through unchanged unless you add -s.

Combine files #

Choose by what links the lines together:

  • By position. paste names.txt counts.txt puts the first line of each file side by side, then the second, and so on. A missing line shifts every later pair.
  • By a shared key. join counts.txt colors.txt combines lines whose first field matches. Both files must be sorted on that field with the same rules, and a repeated key produces more than one output line.
  • By membership. comm compares two sorted files and shows the lines found only in the first, only in the second, or in both.

For join and comm, set the locale to the one you sorted with, for example LC_ALL=C join counts.txt colors.txt.

Split a file #

Split a file into pieces of two lines each:

split -l 2 original.txt piece-

The pieces are named piece-aa, piece-ab, and so on, and joining them in that order with cat restores the original. csplit report.txt '/Section/' splits at a line that matches a pattern instead. Both commands can overwrite existing files with the same names, so run them in an empty directory. New pieces inherit security from their directory.

Commands on this page #

This page covers these commands:

CommandUse it toManual
catWrite files to output in ordercat(1)
moreRead a file one screen at a timemore(1)
nlNumber linesnl(1)
tacPrint lines in reverse ordertac(1)
head, tailRead the start or end of a filehead(1), tail(1)
odShow bytes as numbersod(1)
cutSelect fields from each linecut(1)
pasteCombine lines by positionpaste(1)
joinCombine lines by a shared keyjoin(1)
commCompare two sorted filescomm(1)
split, csplitSplit a file into piecessplit(1), csplit(1)

Edit this page