15
UNIX TRAINING UNIX TRAINING - Day 2 Day 2

Find Grep and Vi in Aix

Embed Size (px)

Citation preview

8/6/2019 Find Grep and Vi in Aix

http://slidepdf.com/reader/full/find-grep-and-vi-in-aix 1/15

UNIX TRAININGUNIX TRAINING -- Day 2Day 2

8/6/2019 Find Grep and Vi in Aix

http://slidepdf.com/reader/full/find-grep-and-vi-in-aix 2/15

8/6/2019 Find Grep and Vi in Aix

http://slidepdf.com/reader/full/find-grep-and-vi-in-aix 3/15

Essential UNIX commandsEssential UNIX commands

Diff command Diff command  Take the different between two f iles. $diff Take the different between two f iles. $diff --bw f l1 f l2bw f l1 f l2 To know the login name of user:To know the login name of user: $logname$logname To know the user identity and group identity values: $idTo know the user identity and group identity values: $id To f ind the name of Unix system: $To f ind the name of Unix system: $unameuname

 Also try Also try unameuname ± ±n (tells hostname) ; unamen (tells hostname) ; uname ± ±aa What this gives?What this gives?

To see who all are currently logged in: $To see who all are currently logged in: $ whowhoWhat does µwho am I¶ gives?What does µwho am I¶ gives?

To display current date and time:To display current date and time: $ date$ dateWant more var iety on the display format:Want more var iety on the display format:$$ date µ+DATE: %ddate µ+DATE: %d--%m%m--%y %n TIME: %H:%M:%S¶%y %n TIME: %H:%M:%S¶

%D=>date as mm/dd/yy%D=>date as mm/dd/yy

%a=>abbreviated weekday (Sun%a=>abbreviated weekday (Sun--Sat)Sat)%h=>abbreviated month (Jan%h=>abbreviated month (Jan--Dec)Dec)

%j=>day of year (001%j=>day of year (001--366)366)%w=>day of week (0%w=>day of week (0--6) Sunday is 06) Sunday is 0%T=>time as HH:MM:SS%T=>time as HH:MM:SS

8/6/2019 Find Grep and Vi in Aix

http://slidepdf.com/reader/full/find-grep-and-vi-in-aix 4/15

Commands Commands ± ± Cond«Cond«

grep: grep command searches specif ied input globally for a match with thegrep: grep command searches specif ied input globally for a match with thesupplied patter n and display it.supplied patter n and display it.

grep points citi _ lspgrep points citi _ lsp

grep points citi _ lsp amex _ lsp # Search a patter n in more than one f ilegrep points citi _ lsp amex _ lsp # Search a patter n in more than one f ile

grep µtotal points¶ citi _ lsp # if patter n compr ises of more than one wor d use µ µgrep µtotal points¶ citi _ lsp # if patter n compr ises of more than one wor d use µ µ

grepgrep --i points amex _ lsp # Ignores the casei points amex _ lsp # Ignores the case

grepgrep --n points amex _ lsp #Pr int number s of lines in which patter n was matchedn points amex _ lsp #Pr int number s of lines in which patter n was matched

grep points *lsp # searches in all the f iles end with lspgrep points *lsp # searches in all the f iles end with lsp

grep [Rr]esponse citi _ lsp # Searches for response or Responsegrep [Rr]esponse citi _ lsp # Searches for response or Response

grep p??r citi _ lsp #Search for all 4 letter wor ds start with p and end with r grep p??r citi _ lsp #Search for all 4 letter wor ds start with p and end with r 

grepgrep ± ±v points amex _ lsp #Searches for line without the patter n points.v points amex _ lsp #Searches for line without the patter n points. grepgrep ± ±c pts mylsp #Pr ints only the number of matches, without quoting the textc pts mylsp #Pr ints only the number of matches, without quoting the text

grepgrep ± ±l pts *lsp #Pr ints only the f iles which has match without quoting the textl pts *lsp #Pr ints only the f iles which has match without quoting the text

8/6/2019 Find Grep and Vi in Aix

http://slidepdf.com/reader/full/find-grep-and-vi-in-aix 5/15

