Saturday, August 13, 2011

Linux Commands - uniq,tee and who


This is another sequel to my previous posts regarding the Linux commands. In this post, I want to explain the uniq, tee and who commands.

Uniq

This  command is used to handle the duplicates in the file content. Suppose I have a file a.text.

$ cat a.text 
Condemned
Condemned
Underworld
Underworld
Vantage point
Toy Story
Toy Story 2

Now use uniq command to display the uniq lines only.

$ uniq a.text 
Condemned
Underworld
Vantage point
Toy Story
Toy Story 2

If we want see only the lines that are not duplicated, use -u flag.

$ uniq -u a.text 
Vantage point
Toy Story
Toy Story 2

If we want to avoid the non duplicated lines, that is opposite to the previous one, use -d flag.

$ uniq -d a.text 
Condemned
Underworld

If want to set a counter on each lines that denotes how much times each line is displayed, use the -c flag.

$ uniq -c a.text 
      2 Condemned
      2 Underworld
      1 Vantage point
      1 Toy Story
      1 Toy Story 2

Tee

tee is normally used to split the output of a program so that it can be seen on the display and also be saved in a file. The command can also be used to capture intermediate output before the data is altered by another command or program.

Run the following command to get a directory listing on your terminal, while also redirecting the output to a file named b.text.

$ ls -al | tee b.text

Who

If you want to know which users are currently logged in to your Linux system, which console they're using, and the date and time they logged in, use the who command.

$ who
ajay     tty7         2011-08-12 13:45 (:0)
ajay     pts/0        2011-08-12 13:46 (:0.0)

In the output shown here, the term tty stands for teletype. In the olden days of computing, a terminal was just a keyboard with an attached printer, so you read everything off the teletype.

If you've logged in with multiple virtual consoles and changed your identity on any of them, you may have some trouble figuring out who you are--or at least what user is logged in to the console you're using. If you find yourself in such an identity crisis, try this related command,

$ whoami
ajay

Thanks

AJAY

Linux Commands - cat,tac,head and tail

In this post, I want to show some file displaying commands that are very useful.

Cat

cat command is used to display the contents of a file in the terminal.

$ cat a.text 
Hi friends, I'm Ajay.
Here I'm posting about my MCA main project.

Tac

tac is same as cat, but shows the content of the file in reverse order.

$ tac a.text 
Here I'm posting about my MCA main project.
Hi friends, I'm Ajay.

Head

head is another file content display command, but having some advanced controls.

$ head a.text 

It will display first 10 lines of a file.

$ head -1 a.text 

It will display first line of the file.

Tail

The tail is opposite to the head command. 

$ tail a.text

It shows the last 10 lines of the file. If you want to see the last line of the file, type,

$ tail -1 a.text

So in our example file head and tail works as follows,

$ head -1 a.text 
Hi friends, I'm Ajay.

$ tail -1 a.text 
Here I'm posting about my MCA main project.

We can also save the output to another file using '>'

$ tail -1 a.text > b.text

Thanks

AJAY

Friday, August 12, 2011

Setting background in Terminal


I have showed you hoe to change the default start up of terminal by customizing the .bashrc file. In this post I want to show you how to change the font,background etc of the terminal.
We can set them without the use of Linux commands. Open your terminal and select Edit->Profiles. There you have an option to set the new profile name. After giving a proper name you can edit the profile as you want.

You can change the font under General tab. You can also edit the cursor type. In Title and Command tab, you can edit the default title name 'Terminal' to whatever you like. On the Colors tab you can edit the foreground and background colors.

Now click on the Background tab. Here you can set an image in your system as your terminal background. Also set the transparency level. Select your customized profile by selecting Terminal->Change Profile in the menu bar. Now your terminal have entirely different look!!! Look at my customized terminal.


Thanks

AJAY

Linux Commands - find and sort


In this post, I want to show you some useful Linux  commands that can be experimented on your terminal.

Find

The find command is used to locate files on a Unix or Linux system.  find will search any set of directories you specify for files that match the supplied search criteria. he syntax looks like this:

find where-to-look criteria what-to-do

If you simply enter find in your terminal, it will display the path names of all files in the current directory and all sub directories. You can give the path of specific directory also.

$ find /home/ajay/web.py-0.36/project/templates/

You can also search for specific type of files.

$ find /home/ajay/web.py-0.36/project/static/ -name \*.gif

The above code will show you the gif files in the specified path. Other files will not be displayed. Try the following code now,

$ find / -mmin -10

It displays the files that are modified less than 10 minutes ago. There are also other interesting uses with the find command. Now lets check another Linux command sort.

Sort

It have the syntax,

sort <flags> <sort fields> <file name>

Sort command can be used in various ways and we can illustrate it using an example. I have a file a.text having following values,

$ cat a.text 
Ajay 23 74
Amala 23 82
Jinesh 22 75
Anoop 21 76
Arun 20 70

If you want to sort this file by the first word of the line, type,

$ sort a.text 
Ajay 23 74
Amala 23 82
Anoop 21 76
Arun 20 70
Jinesh 22 75

If you want to sort in reverse order,

