Wednesday, October 26, 2011

Shell Variables

Working with Shell variables.

As like any other programming languages shell also has variable (most people dont know we can do programming with shell commands.). The shell variables takes single values as strings, even the numeric values is also considered as string.

Declaring and assigning shell variable:
You can declare and assign shell variable as simple as below.
bash-3.00$ color=yellow

You need to be careful while doing this. There should not be any space before and after equal to sign '=', if exists throws error.


Referencing a shell variable:
Below command will display the value stored in the variable color


bash-3.00$ echo $color
yellow


 

what is the output?
bash-3.00$ echo $coloryish

As no variable is declared as coloryish the output will be null. If you are actually interested to display "yellowyish", place the variable in curly braces as below.

bash-3.00$ echo ${color}yish
yellowyish


What if instead of variable color we supplied colour, which is not defined?
bash-3.00$ echo The dress is ${colour}ish
The dress is ish

How to handle this? Fortunately shell helps us to have default values to be set in case the variable is not defined as below
bash-3.00$ echo The dress is ${colour:-green}ish
The dress is greenish


The default value will come into effect only if the variable referenced  is not declared. The syntax :- tells the shell to print the following character

Assigning Variables with read command:
We can assign values to multiple variables at a time with read commnad.

bash-3.00$ read city state message
Thazhekode Kerala Hi Mom!

The above command declares three variables city, state and message and assign values respectively.
city=Thazhekode
state=Kerala
message=Hi Mom!

bash-3.00$ read city state message
Thazhekode New Jersey Hi Mom!

city=Thazhekode
state=New
message=Jersy Hi Mom!

Read command assigns an individual word to a specified variable, and the remaining words are assigned to the last variable. To handle this situation you can use the escape character

bash-3.00$ read city state message
Thazhekode New\ Jersey Hi Mom!




Tuesday, October 25, 2011

UNIX: Shell scripts

Definition: Shell scripts are a series of shell commands added in a file in exactly the order in which they’ll be
executed. Upon giving execute permission for this file, you can execute this entire set of commands by just entering the file name as like a command.

Example:
This example script will search for a specified file in all dirctories.


UNIX: Setting custom prompts

The below command is based on ksh


$ bash
bash-3.00$ PS1='Order me$'
Order me$

UNIX Command Aliases

With alias, you can define new commands that does whatever you like to do with unix in a customized way.

Format for using aliases in
csh:- alias word command-sequence
ksh:- alias word='command'

Example:
bash-3.00$ alias shiyas='ls -CF'

If you want to see the list of aliases created, you only need to enter the word alias

Example:
bash-3.00$ alias
alias ls='ls -CF'
alias shiyas='ls -CF'

Note:
There should not be any space befor or after '=' sign.

If you want to make availability of an alias permanent add it into .chrsc if you are using csh or to .profile if you are using korn shell

C shell History Commands

CommandFunction
!!Repeat the previous command
!$Repeat the last word of the previous command.
!*Repeat all but the first word of the previous command.
^a^bReplace a with b in the previous command.
!nRepeat command n from the history list.

Friday, October 21, 2011

Identifying the type of shell

If you screen shows a % symbol, it means you are using the C shell or modified C shell (tcsh
). If your prompt contains $you could be using the Bourne shell, the Korn shell, or a variant thereof.

UNIX difference btw :q and :q!

Open a file in vi editor and modify the content of the file.

Now try entering :q

of modern UNIX is the energy of the UNIX programming community; plenty of UNIX users
decide to write a new version of a command in order to solve slightly different problems, thus
spawning many versions of a command.
C
No write since last change (:quit! overrides)


You get the error as above. In this case you should enter :q!

Hence
:q means quit vi editor
:q! quit regardless of whether any changes have occured

UNIX: Searching within the file

What is the command to go to 10th line of the given file?
10G

Will 10g works?
No. Remember in UNIX case sensitive matters.
Then what happens if you enter G alone?
The cursor will go to last line of your file.

Search for a pattern in the file
Enter '/', will take you to the end of the file and then provide the patter to search...