Commands Commands ± ± Cond«Cond«

findfind command: Helps in locating the f iles which meet the search cr iter iacommand: Helps in locating the f iles which meet the search cr iter ia

f ind .f ind . ± ±name mylspname mylsp ± ±pr int # Search the f ile mylsp from current dir , downwar dspr int # Search the f ile mylsp from current dir , downwar ds

f ind .f ind . --name [ab]*name [ab]*

f ind /usr f ind /usr --type d type d --name backup # Search for backup dir from /usr downwar dsname backup # Search for backup dir from /usr downwar ds

f ind /usr f ind /usr ± ±type f type f ± ±name backup # Search for backup f ile from /usr downwar dsname backup # Search for backup f ile from /usr downwar ds

f ind /usr/lear ner f ind /usr/lear ner ± ±links 2 # f iles with 2 linkslinks 2 # f iles with 2 links

f ind /usr/lear ner f ind /usr/lear ner ± ±links +2 # Files with more than 2 linkslinks +2 # Files with more than 2 links

f ind .f ind . ± ±perm 744 #All f iles from the current dir downwar ds with permissions 744perm 744 #All f iles from the current dir downwar ds with permissions 744

f ind .f ind . ± ±atime 7atime 7 ± ±pr int # All f iles from the current dir downwar ds which werepr int # All f iles from the current dir downwar ds which wereaccessed exactly 7 days backaccessed exactly 7 days back

f ind .f ind . ± ±atime +7atime +7 ± ±pr int # All f iles from the current dir downwar ds which werepr int # All f iles from the current dir downwar ds which wereaccessed more than 7 days agoaccessed more than 7 days ago

f ind .f ind . ± ±mtime +7mtime +7 ± ±pr int #Files which have not been modif ied since last 7 days (or pr int #Files which have not been modif ied since last 7 days (or in other wor ds, which have been modif ied more than 7 days ago)in other wor ds, which have been modif ied more than 7 days ago)

f ind .f ind . ± ±name [ab]*name [ab]* --exec grep µvalues¶ {}exec grep µvalues¶ {} \\; # What is this?; # What is this?

8/6/2019 Find Grep and Vi in Aix

http://slidepdf.com/reader/full/find-grep-and-vi-in-aix 6/15

Commands Commands ± ± Cond..Cond..

Head command: Use to view the lines of a f ile from the begin.Head command: Use to view the lines of a f ile from the begin. headhead f ile_ name # By default pr ints the f ir st 10 linesf ile_ name # By default pr ints the f ir st 10 lines headhead --1515 f ile_ name # Pr ints f ir st 15 linesf ile_ name # Pr ints f ir st 15 lines head head --1 emp_ list | wc1 emp_ list | wc ± ±c What will be output?c What will be output?

Tail command: Counterpart of head command. View the lines from the endTail command: Counterpart of head command. View the lines from the end

tailtail f ile_ name # By default pr ints the last 10 linesf ile_ name # By default pr ints the last 10 lines tailtail --1515 f ile_ name # Pr ints last 15 linesf ile_ name # Pr ints last 15 lines Monitor ing the growth of a f ile:Monitor ing the growth of a f ile: tailtail ± ±f f <f ile_ name><f ile_ name>

Tr command: Translates the character s. Does not accept f ile name as ar g.Tr command: Translates the character s. Does not accept f ile name as ar g. tr µabc¶ µdef¶ < myf ile: Translation will take place character by character.tr µabc¶ µdef¶ < myf ile: Translation will take place character by character. tr µ/|¶ µ:¶ < emplist | head tr µ/|¶ µ:¶ < emplist | head --3 What is the output?3 What is the output? head head --3 emplist | tr µ[a3 emplist | tr µ[a--z]¶ µ[Az]¶ µ[A--Z]¶Z]¶

Pipe (|) Command:Pipe (|) Command: $ who | grep shailesh$ who | grep shailesh $ ls | grep f ile1$ ls | grep f ile1 $ who | wc$ who | wc ± ±ll $ cat poetry | grep love$ cat poetry | grep love

8/6/2019 Find Grep and Vi in Aix

http://slidepdf.com/reader/full/find-grep-and-vi-in-aix 7/15