$ sort -r a.text 
Jinesh 22 75
Arun 20 70
Anoop 21 76
Amala 23 82
Ajay 23 74

If you want to save the result into a file, then use,

$ sort -r a.text > b.text

Check b.text,

$ cat b.text 
Jinesh 22 75
Arun 20 70
Anoop 21 76
Amala 23 82
Ajay 23 74

If you want to sort the file by the second field, then use,

$ sort +1 -1 a.text 
Arun 20 70
Anoop 21 76
Jinesh 22 75
Ajay 23 74
Amala 23 82

If we replace +1 by +0, we will get the sorted file by the first field.

$ sort +0 -1 a.text 
Ajay 23 74
Amala 23 82
Anoop 21 76
Arun 20 70
Jinesh 22 75

Suppose we have a data like,

Jinesh,22,75
Arun,20,70
Anoop,21,76
Amala,23,82
Ajay,2374

which is separated by commas, then we can specify the delimiter. Because the field separations should be specified.

$ sort +1 -1 b.text

will not give expected result, since it cannot understand the fields. So specify the delimiter. Here given the results without using delimiter and using the delimiter.

$ sort +1 -1 b.text 
Ajay,23,74
Amala,23,82
Anoop,21,76
Arun,20,70
Jinesh,22,75

$ sort -t, +1 -1 b.text 
Arun,20,70
Anoop,21,76
Jinesh,22,75
Ajay,23,74
Amala,23,82

-tx is the format and we need to replace x by the delimiter. Here it is comma. Any delimiter symbols can be used such as colon, semicolon, hyphen etc.

Thanks

AJAY

Friday, August 5, 2011

Change your Terminal look


I have described you about some UNIX tools in my previous posts. Now take a look at a interesting experiment on your terminal.

This experiment is regarding how to run a command automatically every time you open a new terminal. If you bored with the regular terminal, then you can experiment on it.
Open your terminal first. Then type, 

gedit ~/.bashrc

Now the gediter will be opened and a file named .bashrc is opened. It is a hidden file. That is why its name starts with a dot. Now copy the following code into that file.

if [ $(id -u) -eq 1 ];
then # you are root
  PS1='\[\e[1;31m\][\u@\h \W]\$\[\e[0m\] '
else # normal
  ls
  PS1='\[\033[33;22m\]\u @ \w\[\033[04;36m\]\t $ \[\033[01;00m\]♡'
fi

Now close the geditor by saving the file. And also close the terminal. Now open a new terminal. You will see a brand new terminal now!!!

You can give any commands in the place of ls and can change the symbols and colors also.
Examine the code yourself and try to make changes and build a your own terminal look.
Here is the screenshot of my Terminal.


Thanks 

AJAY

Make - An Introduction


In software development, make is a utility that automatically builds executable programs and libraries from source code by reading files called makefiles which specify how to derive the target program. The purpose of the make utility is to determine automatically which pieces of a large program need to be recompiled, and issue the commands to recompile them.

To prepare to use make, you must write a file called the makefile that describes the relationships among files in your program, and the states the commands for updating each file. In a program, typically the executable file is updated from object files, which are in turn made by compiling source files. The make program uses the makefile data base and the last-modification times of the files to decide which of the files need to be updated. For each of those files, it issues the commands recorded in the data base. make executes commands in the makefile to update one or more target names, where name is typically a program. If no -f option is present, make will look for the makefiles GNUmakefile, makefile, and Makefile, in that order. make updates a target if it depends on prerequisite files that have been modified since the target was last modified, or if the target does not exist.  

A makefile consists of dependency lines of text which define a target (a rule) followed by a colon and optionally a set of files on which the target depends. The dependency line is arranged so that the target (left hand of : character) depends on components (right hand of : character).

The commands that follow are usually arranged so that they generate the target. An example: if "file.html" is newer, it is converted to text. The contents of makefile:
    
    file.txt: file.html
            lynx -dump file.html > file.txt

The above is run by invoking make with the target name file.txt:

    make file.txt

Simple Example

This is an example descriptor file to build an executable file called prog1. It requires the source files file1.cc, file2.cc, and file3.cc. An include file, mydefs.h, is required by files file1.cc and file2.cc. If you wanted to compile this file from the command line using C++ the command would be

% CC -o prog1 file1.cc file2.cc file3.cc

This command line is rather long to be entered many times as a program is developed and is prone to typing errors. A descriptor file could run the same command better by using the simple command

% make prog1

or if prog1 is the first target defined in the descriptor file

% make

Let's go through the example to see what make does by executing with the command make prog1 and assuming the program has never been compiled.
  1. make finds the target prog1 and sees that it depends on the object files file1.o file2.o file3.o
  2. make next looks to see if any of the three object files are listed as targets. They are so make looks at each target to see what it depends on. make sees that file1.o depends on the files file1.cc and mydefs.h.
  3. Now make looks to see if either of these files are listed as targets and since they aren't it executes the commands given in file1.o's rule and compiles file1.cc to get the object file.
  4. make looks at the targets file2.o and file3.o and compiles these object files in a similar fashion.
  5. make now has all the object files required to make prog1 and does so by executing the commands in its rule.