primary players. Each is a little different from the other.) Another contributor to the sprawl
of modern UNIX is the energy of the UNIX programming community; plenty of UNIX users
decide to write a new version of a command in order to solve slightly different problems, thus
spawning many versions of a command.
C
/



primary players. Each is a little different from the other.) Another contributor to the sprawl
of modern UNIX is the energy of the UNIX programming community; plenty of UNIX users
decide to write a new version of a command in order to solve slightly different problems, thus
spawning many versions of a command.
C
/decide



primary players. Each is a little different from the other.) Another contributor to the sprawl
of modern UNIX is the energy of the UNIX programming community; plenty of UNIX users
decide to write a new version of a command in order to solve slightly different problems, thus
spawning many versions of a command.
C
/decide



To find next occurence press "n"


Here also the case matters. If you type "DECIDE" after the search command "/" UNIX will look for "DECIDE" not "decide"


What if I need to search backward
ans:
use the command ?
primary players. Each is a little different from the other.) Another contributor to the sprawl
of modern UNIX is the energy of the UNIX programming community; plenty of UNIX users
decide to write a new version of a command in order to solve slightly different problems, thus
spawning many versions of a command.
C
?decide



Now when you enter command "n" it will search backward!!

****
Hope this is helpful. Thanks Phoenix...

Thursday, October 20, 2011

Vi Editor

When we need to add some more lines to existing file we can use "cat" commnad, if we need to replace some string we have "sed"

But when we need to create a new file or edit already existing files we have to go for screen oriented editors.
There are different types of editors vi, ed, ex and emacs of which vi is the most choosen one. vi and emacs use the entire screen to edit.

Different modes of vi:
vi editor has two different modes, hence it is also called "modaleditor". The two different modes are "insert" and "command" mode. The way UNIX interprets your input depends on which mode we are. For example when we are in commnad mode and enter "dd", it will delete one single line, whereas if we are in edit mode entering "dd" will add "dd" to the file. So whenever you are in doubt in which mode you are, just keep on pressin escape button which will take you to command mode.


Diffrence beween vi and emacs:
emacs is a modeless editor. ie whenver you enter any key it adds to the file. Command in emac is entered by holding the control key while entering the command. For example ctrl+c will delete a character.

Cursor Moverments:

In keyboard the letters H,J,K,L comes in a series.
To move the curson left in a file -H
Right - L
Down - J
Up -K

You can move to the begining of next line by pressing the return key.
Move the cursor to the begining of line - ^,0 (zero)
Move the cursor to the end of line - $
Move forward word by word - w
Move backward word by word- b
Move forward to mid of the text in screen - ctrl+d
Move backward mid of the text - ctrl+u
Page down - ctrl+f
Page up -ctrl+b

vi insert modes:
There are basically four different ways you can get into vi insert mode.
i - insert text into file
a - append text into file
o - Open up a line below current line
O - Open up a line above current line (capital o)

Difference btw i and a for edit mode:
at present the cursor is on a, and now I am going to enter edit mode by entering "i"
On entring "i" the curson doesnt move and it will be there in the same location as above and now I am entering "cursor not moved" and you can see that the text entered to the left of "a" with cursor is not moved at all from "a" and then  press escape, you will find cursor move one space back.

On entering "a" to get into the edit mode, vi behaves differently.
Assume the cursor position is on "a" same as above and press "a" to move to edit mode, you can see that the cursor moves one place forward to "l" and whatever we enter is appended after the letter "l" and press escape, you see cursor moves one space back.

Deleting text with "vi" editor

While in command mode there are few commands provided by UNIX to delete the text from the file.

