linux lab manual.doc

58
Ex No: 6 Date: UNIX EDITOR 1. Starting vi Editor 2. Inserting Text ESC Press i – Inserts before character

Upload: ganesh-arora

Post on 25-Nov-2015

59 views

Category:

Documents


2 download

DESCRIPTION

basic Linux program

TRANSCRIPT

Ex No: 6

Date:

UNIX EDITOR

1. Starting vi Editor

2. Inserting Text

ESC Press i Inserts before character

ESC Press I Inserts at Beginning of the Line

ESC Press O Puts Blank Line after the current line

ESC Press o Puts Blank Line after the current line

Esc Press a Appends after the cursor

Esc Press A Appends at the End of the Line

3. Moving Around

Esc Press k Moves up

Esc Press h Moves left

Esc Press l Moves right

Esc Press j Moves down

Esc Press w Moves forward by a word

Esc Press 5w Moves forward by a 5 words

4. Cutting,Copying and Pasting

ESC Press yy Copy a line into temporary storage

ESC Press nyy Copy n lines into temporary storage

ESC Press p paste the copied lines into current position of the cursor

5. Replacing Text

ESC Press r Replace a single character

ESC Press R Invoke insert mode and go on replacing till ESC is pressed

ESC Press s Deletes the current character and inserts character

ESC Press S Removes all the characters on the current line and allows the user to enter data

6. Undo Action

ESC Press u Undoing the pervious changes

ESC Press U Undoing all the changes in the current line

7. Screen Manipulation

ESC CTRL f scrolls a screen forward

ESC CTRL b scrolls a screen backward

ESC CTRL u scrolls half a screen backward

ESC CTRL d scrolls half a screen forward

ESC CTRL L Refreshes the screen

8. Getting out

ESC :q Quit vi

ESC :q! Quit vi without saving changes

ESC :w save and Continue working in vi

ESC :wq - Save the changes and exit from vi

ESC :x - save and exit from vi

ESC ZZ - Save the changes and exit from vi

9. Deleting Text

ESC x - Deletes one character

ESC nx - Deletes the next n characters

ESC dw Deletes one word at the cursor position

ESC n dw Deletes n words from cursor position

ESC dd - Deletes one line

ESC n dd Deletes the next n lines

10. Searching for Text

/mode Search for a pattern mode forward

?Mode Search for a pattern mode backward

Replaces one word with another word

To set the number for each line

Ex No: 5

Date:

FILTERS

1. Word/Line Count

[cse001@localhost dr2]$ wc -c sample.txt /*byte count */

22 sample.txt

[cse001@localhost dr2]$ wc -m sample.txt /* count characters */

22 sample.txt

[cse001@localhost dr2]$ wc -l sample.txt /* count lines */

1 sample.txt

[cse001@localhost dr2]$ wc -w sample.txt /* count words */

5 sample.txt

[cse001@localhost dr2]$ wc file sample.txt /* all counts */

1 5 22 sample.txt

1 5 22 total

[cse001@localhost dr2]$ wc sample.txt

1 5 22 sample.txt

2. Last N Lines : tail

[csea0030@localhost ~]$ cat sample.txt

1.f

2.r

3.e

4.k

5.w

6.q

7.p

8.a

9.s

10.l

11.o

12.u

[csea0030@localhost ~]$ tail sample.txt

3.e

4.k

5.w

6.q

7.p

8.a

9.s

10.l

11.o

12.u

[csea0030@localhost ~]$ tail sample.txt -n 5

8.a

9.s

10.l

11.o

12.u

3. First N Lines :head

[csea0030@localhost ~]$ head -5 sample.txt

1.f

2.r

3.e

4.k

5.w

[csea0030@localhost ~]$ head sample.txt

1.f

2.r

3.e

4.k

5.w

6.q

7.p

8.a

9.s

10.l

4. Sorting the File

[cse001@localhost ~]$ sort vegetables.txt

Brinjal

Chilli

Lady's Finger

Onion

Potato

Pumpkin

[cse001@localhost ~]$ sort -r vegetables.txt

Pumpkin

Potato

Onion

/* reverse order*/

Lady's Finger

Chilli

Brinjal

[cse001@localhost ~]$ sort -c vegetables.txt /* ordered */

sort: vegetables.txt:3: disorder: Onion

[cse001@localhost ~]$ cat numerical.txt

45

23

78

90

12

[cse001@localhost ~]$ sort -n numerical.txt /* numerical order */

12

23

45

78

90

[cse001@localhost ~]$ cat numerical.txt

45

23

78

90

12

45

78

[cse001@localhost ~]$ sort -u numerical.txt /* removes duplicates records */