UNIX Commands UNIX Commands -- CondCond

To change your login passwor d: $ passwdTo change your login passwor d: $ passwd

To pr int a message in lar ge letter s: $ banner welcome to unixTo pr int a message in lar ge letter s: $ banner welcome to unix

Count the number of lines, wor ds and character s in a f ile:Count the number of lines, wor ds and character s in a f ile: wcwc --ll f ile_ namef ile_ name Gives the number of lines in the f ileGives the number of lines in the f ile

wcwc --ww f ile_ namef ile_ name Gives the number of wor ds in the f ileGives the number of wor ds in the f ile

wcwc --cc f ile_ namef ile_ name Gives the number of wor ds in the f ileGives the number of wor ds in the f ile

Please store the following in a file:emp_listPlease store the following in a file:emp_list

1023|Sourav Ganguly |10|General Manager |450001023|Sourav Ganguly |10|General Manager |45000

1024|Sachin Tendulkar |20|Vice President |500001024|Sachin Tendulkar |20|Vice President |50000

1000|Kapil Dev |10|Marketing Head |340001000|Kapil Dev |10|Marketing Head |34000

4235|Rahul Dravid |30|Insurance VP |280004235|Rahul Dravid |30|Insurance VP |28000

2345|Anil Kumble |40|Administration VP |460002345|Anil Kumble |40|Administration VP |46000

6345|V.V.S Laxman |30|Marketing Head |340006345|V.V.S Laxman |30|Marketing Head |34000

8/6/2019 Find Grep and Vi in Aix

http://slidepdf.com/reader/full/find-grep-and-vi-in-aix 8/15

VI (Visual) Editor VI (Visual) Editor 

Very fast and powerful editor.Very fast and powerful editor.

To create a new f ile or open existing f ile: vi f ilenameTo create a new f ile or open existing f ile: vi f ilename

vi has two modes: thevi has two modes: the command mode and the insert modecommand mode and the insert mode. It is . It is essential that you know which mode you are in at any given point in time.essential that you know which mode you are in at any given point in time.When you are in command mode, letter s of the keyboar d will be interpreted When you are in command mode, letter s of the keyboar d will be interpreted 

as commands. When you are in insert mode the same letter s of theas commands. When you are in insert mode the same letter s of thekeyboar d will type or edit text. vi always starts out in command mode.keyboar d will type or edit text. vi always starts out in command mode.

Enter ing text: To begin enter ing text in an empty f ile, you must f ir st changeEnter ing text: To begin enter ing text in an empty f ile, you must f ir st changefrom the command mode to the insert mode. To do this, type the letter i.from the command mode to the insert mode. To do this, type the letter i. Any time to come back to command mode press ESC button. Any time to come back to command mode press ESC button.

Moving One Character at a TimeMoving One Character at a Time

h: left one spaceh: left one space l: r ight one spacel: r ight one space

 j: down one space j: down one space

k: up one spacek: up one space

8/6/2019 Find Grep and Vi in Aix

http://slidepdf.com/reader/full/find-grep-and-vi-in-aix 9/15

VI Cond«VI Cond«

Moving among Wor ds and LinesMoving among Wor ds and Lines

w:w: moves the cur sor forwar d one wor d. So what 5w does?moves the cur sor forwar d one wor d. So what 5w does?

b:b: moves the cur sor backwar d one wor d (if in the middle of a wor d, b will movemoves the cur sor backwar d one wor d (if in the middle of a wor d, b will moveyou to the beginning of the current wor d). What 12b will do?you to the beginning of the current wor d). What 12b will do?

e:e: moves to the end of a wor d.moves to the end of a wor d.

Shotcuts: Two short cuts for moving quickly on a line include theShotcuts: Two short cuts for moving quickly on a line include the $$ and theand the 00(zero) keys. The(zero) keys. The $$ key will move you to the end of a line, while the 0 will key will move you to the end of a line, while the 0 will 

move you quickly to the beginning of the linemove you quickly to the beginning of the line..

Screen movement:Screen movement:

ctrlctrl--f:f: scrolls down one screenscrolls down one screen