<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.
do it, too.
UNIX, by contrast, is much more like a spoken language, with
command options (which you learn about later in this lesson)
more complex commands acting akin to sentences. How you do a specific task can, therefore,
(pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>

Try entering "dd" which deletes an entire line and shift the lines up
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.
UNIX, by contrast, is much more like a spoken language, with
command options (which you learn about later in this lesson)
more complex commands acting akin to sentences. How you do a specific task can, therefore,
(pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>

"2dd" - will delete two lines
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.
more complex commands acting akin to sentences. How you do a specific task can, therefore,
(pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>

 "D" - Delete the line to the right of the cursor
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.
_

(pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>

Here after entering "D", we have
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system_
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


Now let us tweek a little of the "d" commnad
when "w" (word) is used with "d", dw this delete word by word


What is the command in Unix equivalent to delete and Backspace that we use in windows?
x - Deletes the text as like how we use the "delete" button in windows
X [<-] - Deletes the text as like when we use the backspace in windows.


What if you need to undo the delete?
You can undo only the last action using "u"

Example Scenario:
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>

type dw

<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


type "D"
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system_
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


type "u"
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


type "U"

<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


You can see that entire line is replaced. This can be achieved only if you hadn't moved from the line which you are editing.

Change the case of the letter
Use the command tild (~) or "delete" button in your keyboard.

Tuesday, October 18, 2011

Power Filters and File redirection

Two powerful commands that we learn in this section:
awk:helps you get specific columns of information, modify text, swap the order of column of information in a file
tee: enable you to save a copy of the data being transmitted through a pipeline

awk
Syntax: awk '{commands}'
flags:
-f file  specifies that the instruction should be read from the file rather than from command line
-Fc indicates that the program should consider the letter c as the separator between fields of

using print with awk
bash-3.00$ whobuild      pts/2        Oct 17 11:28    (10.222.46.189)
root       pts/10       Jul 12 18:45    (10.209.188.122)
scholm  pts/12       Oct 18 23:46    (10.203.54.223)
jmadh  pts/15       Oct 19 11:06    (10.203.54.225)


bash-3.00$ who | awk '{print}'
build      pts/2        Oct 17 11:28    (10.222.46.189)
root       pts/10       Jul 12 18:45    (10.209.188.122)
scholm  pts/12       Oct 18 23:46    (10.203.54.223)
jmadh  pts/15       Oct 19 11:06    (10.203.54.225)


bash-3.00$ who | awk '{print$1}'
build
root
scholm
jmadh

bash-3.00$ who | awk '{print$2}'pts/2
pts/10
pts/12
pts/15

bash-3.00$ who | awk '{print$3}'Oct
Jul
Oct
Oct
Here what you have seen is each column is considered as field and each field is represented by $1, $2, $3 etc...for field one, field two, field three respectively.

Much better customization:
bash-3.00$  who | awk '{ print "User " $1 " is on terminal line " $2 }'User build     is on terminal line pts/2
User root     is on terminal line pts/10
User scholm is on terminal line pts/12
User jmadh is on terminal line pts/15
Note: You need to be careful to place the strings to be printed in double quotes, else throw error as below, due to conflict with single quotes of the awk command.

bash-3.00$  who | awk '{ print 'User' $1 ' is on terminal line ' $2 }'awk: syntax error near line 1
awk: illegal statement near line 1


NF Variable:
NF indicates number of fields in a line.
When we use this variable without a $ sign it give us number of fields in each line

bash-3.00$ who | awk '{print NF}'6
6
6
6

When used with $ it displays the value at last field of each line
bash-3.00$ who | awk '{print $NF}'(10.222.46.189)
(10.209.188.122)
(10.203.54.223)
(10.203.54.225)

similarly NR stands for number of records.

Sum up the file size with the awk command:
bash-3.00$ ls -ltotal 80
-rw-r--r--   1 fwire    fwire         12 Oct 18 22:40 MyWords.txt
-rw-r--r--   1 fwire    fwire       2958 Oct 19 00:02 def.txt
-rw-r--r--   1 fwire    fwire       2976 Oct 19 00:01 def_new.txt
-rwxr-xr-x   1 fwire    fwire        141 Oct 18 23:40 search
-rw-r--r--   1 fwire    fwire       2958 Oct 18 21:57 what is unix.txt

file size is the fifthe field
bash-3.00$ ls -l | awk '{print $5}'
12
2958
2976
141
2958

Now we need to sum up all these size

bash-3.00$ ls -l | awk '{total=total+$5;print total}'0
12
2970
5946
6087
9045

To get the summed up vlaue alone use command below
bash-3.00$ ls -l | awk '{total+=$5;print total}'| tail -19045

Much better way is using END command
bash-3.00$  ls -l | awk '{total+=$5} END {print total}'9045
It will be better if we can put this code into some file so that our command line will be less lengthier
% cat << EOF > script
{ totalsize += $4 } END { print “You have a total of “totalsize .”}
EOF
The above command will create a file called script in your current directory with belwo content
{ totalsize += $4 } END { print “You have a total of “totalsize .”}

bash-3.00$ ls -l | awk -f scriptYou have a size of 9150
In above command -f tells awk command to look into the file script

 
We can use awk commnad for conditional execution of statement as well, as shown in the example below
% awk -F: ‘{ if (length($1) == 2) print $0 }’ /etc/passwd | wc -l

Here the passwd file is given as input for the awk command, where it says the field seperator is ':' and if the length of first field, ic account is two character lenght, then print the entire lines from the passwd file and this output  is then supplied to wc where we added -l flag, which gives us the total number of lines.

Say we recived an output of 5, this indicates that there are totally 5 accounts which are two character length.
 
tee  command has only one option, -a, which appends the stream data/ output to a specified file.
bash-3.00$ who | tee who.outfwire      pts/10       Jul 12 18:45    (10.209.188.122)
fwire      pts/12       Oct 18 23:46    (10.203.54.223)
fwire      pts/15       Oct 19 14:33    (10.203.54.224)


bash-3.00$ cat who.outfwire      pts/10       Jul 12 18:45    (10.209.188.122)
fwire      pts/12       Oct 18 23:46    (10.203.54.223)
fwire      pts/15       Oct 19 14:33    (10.203.54.224)


Here who | tee who.out placed the output of who to who.out file, which can also be achieved by the command who > who.out. Hence, what is the difference between these two.

In the first type of command the primary thing is we are displaying who are all there in the system and in the background the contents are saved into a specified file, here who.out (you can see the output in the screen for first type of command whereas you cant see for the second type). In the second type of command, the action is writing the output to  the specified file but not been displayed in the screen.


 





tee command:
information, rather than the default of white space

UNIX command "sed" examples

Here "sed" stands for "stream edit"
The main use of sed is to replace the string provided

bash-3.00$ cat def_new.txt | sed -e 's/LINUX/UNIX/g'
In the above example the word LINUX is replaced by UNIX in the file def_new.txt and is displayed.
s stands for substitute.

What if you need replace multiple strings?
bash-3.00$ cat def_new.txt | sed -e 's/LINUX/UNIX/g;s/system/COMPUTER'

sed command can also be used to delete
bash-3.00$ whobuild      pts/2        Oct 17 11:28    (10.222.46.189)
rmc       pts/10       Jul 12 18:45    (10.209.188.122)
root      pts/12       Oct 18 23:46    (10.203.54.223)

bash-3.00$ who | sed 'd'
wont display anything as it deletes everything

bash-3.00$ who | sed '1d'
delete the first line
rmc       pts/10       Jul 12 18:45    (10.209.188.122)
root      pts/12       Oct 18 23:46    (10.203.54.223)


bash-3.00$ who | sed '1,2d'
delete the first two lines
root      pts/12       Oct 18 23:46    (10.203.54.223)

bash-3.00$ who | sed '/build/d'delete only the user build
rmc       pts/10       Jul 12 18:45    (10.209.188.122)
root      pts/12       Oct 18 23:46    (10.203.54.223)


bash-3.00$ who | sed '/rmc/,/build/d'
delete both rmc and build
root      pts/12       Oct 18 23:46    (10.203.54.223)

bash-3.00$ who | sed '1,/rmc/d'root      pts/12       Oct 18 23:46    (10.203.54.223)
delete from line 1 till the line has rmc

UNIX "grep" command Examples

What is grep  command used for?

Ans: grep command can be considered as a "search" command for UNIX/LINUX systems. Used for searching text strings and some regular expressions.

Examples:

1) Search for a text string in a file

grep 'shiyas' /etc/passwd

above command search for all the occurence of the text 'shiyas' in the file passwd and print the lines having those text on the screen.

2) Search for the string in multiple files

grep 'shiyas' *

above command search for all the occurence of the text 'shiyas' in all the files in current dierectory and print the lines having those text on the screen along with the filenames. Here the wildcard character * looks for all the files in the current directory.

grep 'shiyas' *.txt
this command will search for the pattern in all txt files.

grep 'SHIYAS' /etc/passwd
wont display anything as it doesnt have any string SHIYAS, and UNIX is very sensitive for this case

inorder to avoid this case sensitive issue you can use below command which will help you to get the output you need irrespective of case

grep -i 'SHIYAS' /etc/passwd

4) what if I need to display all the lines which doesnt have the string called 'shiyas'
grep -v 'shiyas' /etc/passwd

Note: It is not necessary to place the string in single quotes, but mandatory when a space is included.

5) find all the sub-directories in the current directory
ls -al | grep '^d'