You probably noticed we did not use the target, clean, it is called a phony target.

Make is invoked from a command line with the following format

make [-f makefile] [-bBdeiknpqrsSt] [macro name=value] [names]

However from this vast array of possible options only the -f makefile and the names options are used frequently. The table below shows the results of executing make with these options.

Frequently used make options



Command
Result
make
use the default descriptor file, build the first target in the file
make myprog
use the default descriptor file, build the target myprog
make -f mymakefile
 use the file mymakefile as the descriptor file, build the first target in the file
make -f mymakefile myprog
use the file mymakefile as the descriptor file, build the tar get myprog

These are the essential details of make utility in UNIX.


Thanks


AJAY

AWK- An Introduction


AWK is an extremely versatile programming language for working on files. AWK is one of the early tools to appear in Version 7 Unix and gained popularity as a way to add computational features to a Unix pipeline. There are three variations of AWK:

AWK - the original from AT&T
NAWK - A newer, improved version from AT&T
GAWK - The Free Software foundation's version

The AWK utility is a data extraction and reporting tool that uses a data-driven scripting language consisting of a set of actions to be taken against textual data (either in files or data streams) for the purpose of producing formatted reports. The language used by awk extensively uses the string data type, associative arrays (that is, arrays indexed by key strings), and regular expressions.

The essential organization of an AWK program follows the form:

pattern { action }

The pattern specifies when the action is performed. Like most UNIX utilities, AWK is line oriented.

The AWK program below:

BEGIN { print "START" }
      { print         }
END   { print "STOP"  }

adds one line before and one line after the input file. This isn't very useful, but with a simple change, we can make this into a typical AWK program:

BEGIN { print "File\tOwner"," }
{ print $9, "\t", $3}
END { print " - DONE -" }

The below code can be saved to filename.awk

#!/bin/awk -f
BEGIN { print "File\tOwner" }
{ print $9, "\t", $3}
END   { print " - DONE -" }

The characters "\t" Indicates a tab character so the output lines up on even boundaries. The "$9" and "$3" have a meaning similar to a shell script. Instead of the ninth and third argument, they mean the ninth and third field of the input line. Change the permission with the chmod command, (i.e. "chmod +x filename.awk"), and the script becomes a new command.

There are several arithmetic operators, similar to C. These are the binary operators, which operate on two variables:


Binary Operators



Operator
Type
Meaning
+
Arithmetic
Addition
-
Arithmetic
Subtraction
*
Arithmetic
Multiplication
/
Arithmetic
Division    
%
Arithmetic
Modulo
<space>
String
Concatenation

Using variables with the value of "7" and "3," AWK returns the following results for each operator when using the print command:



Expression
Result
7+3
10
7-3   
4
7*3
21
7/3   
2.33333
7%3   
1
7 3
73

The "+" and "-" operators can be used before variables and numbers. If X equals 4, then the statement:

print -x; will print "-4."

AWK also supports the "++" and "--" operators of C. Variables can be assigned new values with the assignment operators. The second type of expression in AWK is the conditional expression.

Arithmetic values can also be converted into Boolean conditions by using relational operators:

Relational Operators



Operator
Meaning
==
Is equal
!=
Is not equal to
> 
 Is greater than
>=
Is greater than or equal to
< 
Is less than
<=
Is less than or equal to

Two operators are used to compare strings to regular expressions:


Regular Expression Operators



Operator
Meaning 
~
Matches
!~
Doesn't match 

There are only a few commands in AWK. The list and syntax follows:

if ( conditional ) statement [ else statement ]
while ( conditional ) statement
for ( expression ; conditional ; expression ) statement
for ( variable in array ) statement
break
continue
{ [ statement ] ...}
variable=expression
print [ expression-list ] [ > expression ]
printf format [ , expression-list ] [ > expression ]
next 
exit

Awk's built-in variables include the field variables: $1, $2, $3, and so on ($0 represents the entire record). They hold the text or values in the individual text-fields in a record.
Other variables include:

NR: Keeps a current count of the number of input records.
NF: Keeps a count of the number of fields in an input record. The last field in the input record can be designated by $NF.
FILENAME: Contains the name of the current input-file.
FS: Contains the "field separator" character used to divide fields on the input record. The default, "white space", includes any space and tab characters. FS can be reassigned to another character to change the field separator.
RS: Stores the current "record separator" character. Since, by default, an input line is the input record, the default record separator character is a "newline".
OFS: Stores the "output field separator", which separates the fields when Awk prints them. The default is a "space" character.
ORS: Stores the "output record separator", which separates the output records when Awk prints them. The default is a "newline" character.
OFMT: Stores the format for numeric output. The default format is "%.6g".

The print command can display the results of calculations and/or function calls:

print 3+2
print foobar(3)
print foobar(variable)
print sin(3-2)

Output may be sent to a file:

print "expression" > "file name"

or through a pipe:

print "expression" | "command"

This post includes only some basics of AWK and if you want to know more about AWK refer Internet.

Thanks

AJAY