12

23

45

78

90

[cse001@localhost ~]$ cat vegetables.txt

Potato

Onion

Lady's Finger

Chilli

Pumpkin

Brinjal

[cse001@localhost ~]$ sort -b vegetables.txt /* removes blank spaces */

Brinjal

Chilli

Lady's Finger

Onion

Potato

Pumpkin

[cse001@localhost ~]$ cat >list.txt

FIRST

SECOND

THIRD

FOURTH

[cse001@localhost ~]$ sort -f list.txt /* folds uppercase */

FIRST

FOURTH

SECOND

THIRD

5. List Spelling Errors : spell

6. Selecting Fields form Files : cut

[user31@localhost ~]$ cat data.txt

Jovitha 1979 2.10

Varsha 1980 15.0

Suthan 1978 17.4

[user31@localhost ~]$ cut -f 1,3 data.txt

Jovitha 2.10

Varsha 15.0

Suthan 17.4

[user31@localhost ~]$ cut -c 1,2 data.txt

Jo

Va

Su

7. Pasting Files : paste[user31@localhost ~]$ cat f1.txt

a

b

c

d

[user31@localhost ~]$ cat f2.txt

1

2

3

4

5

6

[user31@localhost ~]$ paste f1.txt f2.txt

a 1

b 2

c 3

d 4

5

68. Adding Line Numbers: nl

[user31@localhost ~]$ nl editor

1 The vi Editor is a screen editor which is available in almost all unix systems. It is a fast and powerful editor

2 Basic Modes of operation

3 Command Mode

4 Insertion Mode

5 Last Line Mode

9. SED Filter

[user31@localhost ~]$ cat teaormilk.txt

India's milk is good

Tea Red-Label is good

Tea is better than the coffee

[user31@localhost ~]$ sed 's/good/fine/g' teaormilk.txt

India's milk is fine

Tea Red-Label is fine

Tea is better than the coffee

[user31@localhost ~]$ cat apple.txt

Core

Worm seed

Jewel

[user31@localhost ~]$ cat apple.txt | sed -e 's/e/WWW/'

CorWWW

Worm sWWWed

JWWWwel

[user31@localhost ~]$ cat animals.txt

Tiger

Monkey

Bear

Deer

[user31@localhost ~]$ cat animals.txt | sed -e 's/e/J/g'

TigJr

MonkJy

BJar

DJJr

10. AWK Filter[user31@localhost ~]$ cat fruits.txt

cherry order 4

mango good 10

apple ok 12

banana good 5

[user31@localhost ~]$

[user31@localhost ~]$ awk '/good/{print $3}' fruits.txt

10

5[user31@localhost ~]$ cat basket.txt

Layer1 = potato

Layer2 = lemon

Layer3 = radish

Layer4 = cauliflower

Layer5 = tomato

[user31@localhost ~]$ cat basket.txt | awk -F = '{print $1}'

Layer1

Layer2

Layer3

Layer4

Layer5

[user31@localhost ~]$ cat basket.txt | awk -F = '{print"Has :" $2}'

Has : potato

Has : lemon

Has : radish

Has : cauliflower

Has : tomato

11. GREP Filter[user31@localhost ~]$ cat hello.txt

Hello world

Cartoons are good

Especially toon like tom

They love us

I too

[user31@localhost ~]$ grep "too" hello.txt

Cartoons are good

Especially toon like tom

I too[user31@localhost ~]$ cat >birds.txt

pigeon

bulbul

parrot

duck

[user31@localhost ~]$ grep bulbul birds.txt

bulbulEx No 7.16(a)To print Even Numbers up to N

echo "Enter the Range"

read n

i=2

echo "The Even Numbers are"

while [ $i -le $n ]

do

echo $i

i=`expr $i + 2`

done

OUTPUT

[user31@localhost ~]$ sh ev.sh

Enter the Range

10

The Even Numbers are

2

4

6

8

10Ex No 7.16(b)To print Odd Numbers up to N

echo "Enter the Range"

read n

i=1

echo "The Even Numbers are"

while [ $i -le $n ]

do

echo $i

i=`expr $i + 2`

done

OUTPUT

[user31@localhost ~]$ sh od.sh

Enter the Range

10

The Even Numbers are

1

3

5

7

9

Ex No 7.16(c)To print Natural Numbers up to N

echo "Enter the Range"

read n

i=0

echo "The Numbers are"

while [ $i -le $n ]

do

echo $i

i=`expr $i + 1`

done

OUTPUT

[user31@localhost ~]$ sh nn.sh

Enter the Range

5

The Numbers are