6) search for multiple patterns at one time (egrep)
egrep 'and|loop|cursor' example.txt

Note: egrep stand for "extended grep"
7) suppose you want to search for the strings "Foo" or "Goo" in all files in the current directory. That grep command would be:

grep '[FG]oo' *

8) Search for the string 'bond' but not 'jamesbond'

grep '^bond' /etc/password

9) Display the files that has the search string?
grep -l 'bond' /etc/password

10) Display the line number as well along withe lines that has the search string
grep -n 'bond' /etc/password

11) Display the lines before/after your search pattern

grep -B 4 'bond' /etc/password
this command displays 4 lines before the search pattern

grep -A 4 'bond' /etc/password
this command diplays 4 lines after the search pattern

12) List down all files which has pattern 'bond' in all the subdirectories in the current directory tree

find . -type f -exec grep -il 'foo' {} \;
This command will list down all the files which has the given search pattern, no matter at what level of directory it is

13) What is the grep command that returns specific number of rows for the search pattern provided.
For ex :- if your file has 10 row with the given string and you want to display only 5 out of 10 rows use below command

grep text 'string' | head -5

What if i want to search for multiple patterns?
Use egrep. egrep stands for "extended grep".

How it works?
What if you have to search for a pattern either this"" or this "", you have rely on "egrep" command which has more powerful notational scheme than "grep" command.

