Unix Commands


1) write a grep command to display all lines that begin and end with same character.

à

create f1.txt file

Akash gupta                                                           
akilesh gupta
ankita


$ grep -i '^a.*a$' f1.txt

o/p:-

Akash gupta
akilesh gupta
ankita


2) Delete all leading and traling spaces in all line of a file.

à

create file f2.txt

hi hello            how are you

 tr -d '\ |' < f2.txt


o/p:-

hihello howareyou

3)To print first three columns and first two rows of file.

à
create file f3.txt

rollno   name    result
1          akash gupta     pass
2          jwala gupta      pass
3          akilesh gupta   pass
4          ankita              fail


$ head -n 2 f3.txt|cut -f 1-3

rollno   name    result
1          akash gupta     pass



4) To print line numbers of all lines begins with ‘T’ in  a file.

à

cat > f4.txt

the sachin is a good boy
he is male


grep -ni "^T" f4.txt

o/p:

1:the sachin is a good boy


5) To delete all lines begin with ‘T’ in a file.

à
grep -vi 'T' f4.txt

he is male
6)To display all lines that contains pattern g* in a line.

à

cat >f5.txt

god is great
he is a good boy
g* hello


grep  "g\*" f5.txt

g* hello



7)Count the frequency of each word in a text file.

à

cat f5.txt | tr -d [:punct:] |tr ' ' '\n'|tr 'A-Z' 'a-z'|sort|uniq -c|sort -rn

      2 is
      1 hello
      1 he
      1 great
      1 good
      1 god
      1 g
      1 boy
      1 a

8) List all files of working directory having at last 4 characters in filename.

à

ls ????*

9) Replace multiple spaces with a space in file.

à

tr -s ' ' < f2.txt

hi hello how are you



10) Write a command to locate lines that begin and end with a dot and containing anything between them.

à

cat > f10.txt

. hi hello .
hi

grep "^[.*.$]" f1

. hi hello .


11) Write a command to display all lines that contains 2 or more  ^  symbols at beginning of a file.

à

cat > f11.txt

^^^hello
hi

grep -n ^["'^^'*"] f11.txt

1:^^^hello

12) Write a command to display all filename containing only digits in a filename.

à


ls | grep "^[0-9]"

o/p:

5KoH8dwx4jY.flv.part


13) Write a command to display lines 5 to 10,15-20, and last lines of file.

à

cat > f13.txt


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
unix
27
unix
28


$ sed -n -e '5,10p' -e '15,20p' -e '$p' f13.txt

o/p:

5
6
7
8
9
10
15
16
17
18
19
20
28

14) Write a sed command to extract first word of each line.

à

sed -n '1,$p' f5.txt | cut -d " " -f 1

god
he
g*

15) Write a command to display all line but not the last line of file.

à

$ sed -n '$!p' f5.txt

god is great
he is a good boy


16) Write a command to display filenames of working directory having first and last charater must be an alphabet.

à

ls | grep "^[a-z]"


17) Write a command to display all files having length greater than equal to 10 characters.

à

ls | grep -e '[^\]\{10,\}'


18) To display those lines between 25 to 50 having pattern ‘unix’ in it.

à

sed -n '25,50p' f13.txt | grep -n 'unix'

3:unix
5:unix





19) To select files having read and write permissions for all categories of users.

à

$ ls -l | grep -n '^-rw.rw.rw.'

20) To count number of characters on last line of file.

à

tail -1 f5.txt | wc -c
9

21) Display regular files having more than two links.

à
$ ls –l |cut –a “ “ –f |grep “2”


22) To count all lines that end with digits.

à

grep "[0-9]$" f1.txt | wc -l

0

23) Display those lines of the file in which third field contain ‘director’ or ‘chairman’.

à

$ cat > f23.txt

eid     ename    desig
1          akash   director
2          jayesh  chairman
3          rahul    manager

$ grep -e 'director' -e 'chairman' f23.txt | cut -f3

director
chairman




24) Write a grep command to display all files of current directory that contains ‘unix’ pattern in it.

à

$ grep -l 'unix' f13.txt
f13.txt


25) To extract first word of each line of file.

à

$ awk '{print $1}' f5.txt

god
he
g*












No comments:

Post a Comment