0

1

2

3

4

5

Ex No 7.17Security Commands

if [$# -lt 1 ]

then

echo "invalid usage"

exit

fi

if [ -x $1 ]

then echo "$1 has executable permission"

else

echo "$1 has no executable permission"

chmod u+x $1

ls -1 $1

fi

OUTPUT

[user31@localhost ~]$ cat >p

hai

hello

[user31@localhost ~]$

[user31@localhost ~]$ sh se.sh p

p has no executable permission

p

Ex No 7.18To find whether the given year is leap or notread year

if [ `expr $year % 4 ` -eq 0 ]

then

echo "$year is a leap year"

else

echo "$year is not a leap year"

fi

OUTPUT

[user31@localhost ~]$ sh leap.sh

2010

2010 is not a leap year

[user31@localhost ~]$ sh leap.sh

2020

2020 is a leap year

Ex No 7.19Combinations of 1 2 3for i in 1 2 3

do

for j in 1 2 3

do

for k in 1 2 3

do

echo "$i $j $k"

done

done

done

OUTPUT

[user31@localhost ~]$ sh ott.sh

1 1 1

1 1 2

1 1 3

1 2 1

1 2 2

1 2 3

1 3 1

1 3 2

1 3 3

2 1 1

2 1 2

2 1 3

2 2 1

2 2 2

2 2 3

2 3 1

2 3 2

2 3 3

3 1 1

3 1 2

3 1 3

3 2 1

3 2 2

3 2 3

3 3 1

3 3 2

3 3 3

Ex No 8.1C Programs- File Handling- Reading and Writing Data in a File#include

int main()

{

FILE *fptr;

char s1[50],s2[50];

printf("\nEnter Any String:");

gets(s1);

fptr=fopen("abc.txt","w");

fputs(s1,fptr);

fclose(fptr);

fptr=fopen("abc.txt","r");

fgets(s2,50,fptr);

printf("\nThe Given String:");

puts(s2);

}

OUTPUT

[user31@localhost ~]$ cc f1.c[user31@localhost ~]$ ./a.out

Enter Any String:Hi,How ru?

The Given String:Hi,How ru?

Ex No 8.2C Programs- File Handling- Copy the Contents of one File to another File

#include

#include

main()

{

FILE *fp1,*fp2;

int c;

char fname1[40],fname2[40];

printf("Enter the source file:");

gets(fname1);

printf("Enter the target file:");

gets(fname2);

fp1=fopen(fname1,"r");

fp2=fopen(fname2,"w");

if(fp1==NULL)

{

printf("Cannot open %s for reading\n",fname1);

}

else if(fp2==NULL)

{

printf("Cannot open %s for reading\n",fname1);

}

else

{

c=getc(fp1);

while(c!=EOF)

{

putc(c,fp2);

c=getc(fp1);

}

fclose(fp1);

fclose(fp2);

printf("\n File Copied\n");

}

}OUTPUT

[user31@localhost ~]$ cc f2.c

[user31@localhost ~]$ ./a.out

Enter the source file:ss.txt

Enter the target file:ee.txt

File Copied

[user31@localhost ~]$ cat ee.txt

Hi, how ru?

Fine

Ex No 8.3C Programs- File Handling- To count Characters and lines in a file

#include

#include

#include

main()

{

FILE *fp;

int c,nc,nlines;

char filename[40];

nlines=0;

nc=0;

printf("To count the characters and lines in a given file\n");

printf("Enter the File name:");

gets(filename);

fp=fopen(filename,"r");

if(fp==NULL)

{

printf("cannot open %s for reading \n",filename);

}

c=getc(fp);

while(c!=EOF)

{

if(c=='\n')

nlines++;

else

nc++;

c=getc(fp);

}

fclose(fp);

if(nc!=0)

{

printf("There are %d characters in %s\n",nc,filename);

printf("There are %d lines\n",nlines);

}

else

printf("The file %s is empty\n",filename);

}

OUTPUT

[user31@localhost ~]$ cc f4.c[user31@localhost ~]$ cat >yy.txt

"SED" stands for stream editor.The sed acts like a UNIX filter.

It takes input from a file and send its output to standard output.

[user31@localhost ~]$ ./a.out

To count the characters and lines in a given file

Enter the File name:yy.txt

There are 129 characters in yy.txt

There are 2 lines

Ex No 9

C Programs- Signal Handling

#include

#include

void cntl_c_handler(int dummy)

{

printf("you just press ctrl c\n");

signal(SIGINT,cntl_c_handler);

}

main()

{

int i,j;

signal(SIGINT,cntl_c_handler);

for(j=0;j