NotationMeaning
cMatches the character c
\cForces c to be read as the letter c, not as another meaning the character might have
^Beginning of the line
$End of the line
.Any single character
[xy]Any single character in the set specified
[^xy]Any single character not in the set specified
c*Zero or more occurrences of character c
c+One or more occurrences of character c
c?Zero or one occurrences of character c
a|bEither a or b
(a)Regular expression


Application of egrep is explained as below.

cat passwd | head-10

root:x:0:0:Super-User:/:/sbin/sh
daemon:x:1:1::/:
bin:x:2:2::/usr/bin:
sys:x:3:3::/:
adm:x:4:4:Admin:/var/adm:
lp:x:71:8:Line Printer Admin:/usr/spool/lp:
uucp:x:5:5:uucp Admin:/usr/lib/uucp:
nuucp:x:9:9:uucp Admin:/var/spool/uucppublic:/usr/lib/uucp/uucico
smmsp:x:25:25:SendMail Message Submission Program:/:
listen:x:37:4:Network Admin:/usr/net/nls:

When I am looking for one or more occurence of string 'c' in /etc/passwd file, using grep the command is as below
grep 'cc*' /etc/passwd
uucp:x:5:5:uucp Admin:/usr/lib/uucp:
nuucp:x:9:9:uucp Admin:/var/spool/uucppublic:/usr/lib/uucp/uucico

what if i am entering the command as
grep 'c*' /etc/passwd
this will list down all those lines which has zero or more occurence of the string 'c' which is equal to listing down the entire content of the file.

root:x:0:0:Super-User:/:/sbin/sh
daemon:x:1:1::/:
bin:x:2:2::/usr/bin:
sys:x:3:3::/:
adm:x:4:4:Admin:/var/adm:
lp:x:71:8:Line Printer Admin:/usr/spool/lp:
uucp:x:5:5:uucp Admin:/usr/lib/uucp:
nuucp:x:9:9:uucp Admin:/var/spool/uucppublic:/usr/lib/uucp/uucico
smmsp:x:25:25:SendMail Message Submission Program:/:
listen:x:37:4:Network Admin:/usr/net/nls:

