Monday, December 6, 2010

Learning UNIX - II

Comparison and Search Management
________________________________________

xyx:/>diff command. Compares the two files and print out the differences between them

Let two ascii text files file1 and file2
Contents of file1:
This is a test file to compare the file contents
This is a test file to compare the file contents
This is a test file to compare the file contents
This is a test file to compare the file contents

Contents of file2 contains
This is a second test file to compare the file contents
This is a test file to compare the file contents
This is a test file to compare the file contents
This is a test file to compare the file contents
xyx:/>diff file1 file2
0a1
> This is a second test file to compare the file contents
3d3
This is a test file to compare the file contents
________________________________________

xyx:/>cmp command. Compares the two files, similarly as diff.
xyx:/>cmp file1 file2
file1 file2 differ: char 12, line 1
________________________________________

xyx:/>dircmp Command compares two directories. Let us say dir1 and dir2 and each has 5-10 files in it. Then
xyx:/> dircmp dir1 dir2
Nov 20 14:14 2010 dir1 only and dir2 only Page 1
./userfile/duplicate_Tue.lo ./add_test_profiles.sh
./ userfile/duplicate2 _Tue_09 ./afiedt.buf
./ userfile/duplicate3 _Fri.log ./alter_user_profiles.sh
./ userfile/duplicate4 _Fri_113 ./kb.sh

________________________________________

xyx:/>grep Command is the most useful search command. Mostly used with combination with other commands like PS command, to find processes running on system, a pattern in a file, search one or more files to match an expression etc.

xyx:/> ps -ef | grep sleep will display all the sleep processes running in the system as follows.
root 15258 4190 0 14:21:25 ? 0:00 sleep 300
root 15890 15879 0 14:24:59 ? 0:00 sleep 360
_______________________________________

xyx:/>find command very useful for search of any file anywhere , provided that file and directory you are searching has read write permission set to you ,your group or all.
Some common Examples:

xyx:/>find $crk -print will lists all files in crk home directory.
xyx:/>find /crkdir -name client1 -print will list all files named client1 in /crkDir directory.
xyx:/>find / -type d -name 'man*' -print will list all manpage directories.
xyx:/>find / -size 0 -ok rm {} \; will remove all empty files on system.

_______________________________________

Text processing tools
________________________________________

xyx:/>cut command selects a list of columns or fields from one or more files and show on screen

-c is for character from first columns and -f for fields.
Example:
xyx:/>cut -c1,4 file1
ts
ts
ts
It is printing character of columns 1 and 4 of this file which contains t and s (part of this).

-c list cut the column positions identified in list.
-f list cut the fields identified in list.
-s used with -f to suppress lines without delimiters.
________________________________________

xyx:/>paste command merge the lines of one or more files into vertical columns separated by a tab.
xyx:/>paste file1 file2
This is a test file to compare the file contents This is a second test file to compare the file contents
This is a test file to compare the file contents This is a test file to compare the file contents
This is a test file to compare the file contents This is a test file to compare the file contents
This is a test file to compare the file contents This is a test file to compare the file contents
Let us take file1
this is firstline
and a file named testfile2 contains
this is testfile2
then running this command
paste testfile testfile2 > outputfile ##will put this into outputfile
this is firstline this is testfile2
it contains contents of both files in columns.
who | paste - - will list users in two columns.

Available imp Options:
-d'char' separate columns with char instead of a tab.
-s merge subsequent lines from one file.
________________________________________

xyx:/>sort command.
sort command sort the lines of a file or files, in alphabetical order. for example if you have a file named testfile with these contents
zzz
aaa
1234
yuer
wer
qww
wwe
Then running
> sort testfile
will give us output of

1234
aaa
qww
wer
wwe
yuer
zzz
Available Options:
-b ignores leading spaces and tabs.
-c checks whether files are already sorted.
-d ignores punctuation.
-i ignores non-printing characters.
-n sorts in arithmetic order.
-o file put output in a file.
+m[-m] skips n fields before sorting, and sort upto field position m.
-r reverse the order of sort.
-u identical lines in input file apear only one time in output.
________________________________________

xyx:/>uniq command.
uniq command removes duplicate adjacent lines from sorted file while sending one copy of each second file.
Examples

sort names | uniq -d will show which lines appear more than once in names file.
Available Options:
-c print each line once,
-d print duplicate lines once, but no unique lines.
-u print only unique lines.
________________________________________
xyx:/>awk and nwak command.
awk is more like a scripting language builtin on all unix systems. Although mostly used for text processing, etc.

Examples: to show total space in your system

xyz:/>df -t | awk 'BEGIN {tot=0} $2 == "total" {tot=tot+$1} END {print (tot*512)/1000000}'

Here the output of command df -t is being passed into awk which is counting the field 1 after pattern "total" appears. Same way if you change $1 to $4 it will accumulate and display the addition of field 4 which is used space.
________________________________________

xyx:/>sed command.
sed command launches a stream line editor which are used at command line.
you can enter your sed commands in a file and then using -f option edit your text file. It works as
sed [options] files
Available options:
-e 'instruction' editing instruction to the files.
-f script set of instructions from the editing script.
-n suppress default output.
_______________________________________

xyx:/>vi editor.
vi command launches a visual editor. vi editor is a default editor of all Unix systems. It has several modes. In order to write characters you will need to hit i to be in insert mode and then start typing. Make sure that your terminal has correct settings, vt100 emulation works good if you are logged in using pc.
Once you are done typing then to be in command mode where you can write/search/ you need to hit
Examples:-
Case I. To edit a file type
vi filename
:w filename to write
Case II. In case you are done writing and want to exit
:w! will write and exit.
Available options:
i for insert mode.

I inserts text at the cursor
A appends text at the end of the line.
a appends text after cursor.
O open a new line of text above the cursor.
o open a new line of text below the cursor.

: for command mode
to invoke command mode from insert mode.
:!sh to run unix commands.
x to delete a single character.
dd to delete an entire line
ndd to delete n number of lines.
d$ to delete from cursor to end of line.
yy to copy a line to buffer.
P to paste text from buffer.
nyy copy n number of lines.
:%s/string A/string B /g to replace string A with string B in whole file.
G to go to last line in file.
1G to go to the first line in file.
w to move forward to next word.
b to move backwards to next word.
$ to move to the end of line.
J join a line with the one below it.

/string to search string in file.
n to search for next occurrence of string.

No comments:

Post a Comment