Unix Text Manipulation

This article by Brad Yoes is my personal cheat sheet on UNIX manipulation. I highly suggests you read it if you want a better understanding on how to use these UNIX commands.

Note: This cheat sheet page is still very much a work in progress. I’m not even half way through yet and my documentation is pretty sparse. I’ll add onto when I can and try making it more verbose.

Operators and Miscellaneous Stuff

Standard input  >
Standard output  <
End of file <ctrl-d>


The cat command performs basic file manipulation.

File creation cat > foo.bar
Entering a line of text
Entering another line of text
File appending cat >> foo.barEntering a new line of text


File Displaying cat foo.bar
File Displaying with counted lines cat -n foo.bar


The nl command is useful in reading lines.

Readline nl foo.bar
Readline with regex nl -b p^[E] foo.bar
Customizing readline deliminator nl -s: foo.bar


WC is a basic counting command

Counting lines, words separated by whitespace, and total characters wc foo.bar
Counting only lines wc -l foo.bar
Counting only words separated by whitespace wc -w foo.bar
Counting only characters wc -c foo.bar


Grep is a basic command for searching a file

Searching a single file grep Enter foo.bar
Searching multiply files grep Enter foo.bar text.txt
Counting found matches grep -c Enter foo.bar
Hiding file name output grep -h Enter foo.bar
Displaying own matched file names grep -l Enter foo.bar
Ignoring case search grep -i enter foo.bar
Including line numbers grep -n Enter foo.bar
Inverted searching grep -v Unix foo.bar
Word matching grep -w Enter foo.bar

Streams And Pipes

Stream File Descriptors

stdin 0
stdout 1
stderr 2

Merging and Splitting Streams

make –f build_example 2>&1 | tee build.log

File Redirection
This example will make a copy of the file foo.bar

cat < foo.bar > foo.bar.bak

Process Piping
This example will insert line numbers into the foo.bar file

cat foo.bar| nl

Piping vs Redirection
You can do similar things with one or the other. This examples will have the same output.

grep -i example < foo.bar
cat foo.bar | grep -i example

Here Document
You can do some nifty things with here-doc, ‘<<‘.  The here-doc command will redirect text into a command or file.

cat << EOF

> Hello World!


File Head and Tail

If you’re not first then you’re last. The head command is the beginning of a file and likewise the tail command is the end. The flags -n and -c are important here. To display a given amount of lines you can use -n and to display a given amount of characters in the file you use the -c.

Displaying beginning lines with -n head -n5 foo.bar
Displaying ending lines with -n tail -n5 foo.bar
Displaying beginning characters with -c head -c12 foo.bar
Displaying ending characters with -c tail -c12 foo.bar


The TR command is pretty useful. Normally, tr is used with two sets of characters, and replaces characters from the first set with characters from the second set.

Remove carriage returns tr -d ‘\r’ unixfile.txt

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s