whereas while using egrep it is as simple as,
egrep 'u+' /etc/passwd
root:x:0:0:Super-User:/:/sbin/sh
bin:x:2:2::/usr/bin:
lp:x:71:8:Line Printer Admin:/usr/spool/lp:
uucp:x:5:5:uucp Admin:/usr/lib/uucp:
nuucp:x:9:9:uucp Admin:/var/spool/uucppublic:/usr/lib/uucp/uucico
smmsp:x:25:25:SendMail Message Submission Program:/:
listen:x:37:4:Network Admin:/usr/net/nls:


then what if I am giving the command as
egrep 'cc+' /etc/passwd
this will list down all those lines which has two or more occurence of the charater 'c'
uucp:x:5:5:uucp Admin:/usr/lib/uucp:
nuucp:x:9:9:uucp Admin:/var/spool/uucppublic:/usr/lib/uucp/uucico


Now let us look into the question asked above, how to search for multiple patterns.
It is as simple as this

bash-3.00$ cat passwd |head -10 | egrep 'uu|mm'uucp:x:5:5:uucp Admin:/usr/lib/uucp:
nuucp:x:9:9:uucp Admin:/var/spool/uucppublic:/usr/lib/uucp/uucico
smmsp:x:25:25:SendMail Message Submission Program:/:

I want to list down those lines which starts with a
bash-3.00$ cat passwd |head -10 | egrep '^a' passwdadm:x:4:4:Admin:/var/adm:
apache:x:104:103:Apache User:/export/apache:/bin/bash


What if I want to list down those lines which starts with a,b,c,d
bash-3.00$ cat passwd |head -10 | egrep '^[a-d]' passwddaemon:x:1:1::/:
bin:x:2:2::/usr/bin:
adm:x:4:4:Admin:/var/adm:
build:x:102:103:Build User:/export/build:/bin/bash
apache:x:104:103:Apache User:/export/apache:/bin/bash
csvn:x:204:204:CollabNet Subversion User:/opt/CollabNet_Subversion:/bin/sh


What is fgrep and how it is used?
fgrep stands for 'file based grep'. For example I have a file of search strings say MyWords.txt and what I need is to list down the lines in 'appreport.txt' file having these search strings.

For this let me go the directory
bash-3.00$ cd /tmp/shiyas/skills/unix/
bash-3.00$ lswhat is unix.txt
bash-3.00$ cat what\ is\ unix.txt | head -5X is a computer operating system, a control program that works with users to run
programs, manage resources, and communicate with other computer systems. Several people
can use a UNIX computer at the same time; hence UNIX is called a multiuser system. Any
of these users can also run multiple programs at the same time; hence UNIX is called
multitasking. Because UNIX is such a pastiche.a patchwork of development.it.s a lot


let me create one file Mywords.txt having words manage & such

bash-3.00$ vi MyWords.txtmanage
such
~
~
~
:wq!

"MyWords.txt" [New file] 2 lines, 12 characters
bash-3.00$ cat MyWords.txtmanage
such


bash-3.00$ fgrep -f MyWords.txt what\ is\ unix.txtprograms, manage resources, and communicate with other computer systems. Several people
multitasking. Because UNIX is such a pastiche.a patchwork of development.it.s a lot
used in high-speed networking, file revision management, and software development.
Why is having all this choice such a big deal? Think about why Microsoft MS-DOS and the


Is there any alernative for typing down such a long command?
Ofcourse you have, and it is using 'alias'
the command for alias is
bash-3.00$ alias search='fgrep -i -f MyWords.txt'
you have to be very careful with the syntax else will through error.
the space after '=' will give error as below
bash-3.00$ alias search= 'fgrep -i -f MyWords.txt'
bash: alias: fgrep -i -f MyWords.txt: not found


