dog is better than cat
System Administrators are always looking for ways to make life easier. The command line is full of nice little features that can save you tons of time. Ever wondered how to remove all blank lines from a file or how to translate all uppercase characters to lowercase. In this article we will be focussing on how to easily parse input or output.
cat
Everyone uses cat, cat stand for catenate or concatenate. www.answers.com says the following about the two words.
- To connect in a series of ties or links; form into a chain.
- Computer Science. To arrange (strings of characters) into a chained list.
So we can link or tie files together with cat, away for doing this would be.
$ cat file1 file2
This is file1
This is file2
This would output the two files to standard output, we could also merge/catenate/link them into a single file.
$ cat file1 file2 > bothfiles
$ cat bothfiles
This is file1
This is file2
Lot’s of people use cat just for this. However there are more nice tricks to cat then just catenate. Lets dive into a couple of handy features. Sometimes text files contain a lot of white-lines after another. In order to reduce al those multiple white-lines into a single line you can use the –squeeze-blank option. First we will create a nice playground. As you might know cat can also read from standard input, for instance.
$ echo -e "Hello world,\n\nWelcome to my blog." | cat
Hello world,
Welcome to my blog.
A bit useless you might think however it’s nice for the examples that will get to shortly. I used the -e option to echo which enables interpretation of the backslash-escaped characters, like \n to print a newline.
Line numbers
In order to print the line numbers we can use –number option to cat.
$ echo -e "Hello world,\n\nWelcome to my blog." | cat -n
1 Hello world,
2
3 Welcome to my blog.
Squeeze
I used the short option to –number which is -n. To get back to our –squeeze-blank subject let’s insert an extra \n to our text.
$ echo -e "Hello world,\n\n\nWelcome to my blog." | cat -n
1 Hello world,
2
3
4 Welcome to my blog.
As you can see line 2 and line 3 both are white-lines. Image we have like 10 or even more consecutive white-lines. Using cat to read the file contents then these white-lines because useless let’s now squeeze them into a singe white-line.
$ echo -e "Hello world,\n\n\nWelcome to my blog." | cat -n -s
1 Hello world,
2
3 Welcome to my blog.
Here i also use the short option -s instead of the long –squeeze-blank. As you can see instead of 4 lines of text we get 3.
Nonblank
The –number option is really nice, it allows you to quickly identify a line with in a file. If your looking at a file with some other people you can easily talk about a certain line. If however you are not interested in all the white-lines you can also use the –number-nonblank option.
$ echo -e "Hello world,\n\n\nWelcome to my blog." | cat -
1 Hello world,
2 Welcome to my blog.
Line endings, TAB endings, etc
Yep -b is the short option to –number-nonblank. Sometimes is really handy to know exactly where a line ends. For instance if you dont want any extra blank space at the end of a line –show-ends to the rescue.
$ echo -e "Hello world,\n\n\nWelcome to my blog." | cat -n -E
1 Hello world,$
2 $
3 $
4 Welcome to my blog.$
Where -E is the short option for –show-ends. The same thing applies to tabs, if you wan to know if a certain piece of text uses tabs you can use the –show-tabs or -T which is the short option.
$ echo -e "Steven\tKroon\n\nWelcome\t\t\t\tTo\nMy Blog." | cat -n
1 Steven Kroon
2
3 Welcome To
4 My Blog.
Now lets look at the tabs.
$ echo -e "Steven\tKroon\n\nWelcome\t\t\t\tTo\nMy Blog." | cat -n -T
1 Steven^IKroon
2
3 Welcome^I^I^I^ITo
4 My Blog.
As you can see the tabs are now replace by ^I, notice that line 4 does not contain a tab, although it looks like it in the output, it just contains 4 spaces instead of a tab. There is also a way to look for non-printable characters this can be done with –show-nonprinting. If you would like to look at all these things at once you can use the –show-all option to enable these flags at once.
$ echo -e "Steven\tKroon\n\nWelcome\t\t\t\tTo\nMy Blog." | cat -n -A
1 Steven^IKroon$
2 $
3 Welcome^I^I^I^ITo$
4 My Blog.$
And -A being the short option. Well thats about it with the handy features for cat, lets get into a more advanced cat called dog.
Pages: 1 2