ctrlctrl--b:b: scrolls up one screenscrolls up one screen ctrlctrl--u:u: scrolls up a half a screenscrolls up a half a screen

ctrlctrl--d:d: scrolls down a half a screenscrolls down a half a screen

8/6/2019 Find Grep and Vi in Aix

http://slidepdf.com/reader/full/find-grep-and-vi-in-aix 10/15

VI Cond..VI Cond..

Searching for text:Searching for text: When you are in command mode, type aWhen you are in command mode, type a / / followed thefollowed thetext you wish to search for. When you press text you wish to search for. When you press ReturnReturn , the cur sor will move to, the cur sor will move tothe f ir st incidence of that str ing of text. You can repeat the search by typing the f ir st incidence of that str ing of text. You can repeat the search by typing nn or search in a backwar ds direction by using or search in a backwar ds direction by using NN ..

Basic Editing:Basic Editing:

Deleting Characters, Words and Lines:Deleting Characters, Words and Lines:

x: deletes the character under the cursor. What 10x does?x: deletes the character under the cursor. What 10x does?

X: deletes the character to the left of your cursor.X: deletes the character to the left of your cursor.

dw: deletes from the character selected to the end of the word.dw: deletes from the character selected to the end of the word.

dd: deletes all the current line. What 5dd does?dd: deletes all the current line. What 5dd does?

D: deletes from th

e cu

rrent ch

aracter to th

e end of th

e line.D: deletes from th

e cu

rrent ch

aracter to th

e end of th

e line. Pasting text using Put: The Put command will paste in the last portion of Pasting text using Put: The Put command will paste in the last portion of 

text that was deleted since deleted text is stored in a buffer. To use thistext that was deleted since deleted text is stored in a buffer. To use thiscommand, place the cursor where you wish the deleted text to appear.command, place the cursor where you wish the deleted text to appear.Then use p to reinsert the text. What is I use P?Then use p to reinsert the text. What is I use P?

8/6/2019 Find Grep and Vi in Aix

http://slidepdf.com/reader/full/find-grep-and-vi-in-aix 11/15

VI Cond«VI Cond«

Yanking the text: Yanking the text: "[a"[a--z]nyy: yank next n lines into named buffer [az]nyy: yank next n lines into named buffer [a--z]z]

"[a"[a--z]p/P: place the contents of selected buffer below/above the current linez]p/P: place the contents of selected buffer below/above the current line

Replacing or Changing Characters, Words, and LinesReplacing or Changing Characters, Words, and Lines

r: replaces the current character with the next character you enter/type. Oncer: replaces the current character with the next character you enter/type. Onceyou enter the character you are retur ned to command mode.you enter the character you are retur ned to command mode.

R: puts you in overtype mode until you hit ESC which will then retur n you toR: puts you in overtype mode until you hit ESC which will then retur n you tocommand mode.command mode.

cw: changes and replaces the current wor d with text that you type. A dollar sign cw: changes and replaces the current wor d with text that you type. A dollar sign mar ks the end of the text you're changing. Pressing ESC when you f inish will mar ks the end of the text you're changing. Pressing ESC when you f inish will retur n you to command moderetur n you to command mode..

Joining Lines:Joining Lines: place your cur sor on the f ir st line to be joined and typeplace your cur sor on the f ir st line to be joined and type JJ .. As with other commands, you can precede As with other commands, you can precede JJ with a number to join multiplewith a number to join multiplelines (4J joins 4 lines)lines (4J joins 4 lines)

8/6/2019 Find Grep and Vi in Aix

http://slidepdf.com/reader/full/find-grep-and-vi-in-aix 12/15

VI Cond«VI Cond«

Undoing:Undoing: When you make a mistake you can undo it. DO NOT move theWhen you make a mistake you can undo it. DO NOT move thecur sor from the line where you made the change. Then try using one of thecur sor from the line where you made the change. Then try using one of thefollowing two commands:following two commands: u: undoes the last change you made anywhere in the f ile. Using u again will u: undoes the last change you made anywhere in the f ile. Using u again will 

"undo the undo"."undo the undo".

U: undoes all recent changes to the current line. You can not have moved fromU: undoes all recent changes to the current line. You can not have moved fromthe line to recover the or iginal line.the line to recover the or iginal line.

Quitting and Saving a FileQuitting and Saving a File :w to save your f ile but not quit vi (this is good to do per iodically in case of :w to save your f ile but not quit vi (this is good to do per iodically in case of 

machine crash!).machine crash!).

:q to quit if you haven't made any edits.:q to quit if you haven't made any edits.

:wq to quit and save edits (Can use ZZ also).:wq to quit and save edits (Can use ZZ also).

Quitting without Saving a FileQuitting without Saving a File :q! wipes out all edits and allows you to exit from vi.:q! wipes out all edits and allows you to exit from vi.

:e! reads the or iginal f ile back in so that you can start over.:e! reads the or iginal f ile back in so that you can start over.

Repeating a command:Repeating a command: UseUse dot(.)dot(.) to repeat the last basic cmd issued.to repeat the last basic cmd issued.

8/6/2019 Find Grep and Vi in Aix

http://slidepdf.com/reader/full/find-grep-and-vi-in-aix 13/15

VI Cond«VI Cond«

Customizing vi environment:Customizing vi environment: We can customize several options within vi We can customize several options within vi using using :set:set command.command.

:set wrapmargin=10:set wrapmargin=10 specify that you wish to have a r ight mar gin of 10.specify that you wish to have a r ight mar gin of 10.

:set number :set number . This command causes vi to display line number s in the f ile you. This command causes vi to display line number s in the f ile youare wor king on.are wor king on.

:set ic:set ic Ignore the case.Ignore the case.

To view a listing of other options, you could typeTo view a listing of other options, you could type :set all:set all .. ToTo add a blank lineadd a blank line in between the f ile, place the cur sor on the line wherein between the f ile, place the cur sor on the line where

you want to insert the blank line after that, press you want to insert the blank line after that, press oo and type Retur nand type Retur n..

Appending text:Appending text: aa Begin appending text, one character to the r ight of current cur sor locationBegin appending text, one character to the r ight of current cur sor location

AA Begin appending text at the end of the current lineBegin appending text at the end of the current line

8/6/2019 Find Grep and Vi in Aix

http://slidepdf.com/reader/full/find-grep-and-vi-in-aix 14/15

VI Cond«VI Cond«

Searching & Substitution commands:Searching & Substitution commands:

 / [string] / [string] search forwar d for str ingsearch forwar d for str ing

? [string]? [string] search backwar ds for str ingsearch backwar ds for str ing

nn repeat last searchrepeat last search

NN repeat search in opposite directionrepeat search in opposite direction

cw cw change the contents of the current wor d, (use ESC to stop replacementchange the contents of the current wor d, (use ESC to stop replacementmode)mode)

c$c$ Replace all content to the r ight of cur sor (exit replacement mode with ESC)Replace all content to the r ight of cur sor (exit replacement mode with ESC)

c0c0 Replace all content to the left of cur sor (exit with ESC)Replace all content to the left of cur sor (exit with ESC)

:1,$s/s1/s2/g:1,$s/s1/s2/g global replacement of str ing1 with str ing2global replacement of str ing1 with str ing2

r r replace current character with next character typedreplace current character with next character typed

8/6/2019 Find Grep and Vi in Aix

http://slidepdf.com/reader/full/find-grep-and-vi-in-aix 15/15

VI Cond..VI Cond..

Some Fancy Stuff:Some Fancy Stuff:

:1,10w file write lines 1 through 10 to file newfile:1,10w file write lines 1 through 10 to file newfile

:340,$w >> file write lines 340 through the end of the file and:340,$w >> file write lines 340 through the end of the file andappend to file newfileappend to file newfile

:sh escape temporarily to a shell:sh escape temporarily to a shell ^d return from shell to VI^d return from shell to VI

:![command] execute UNIX command without leaving VI:![command] execute UNIX command without leaving VI

:r![command] read output of command into VI:r![command] read output of command into VI

:r[filename] read filename into VI:r[filename] read filename into VI

:^g list cu

rrent line nu

mber :^g list cu

rrent line nu

mber