Now lets see how we can use this alias
bash-3.00$ search what\ is\ unix.txtprograms, manage resources, and communicate with other computer systems. Several people
multitasking. Because UNIX is such a pastiche.a patchwork of development.it.s a lot
used in high-speed networking, file revision management, and software development.
Why is having all this choice such a big deal? Think about why Microsoft MS-DOS and the
bash-3.00$


Now I am removing the alias function
bash-3.00$ unalias search
bash-3.00$ search what\ is\ unix.txt
bash: search: command not foundbash-3.00$

I need to display only the words that match instead of the entire line, what should I do?
To achieve this we have to use the 'awk' command , for which below is sample
bash-3.00$ echo 'My name is shiyas' | awk '{for (i=1;i<=NF;i++) print $i}'
My
name
is
shiyas

bash-3.00$
NF stands for number of fields (here it is 4)

Now lets work on displaying only the matching word alone, not the entire line
Logic we are going to implement is:
Make the content of file a list of one word each as above and now search for the pattern
step1: awk '{for (i=1;i<=NF;i++) print $i}'
step2: fgrep -i -f MyWords.txt what\ is\ unix.txtFor this lets code one shell script to incorporate both above commands.

bash-3.00$ cat search
#Wrongwords - show a list of commonly misused words in the file
cat $* | \
awk .{for (i=1;i<=NF;i++) print $i}. |\
fgrep -i -f MyWords.txt


bash-3.00$ unalias search

Give execute permission for the above shell script
bash-3.00$ chmod +x search

****
Hope this is helpful. Thanks Phoenix

Saturday, October 15, 2011

Unix Commands

> To sort filenames alphabetically regardless of case

% ls -1 | sort -f

ls -1 will list the files in one single column, this output is passed to sorting, where sort -f makes sure that the list is sorted not considering the case.

> Sorting lines of a file?
sort < shiyas.txt

> find the largest file in your directory
bash-3.00$ ls -s | sort -nr
ls -s list the files and folder along with size
pass this to sort numerically and in reverse order.
the first line gives you the largest file along with its size in blocks.

Here if we need to see only the highest 5 files add head -5
bash-3.00$ ls -s | sort -nr | head -5

I have a file in which there is space after each line. what should be the unix command to remove these spaces?
I i am using "uniq" command for this purpose,  it wont work, as uniq command will consider the duplication of lines only if the duplicants are adjacent. What should i do in this case?

sort text.txt | uniq > new.txt

here the sorting will sort your file having space between each line, in a manner that the blank line will come first. Supply this output to the uniq command and now the blank lines are adjacent, hence uniq command take one among them and place this new content to a new file.
Problem solved !!

I want to display the contents of my file along with the line number. How could I achieve that?
% cat -n text.txt
here the flag n will do the job of giving line numbers

We have one alternative for this
% nl text.txt
"nl" will do the same job of "cat -n"

> test.txt has 10 lines of which 5 are blank lines. When nl test.txt is fired, it is expected that the output is a numbered lines. What will be the last number? 5 or 10
ans: 5. nl by default only numbers the lines that are not blank.

So, what should I do to number the blank lines as well?
% nl -ba test.txt
will number all the lines.

Is there any alternativ for nl command?
% nl -bt test.txt
will number only printable text


I want to number all those lines which has specific pattern.
% nl -bpORA -s: shiyas_new.txt

here "bpORA" looks for the word ORA in each line
"-s:" is used for seperator which seperates number and line like 1:


search for the line in a file for a given pattern:
%grep STATUS ../bin/fatwire.sh
here STATUS is the word you are looking for in the fatwire.sh

Output:
echo "$STATUS"
export STATUS
List down all the files under the directory which has a particular pattern in it
grep 'INGESTION' * */*

* expands your search beyond the files in the current dirctory
and */* expands your search to all files contained one directory below the current point.


I have a filename called 'wha is unix.txt' and I need to display the content of this file in screen for which I am using the command
cat 'what is unix.txt',
will this work and is there any other alternative for this?

Ofcourse this will work and you have one alternative as well. The alternative is:
cat what\ is\ unix.txt
Here what we have done is, we have used the escape character which tells the unix system to interpret the space as space itself.