prof. navrati saxena ta: rochak sachan

17
Prof. Navrati Saxena [email protected] TA: Rochak Sachan [email protected]

Upload: others

Post on 07-Dec-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Prof. Navrati Saxena TA: Rochak Sachan

Prof. Navrati Saxena [email protected]

TA: Rochak Sachan [email protected]

Page 2: Prof. Navrati Saxena TA: Rochak Sachan

© Rochak Sachan 2

Purpose of essential utility Locating system information For better file management organizing your data System administration etc…

Page 3: Prof. Navrati Saxena TA: Rochak Sachan

© Rochak Sachan

Create two file using text editor(vi,cat….)

First file name as : sname

Second file name as: smark

Example:

3

$vi sname.txt S.ID Name ------------------------------- 11 Park 12 Lee 13 Kim 14 Won 15 Nam

$vi smark.txt S.ID Mark ------------------------------- 11 82 12 98 13 90 14 85 15 75

F1 F2

Page 4: Prof. Navrati Saxena TA: Rochak Sachan

© Rochak Sachan

Purpose to select any field of file Use cut command Select field using –f option (like : -f1 , -f2) Syntax:

cut -f{field number} {file-name} Example:

$cut -f2 sname ▪ It will print only F2 field(name)

$cut -f1 sname ▪ It will print only F1 field(S.ID)

4

Page 5: Prof. Navrati Saxena TA: Rochak Sachan

© Rochak Sachan

Use cut utility using redirection symbol(>)

$cut -f2 sname > /tmp/sn.tmp.$$

$cut -f2 smark > /tmp/sm.tmp.$$

$cat /tmp/sn.tmp.$$

$cat /tmp/sm.tmp.$$

5

Park

Lee

Kim

Won

Nam

82

98

90

85

75

Page 6: Prof. Navrati Saxena TA: Rochak Sachan

© Rochak Sachan

Paste utility join textual information together.

Use paste command Syntax:

paste {file1} {file2}

Example

$ paste sname smark

$ paste /tmp/sn.tmp.$$ /tmp/sm.tmp.$$

6

Page 7: Prof. Navrati Saxena TA: Rochak Sachan

© Rochak Sachan

The join utility joins, lines from separate files.

Work only if common field have in both file Syntax:

join {file1} {file2}

Example

$join sname smark

7

Page 8: Prof. Navrati Saxena TA: Rochak Sachan

© Rochak Sachan

Use to translate range of characters into other range of characters.

Syntax: tr {pattern-1} {pattern-2}

Example: $ tr "h2" "3x" < sname

▪ each occurrence of character 'h' is replace with '3' and '2' with 'x‘

▪ translate specific characters into other specific characters or range of characters into other ranges. ▪ h -> 3

▪ 2 -> x

8

$ tr "h2" "3x" < sname

11 Park 1x Lee 13 Kim 14 Won 15 Nam

Page 9: Prof. Navrati Saxena TA: Rochak Sachan

© Rochak Sachan

tr translate range of characters (i.e. small a to z) into other (i.e. to Capital A to Z) ranges.

Another example:

$ tr "[a-z]" "[A-Z]“

▪ after executing command type text in lower case like:

For terminate this program press ctrl+c

9

$ tr "[a-z]" "[A-Z]"

hi i am park

HI I AM PARK

what a magic

WHAT A MAGIC

Page 10: Prof. Navrati Saxena TA: Rochak Sachan

© Rochak Sachan

To manipulate data. Syntax:

awk 'pattern action' {file-name}

Example:

Create any file like ‘computer’

10

$vi computer.txt

Keyboard order 4

Memory good 10

Mouse okay 4

Printer good 12

Floppy good 5

Page 11: Prof. Navrati Saxena TA: Rochak Sachan

© Rochak Sachan

Use following command

$ awk '/good/ { print $3 }' computer

▪ This command will print third field which is containing ‘good’ word like:

Try one more command

$ awk '/good/ { print $1 }' computer

11

10

12

5

Page 12: Prof. Navrati Saxena TA: Rochak Sachan

© Rochak Sachan

Editing file without using editor Edit file or may be input from a pipeline Syntax:

sed {expression} {file}

Example:

Create a file using ‘vi’ or ‘cat’

12

$vi ipadorgalaxy

Samsung galaxy is good.

Apple ipad is good.

galaxy is better than the ipad.

Page 13: Prof. Navrati Saxena TA: Rochak Sachan

© Rochak Sachan

Use following command: $ sed '/galaxy/s//ipad/g' ipadorgalaxy >

/tmp/result.tmp.$$

$ cat /tmp/result.tmp.$$

13

Page 14: Prof. Navrati Saxena TA: Rochak Sachan

© Rochak Sachan

Removes duplicate lines Synatx:

uniq {file-name} Example:

Create any file like :

Try following command: ▪ uniq myinfo

14

$vi myinfo

Hello,

My name is Kim Sung

My ID is : 2011111777

2011111777

I am from SungkyunKwan University.

SungKyunKwan students are very good.

Page 15: Prof. Navrati Saxena TA: Rochak Sachan

© Rochak Sachan

To find matching pattern or word Syntax:

grep "word-to-find" {file-name} Example

Create any file

Try following command ▪ $ grep "too" demofile

15

$ vi demofile

hello world!

cartoons are good

especially toon like tom (cat)

what

the number one song

12221

they love us

I too

Page 16: Prof. Navrati Saxena TA: Rochak Sachan

© Rochak Sachan 16

Richard Petersen; "The Complete Reference,

Linux, Fourth Edition", Osborne/McGraw-Hill,

Christopher Negus; "Linux Bible, 2007 Edition", Wiley Publishing, Inc

http://www.freeos.com/guides/lsst/ http://bash.cyberciti.biz/guide/Main_Page

Page 17: Prof. Navrati Saxena TA: Rochak Sachan

rochak @skku.edu