Originally published by Robert Beisert at fortcollinsprogram.robert-beisert.com

Linux + C – Some Useful Filters

Soon we’ll talk about perhaps the most powerful programmable filter ever conceived (the scripting language AWK), but first let’s look at a few other common filters and their uses.

More

The filter “more” is a program that lets us print output one screen at a time. If you’ve ever run a diff between two files or a recursive ls, you might know that the output can be thousands of lines long. Normally, these lines will be presented in one long stream, and you will never see more than the hundred or so lines that the terminal stores in memory by default.

The solution is simple enough. more stores all these lines in memory and prints them out one screen at a time. This way, you can see all the output without piping it to a file and opening it in vi.

The most common usage of more is as follows:

cat file_name | more

Note: cat is a filter that prints a file out to the terminal. However, any program that creates output can be piped to more.

Less

Unfortunately for more, it has no history. That means that you can only ever scroll down, not up.

The solution? less is a program that acts like more, but it allows you to scroll up and down as well. The wikipedia page on less is more useful (in my mind) than the manual page, but basically you can scroll down with the Space Bar (just like more) and scroll back up with b. You can also scroll by one line using j and k (j down, k up). To quit this program (just like with more), you hit the q key.

We usually call less exactly like we call more:

cat file_name | more

Sed

Sed is a stream editor (as opposed to a file editor). What this means is that the tool takes a stream of input, performs some operation on it, and moves on.

Sed is less powerful (and generally less useful) than awk, but I do have one excellent application: removing tabs from the end of a line.

The following operations will extract the tabs and spaces (respectively) from the end of lines in a file. Very useful for someone like me, who tends to accidentally leave a tab or two in some files.

sed -i ‘s/[\t]*s//’ File_Name

sed -i ‘s/[ ]*s//’ File_name

Bonus: Clear

While we’re talking about printing lines on a screen, it’s worth noting that the program “clear” will print a number of blank lines sufficient to clear your terminal window. This makes it look somewhat like you just started the terminal, but with all the history and memory of the terminal still intact.

If you’re done with all the output on your screen, just run clear once.

photo by: