Posts

Showing posts with the label Bash

Learning BASH: Text Processing - HEAD & TAIL

Image
BASH continues to surprise me with it's amazing collection of simple, yet extremely useful commands. They can give you a huge boost in speed and control while working. No doubt bash along with editors like VIM are the developer's favorite combinations. Gradually , you will feel the invention of mouse as a waste since you can pretty much control everything with just your keyboard.

Today we continue with more commands that are related to Text Processing.

HEAD & TAIL command
These commands are used to get contents of a file starting from the top and bottom. Unlike the CAT command that displays the whole content of a file, these command gives you control over how much you want to see.

syntax: HEAD filename | TAIL filename
Note: By default, HEAD | TAIL shows 1st/last 10 lines of a file.

Lets say I have a text file like this.
$ cat numbers.txt 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Let's run head command without any arguments other than the filename.

$ head number…

Learning BASH: Text Processing - Cut Command

Text processing tools in Bash is a huge topic . So we will take it one command at a time.

CUT COMMAND
You might think , CUT means to remove a file from location A to location B. But as the link here says, Cut command in unix (or linux) is used to select sections of text from each line of files. You can use the cut command to select fields or columns from a line by specifying a delimiter or you can select a portion of text by specifying the range or characters. Basically the cut command slices a line and extracts the text.

The definition of CUT command in linux itself says:



Print selected parts of lines from each FILE to standard output.
I created a text file (I am on windows running Cygwin...so......) . Added a few lines.

This is the first line
This is the second
And this is not the last line
Finally we end
Good Bye

The linux help says:


 N N'th byte, character or field, counted from 1
N-from N'th byte, character or field, to end of line
N-Mfrom N'th to M'th (included) byte, charac…