You are on page 1of 23

1.

Write a command to print the lines that has the the pattern "july" in all the files in a particular
directory?
grep july *
This will print all the lines in all files that contain the word july along with the file name. If any of the
files contain words like "!"#" or "uly"$ the a%o&e command would not print those lines.
'. Write a command to print the lines that has the word "july" in all the files in a directory and also
suppress the file name in the output.
grep -h july *
(. Write a command to print the lines that has the word "july" while ignoring the case.
grep -i july *
The option i make the grep command to treat the pattern as case insensiti&e.
). When you use a single file as input to the grep command to search for a pattern$ it won*t print the
filename in the output. +ow write a grep command to print the file name in the output without using
the *,-* option.
grep pattern file name /dev/null
The .de&.null or null de&ice is special file that discards the data written to it. /o$ the .de&.null is
always an empty file.
0nother way to print the file name is using the *,-* option. The grep command for this is
grep -H pattern filename
1. Write a command to print the file names in a directory that does not contain the word "july"?
grep -L july *
The *,"* option makes the grep command to print the file names that do not contain the specified
pattern.
2. Write a command to print the line num%ers along with the line that has the word "july"?
grep -n july filename
The *,n* option is used to print the line num%ers in a file. The line num%ers start from 1
3. Write a command to print the lines that starts with the word "start"?
grep '^start' filename
The *4* sym%ol specifies the grep command to search for the pattern at the start of the line.
5. In the te6t file$ some lines are delimited %y colon and some are delimited %y space. Write a
command to print the third field of each line.
awk '{ if( $ ! /"/ # { $%&'"'( ) else { $% &' '( ) print $* )' filename
7. Write a command to print the line num%er %efore each line?
awk '{print +,- $)' filename
18. Write a command to print the second and third line of a file without using +9.
awk './01+ {,%&''($%&'2n') {print $3-$*)' filename
11. -ow to create an alias for the comple6 command and remo&e the alias?
The alias utility is used to create the alias for a command. The %elow command creates alias for ps
,aef command.
alias pg&'ps -aef'
If you use pg$ it will work the same way as ps ,aef.
To remo&e the alias simply use the unalias command as
unalias pg
1'. Write a command to display today*s date in the format of *yyyy,mm,dd*?
The date command can %e used to display today*s date with time
date '456-5m-5d'
9ecommended 9eading:
!ni6 Inter&iew ;uestions , <art 3
0wk =ommand in !ni6
/ed =ommand in !ni6
>rep =ommand in !ni6
?ind =ommand in !ni6
If you like this article$ then please share it or click on the google @1 %utton.
Amail This BlogThisC /hare to Twitter /hare to ?ace%ook /hare to <interest
) comments "a%els: !ni6$ !ni6 Inter&iew ;uestions
TOP UNIX INTERVIEW QUESTIONS - PART 7
1. Write a command to display your name 188 times.
The #es utility can %e used to repeatedly output a line with the specified string or *y*.
yes 7y8ur9name: ; head -<
'. Write a command to display the first 18 characters from each line of a file?
=ut -= -< filename
(. The fields in each line are delimited %y comma. Write a command to display third field from each
line of a file?
=ut -d'-' -f3 filename
). Write a command to print the fields from 18 to '8 from each line of a file?
=ut -d'-' -f<-3 filename
1. Write a command to print the first 1 fields from each line?
=ut -d'-' -f-> filename
2. By default the cut command displays the entire line if there is no delimiter in it. Which cut option is
used to suppress these kind of lines?
The ,s option is used to suppress the lines that do not contain the delimiter.
3. Write a command to replace the word "%ad" with "good" in file?
sed s/?ad/g88d/ 7 filename
5. Write a command to replace the word "%ad" with "good" glo%ally in a file?
sed s/?ad/g88d/g 7 filename
7. Write a command to replace the word "apple" with "DappleE" in a file?
sed s/apple/(@#/ 7 filename
18. Write a command to switch the two consecuti&e words "apple" and "mango" in a file?
sed 's/2(apple2# 2(mang82#/23 2</' 7 filename
11. Write a command to display the characters from 18 to '8 from each line of a file?
=ut -= <-3 filename
9ecommended 9eading:
!ni6 Inter&iew ;uestions , <art 2
0wk =ommand A6amples
/ed =ommand A6amples
>rep =ommand A6amples
?ind =ommand A6amples
If you like this article$ then please share it or click on the google @1 %utton.
Amail This BlogThisC /hare to Twitter /hare to ?ace%ook /hare to <interest
8 comments "a%els: !ni6$ !ni6 Inter&iew ;uestions
TOP UNIX INTERVIEW QUESTIONS - PART 6
1. Write a command to remo&e the prefi6 of the string ending with *.*.
The %asename utility deletes any prefi6 ending in .. The usage is mentioned %elow:
?asename /usr/l8=al/?in/file
This will display only file
'. -ow to display Fero %yte siFe files?
ls -l ; grep '^-' ; awk '/^-/ {if ($> A& # print $B )'
(. -ow to replace the second occurrence of the word "%at" with "%all" in a file?
sed 's/?at/?all/3' 7 filename
). -ow to remo&e all the occurrences of the word "jhon" e6cept the first one in a line with in the
entire file?
sed 's/jh8n//3g' 7 filename
1. -ow to replace the word "lite" with "light" from 188th line to last line in a file?
sed '<-$ s/lite/light/' 7 filename
2. -ow to list the files that are accessed 1 days ago in the current directory?
find -atime > -type f
3. -ow to list the files that were modified 1 days ago in the current directory?
find -mtime > -type f
5. -ow to list the files whose status is changed 1 days ago in the current directory?
find -=time > -type f
7. -ow to replace the character *.* with *$* in a file?
sed 's/2//-/' 7 filename
sed 's;/;-;' 7 filename
18. Write a command to find the num%er of files in a directory.
ls -l;grep '^-';w= -l
9ecommended 9eading:
!ni6 Inter&iew ;uestions , <art 1
0wk =ommand A6amples in !ni6
/ed =ommand A6amples in !ni6
>rep =ommand A6amples in !ni6
?ind =ommand A6amples in !ni6
If you like this article$ then please share it or click on the google @1 %utton.
Amail This BlogThisC /hare to Twitter /hare to ?ace%ook /hare to <interest
8 comments "a%els: !ni6$ !ni6 Inter&iew ;uestions
TOP UNIX INTERVIEW QUESTIONS - PART 5
1. -ow to display the processes that were run %y your user name ?
ps -aef ; grep 7user9name:
'. Write a command to display all the files recursi&ely with path under current directory?
find C -depth -print
(. Gisplay Fero %yte siFe files in the current directory?
find -siDe -type f
). Write a command to display the third and fifth character from each line of a file?
=ut -= *-> filename
1. Write a command to print the fields from 18th to the end of the line. The fields in the line are
delimited %y a comma?
=ut -d'-' -f<- filename
2. -ow to replace the word ">un" with "<en" in the first 188 lines of a file?
sed '<- s/0un/Een/' 7 filename
3. Write a !ni6 command to display the lines in a file that do not contain the word "90H"?
grep -v ,FG filename
The *,&* option tells the grep to print the lines that do not contain the specified pattern.
5. -ow to print the sIuares of num%ers from 1 to 18 using awk command
awk './01+ { f8r(i&<(i7&<(i44# {print 'sHuare 8f'-i-'is'-i*i())'
7. Write a command to display the files in the directory %y file siFe?
ls -l ; grep '^-' ;s8rt -nr -k >
18. -ow to find out the usage of the =<! %y the processes?
The top utility can %e used to display the =<! usage %y the processes.
9ecommended 9eading:
!ni6 Inter&iew ;uestions , <art )
0wk =ommand
/ed =ommand
>rep =ommand
?ind =ommand
If you like this article$ then please share it or click on the google @1 %utton.
Amail This BlogThisC /hare to Twitter /hare to ?ace%ook /hare to <interest
8 comments "a%els: !ni6$ !ni6 Inter&iew ;uestions
TOP UNIX INTERVIEW QUESTIONS - PART 4
1. -ow do you write the contents of ( files into a single file?
=at file< file3 file* : file
'. -ow to display the fields in a te6t file in re&erse order?
awk './01+ {I,%&'') { f8r(i&+$(i:(i--# print $i-' '( print '2n')' filename
(. Write a command to find the sum of %ytes DsiFe of fileE of all files in a directory.
ls -l ; grep '^-'; awk './01+ {sum&) {sum & sum 4 $>) /+J {print sum)'
). Write a command to print the lines which end with the word "end"?
grep 'end$' filename
The *J* sym%ol specifies the grep command to search for the pattern at the end of the line.
1. Write a command to select only those lines containing "july" as a whole word?
grep -w july filename
The *,w* option makes the grep command to search for e6act whole words. If the specified pattern is
found in a string$ then it is not considered as a whole word. ?or e6ample: In the string "mikejulymak"$
the pattern "july" is found. -owe&er "july" is not a whole word in that string.
2. -ow to remo&e the first 18 lines from a file?
sed '<-< d' 7 filename
3. Write a command to duplicate each line in a file?
sed 'p' 7 filename
5. -ow to e6tract the username from *who am i* comamnd?
wh8 am i ; =ut -f< -d' '
7. Write a command to list the files in *.usr* directory that start with *ch* and then display the num%er
of lines in each file?
w= -l /usr/=h*
0nother way is
find /usr -name '=h*' -type f -eKe= w= -l {) 2(
18. -ow to remo&e %lank lines in a file ?
grep -v L^$M filename : new9filename
9ecommended 9eading:
!ni6 Inter&iew ;uestions , <art (
0wk =ommand
/=< =ommand in !ni6
//- =ommand in !ni6
?ind =ommand
If you like this article$ then please share it or click on the google @1 %utton.
Amail This BlogThisC /hare to Twitter /hare to ?ace%ook /hare to <interest
8 comments "a%els: !ni6$ !ni6 Inter&iew ;uestions
TOP UNIX INTERVIEW QUESTIONS - PART 3
1. Gisplay all the files in current directory sorted %y siFe?
ls -l ; grep '^-' ; awk '{print $>-$B)' ;s8rt -n;awk '{print $3)'
'. Write a command to search for the file *map* in the current directory?
find -name map -type f
(. -ow to display the first 18 characters from each line of a file?
=ut -= -< filename
). Write a command to remo&e the first num%er on all lines that start with "K"?
sed '2-^N- s/O-BPO-BP*//' 7 filename
1. -ow to print the file names in a directory that has the word "term"?
grep -l term *
The *,l* option make the grep command to print only the filename without printing the content of the
file. 0s soon as the grep command finds the pattern in a file$ it prints the pattern and stops searching
other lines in the file.
2. -ow to run awk command specified in a file?
awk -f filename
3. -ow do you display the calendar for the month march in the year 1751?
The cal command can %e used to display the current month calendar. #ou can pass the month and
year as arguments to display the reIuired year$ month com%ination calendar.
=al * <BQ>
This will display the calendar for the Harch month and year 1751.
5. Write a command to find the total num%er of lines in a file?
w= -l filename
Lther ways to print the total num%er of lines are
awk './01+ {sum&) {sum&sum4<) /+J {print sum)' filename
awk '/+J{print +,)' filename
7. -ow to duplicate empty lines in a file?
sed '/^$/ p' 7 filename
18. A6plain iostat$ &mstat and netstat?
Iostat: reports on terminal$ disk and tape I.L acti&ity.
Mmstat: reports on &irtual memory statistics for processes$ disk$ tape and =<! acti&ity.
+etstat: reports on the contents of network data structures.
9ecommended 9eading:
!ni6 Inter&iew ;uestions , <art '
=ronta% =ommand in !ni6
/=< =ommand in !ni6
//- =ommand in !ni6
?ind =ommand
If you like this article$ then please share it or click on the google @1 %utton.
Amail This BlogThisC /hare to Twitter /hare to ?ace%ook /hare to <interest
8 comments "a%els: !ni6$ !ni6 Inter&iew ;uestions
TOP UNIX INTERVIEW QUESTIONS - PART 2
1. -ow do you rename the files in a directory with Nnew as suffi6?
ls -lrt;grep '^-'; awk '{print 'mv '$B' '$B'Cnew')' ; sh
'. Write a command to con&ert a string from lower case to upper case?
e=h8 'apple' ; tr Oa-DP OF-RP
(. Write a command to con&ert a string to Initcap.
e=h8 apple ; awk '{print t8upper(su?str($<-<-<## t8l8wer(su?str($<-3##)'
). Write a command to redirect the output of date command to multiple files?
The tee command writes the output to multiple files and also displays the output on the terminal.
date ; tee -a file< file3 file*
1. -ow do you list the hidden files in current directory?
ls -a ; grep '^2C'
2. "ist out some of the -ot Oeys a&aila%le in %ash shell?
=trl@l , =lears the /creen.
=trl@r , Goes a search in pre&iously gi&en commands in shell.
=trl@u , =lears the typing %efore the hotkey.
=trl@a , <laces cursor at the %eginning of the command at shell.
=trl@e , <laces cursor at the end of the command at shell.
=trl@d , Oills the shell.
=trl@F , <laces the currently running process into %ackground.
3. -ow do you make an e6isting file empty?
=at /dev/null : filename
5. -ow do you remo&e the first num%er on 18th line in file?
sed '< s/O-BPO-BP*//' 7 filename
7. What is the difference %etween join ,& and join ,a?
j8in -v " 8utputs 8nly mat=hed lines ?etween tw8 filesC
j8in -a " 1n additi8n t8 the mat=hed lines- this will 8utput unmat=hed lines
als8C
18. -ow do you display from the 1th character to the end of the line from a file?
=ut -= >- filename
9ecommended 9eading:
!ni6 Inter&iew ;uestions , <art 1
=ron jo%s in !ni6
/=< =ommand A6amples in !ni6
//- =ommand A6amples in !ni6
?ind =ommand A6amples
If you like this article$ then please share it or click on the google @1 %utton.
Amail This BlogThisC /hare to Twitter /hare to ?ace%ook /hare to <interest
) comments "a%els: !ni6$ !ni6 Inter&iew ;uestions
TOP UNIX INTERVIEW QUESTIONS - PART 1
1. -ow to display the 18th line of a file?
head -< filename ; tail -<
'. -ow to remo&e the header from a file?
sed -i '< d' filename
(. -ow to remo&e the footer from a file?
sed -i '$ d' filename
). Write a command to find the length of a line in a file?
The %elow command can %e used to get a line from a file.
sed Sn '7n: p' filename
We will see how to find the length of 18th line in a file
sed -n '< p' filename;w= -=
1. -ow to get the nth word of a line in !ni6?
=ut Sf7n: -d' '
2. -ow to re&erse a string in uni6?
e=h8 'java' ; rev
3. -ow to get the last word from a line in !ni6 file?
e=h8 'uniK is g88d' ; rev ; =ut -f< -d' ' ; rev
5. -ow to replace the n,th line in a file with a new line in !ni6?
sed -i'' '< d' filename T d stands f8r delete
sed -i'' '< i new inserted line' filename T i stands f8r insert
7. -ow to check if the last command was successful in !ni6?
e=h8 $U
18. Write command to list all the links from a directory?
ls -lrt ; grep '^l'
11. -ow will you find which operating system your system is running on in !+IP?
uname -a
1'. =reate a read,only file in your home directory?
t8u=h file( =hm8d V file
1(. -ow do you see command line history in !+IP?
The *history* command can %e used to get the list of commands that we are e6ecuted.
1). -ow to display the first '8 lines of a file?
By default$ the head command displays the first 18 lines from a file. If we change the option of head$
then we can display as many lines as we want.
head -3 filename
0n alternati&e solution is using the sed command
sed '3<-$ d' filename
The d option here deletes the lines from '1 to the end of the file
11. Write a command to print the last line of a file?
The tail command can %e used to display the last lines from a file.
tail -< filename
0lternati&e solutions are:
sed -n '$ p' filename
awk '/+J{print $)' filename
9ecommended 9eading:
!ni6 Inter&iew ;uestions , <art 5
=ron jo%s in !ni6
/=< =ommand A6amples in !ni6
//- =ommand A6amples in !ni6
?ind =ommand A6amples
If you like this article$ then please share it or click on the google @1 %utton.
Amail This BlogThisC /hare to Twitter /hare to ?ace%ook /hare to <interest
8 comments "a%els: !ni6$ !ni6 Inter&iew ;uestions
UNIX INTERVIEW QUESTIONS ON FIND COMMAND
?ind utility is used for searching files using the directory information.
1. Write a command to search for the file *test* in the current directory?
find ,name test ,type f
'. Write a command to search for the file *temp* in *.usr* directory?
find .usr ,name temp ,type f
(. Write a command to search for Fero %yte siFe files in the current directory?
find ,siFe 8 ,type f
). Write a command to list the files that are accessed 1 days ago in the current directory?
find ,atime 1 ,type f
1. Write a command to list the files that were modified 1 days ago in the current directory?
find ,mtime 1 ,type f
2. Write a command to search for the files in the current directory which are not owned %y any user
in the .etc.passwd file?
find . ,nouser ,type f
3. Write a command to search for the files in *.usr* directory that start with *te*?
find .usr ,name *teQ* ,type f
5. Write a command to search for the files that start with *te* in the current directory and then display
the contents of the file?
find . ,name *teQ* ,type f ,e6ec cat RS TU
7. Write a command to list the files whose status is changed 1 days ago in the current directory?
find ,ctime 1 ,type f
18. Write a command to list the files in *.usr* directory that start with *ch* and then display the num%er
of lines in each file?
find .usr ,name *chQ* ,type f ,e6ec wc ,l RS TU
9ecommended 9eading:
0wk =ommand in !ni6
=ron jo%s in !ni6
/=< =ommand A6amples in !ni6
//- =ommand A6amples in !ni6
?ind =ommand A6amples
If you like this article$ then please share it or click on the google @1 %utton.
Amail This BlogThisC /hare to Twitter /hare to ?ace%ook /hare to <interest
8 comments "a%els: !ni6$ !ni6 Inter&iew ;uestions
UNIX INTERVIEW QUESTIONS ON CUT COMMAND
The cut command is used to used to display selected columns or fields from each line of a file. =ut
command works in two modes:
Gelimited selection: The fields in the line are delimited %y a single character like
%lank$comma etc.
9ange selection: Aach field starts with certain fi6ed offset defined as range.
1. Write a command to display the third and fourth character from each line of a file?
cut ,c ($) filename
'. Write a command to display the characters from 18 to '8 from each line of a file?
cut ,c 18,'8 filename
(. Write a command to display the first 18 characters from each line of a file?
cut ,c ,18 filename
). Write a comamnd to display from the 18th character to the end of the line?
cut ,c 18, filename
1. The fields in each line are delimited %y comma. Write a command to display third field from each
line of a file?
cut ,d*$* ,f' filename
2. Write a command to print the fields from 18 to '8 from each line of a file?
cut ,d*$* ,f18,'8 filename
3. Write a command to print the first 1 fields from each line?
cut ,d*$* ,f,1 filename
5. Write a command to print the fields from 18th to the end of the line?
cut ,d*$* ,f18, filename
7. By default the cut command displays the entire line if there is no delimiter in it. Which cut option is
used to supress these kind of lines?
The ,s option is used to supress the lines that do not contain the delimiter.
18. Write a cut command to e6tract the username from *who am i* comamnd?
who am i V cut ,f1 ,d* *
9ecommended 9eading:
0wk =ommand in !ni6
=ron jo%s in !ni6
/=< =ommand A6amples in !ni6
//- =ommand A6amples in !ni6
?ind =ommand A6amples
If you like this article$ then please share it or click on the google @1 %utton.
Amail This BlogThisC /hare to Twitter /hare to ?ace%ook /hare to <interest
8 comments "a%els: !ni6$ !ni6 Inter&iew ;uestions
UNIX INTERVIEW QUESTIONS ON SED COMMAND
/AG is a special editor used for modifying files automatically.
1. Write a command to replace the word "%ad" with "good" in file?
sed s.%ad.good. W filename
'. Write a command to replace the word "%ad" with "good" glo%ally in a file?
sed s.%ad.good.g W filename
(. Write a command to replace the character *.* with *$* in a file?
sed *s.T..$.* W filename
sed *sV.V$V* W filename
). Write a command to replace the word "apple" with "DappleE" in a file?
sed s.apple.DXE. W filename
1. Write a command to switch the two consecuti&e words "apple" and "mango" in a file?
sed *s.TDappleTE TDmangoTE.T' T1.* W filename
2. Write a command to replace the second occurrence of the word "%at" with "%all" in a file?
sed *s.%at.%all.'* W filename
3. Write a command to remo&e all the occurrences of the word "jhon" e6cept the first one in a line
with in the entire file?
sed *s.jhon..'g* W filename
5. Write a command to remo&e the first num%er on line 1 in file?
sed *1 s.Y8,7ZY8,7ZQ..* W filename
7. Write a command to remo&e the first num%er on all lines that start with "K"?
sed *T$4K$ s.Y8,7ZY8,7ZQ..* W filename
18. Write a command to replace the word "gum" with "drum" in the first 188 lines of a file?
sed *1$88 s.gum.drum.* W filename
11. write a command to replace the word "lite" with "light" from 188th line to last line in a file?
sed *188$J s.lite.light.* W filename
1'. Write a command to remo&e the first 18 lines from a file?
sed *1$18 d* W filename
1(. Write a command to duplicate each line in a file?
sed *p* W filename
1). Write a command to duplicate empty lines in a file?
sed *.4J. p* W filename
11. Write a sed command to print the lines that do not contain the word "run"?
sed ,n *.run.Cp* W filename
9ecommended 9eading:
0wk =ommand in !ni6
=ron jo%s in !ni6
/=< =ommand A6amples in !ni6
//- =ommand A6amples in !ni6
?ind =ommand A6amples
If you like this article$ then please share it or click on the google @1 %utton.
Amail This BlogThisC /hare to Twitter /hare to ?ace%ook /hare to <interest
8 comments "a%els: !ni6$ !ni6 Inter&iew ;uestions
UNIX INTERVIEW QUESTIONS ON GREP COMMAND
The grep is one of the powerful tools in uni6. >rep stands for "glo%al search for regular e6pressions
and print". The power of grep lies in using regular e6pressions mostly.
The general synta6 of grep command is
grep YoptionsZ pattern YfilesZ
1. Write a command to print the lines that has the the pattern "july" in all the files in a particular
directory?
grep july Q
This will print all the lines in all files that contain the word july along with the file name. If any of the
files contain words like "!"#" or "uly"$ the a%o&e command would not print those lines.
'. Write a command to print the lines that has the word "july" in all the files in a directory and also
suppress the filename in the output.
grep ,h july Q
(. Write a command to print the lines that has the word "july" while ignoring the case.
grep ,i july Q
The option i make the grep command to treat the pattern as case insensiti&e.
). When you use a single file as input to the grep command to search for a pattern$ it won*t print the
filename in the output. +ow write a grep command to print the filename in the output without using
the *,-* option.
grep pattern filename .de&.null
The .de&.null or null de&ice is special file that discards the data written to it. /o$ the .de&.null is
always an empty file.
0nother way to print the filename is using the *,-* option. The grep command for this is
grep ,- pattern filename
1. Write a !ni6 command to display the lines in a file that do not contain the word "july"?
grep ,& july filename
The *,&* option tells the grep to print the lines that do not contain the specified pattern.
2. Write a command to print the file names in a directory that has the word "july"?
grep ,l july Q
The *,l* option make the grep command to print only the filename without printing the content of the
file. 0s soon as the grep command finds the pattern in a file$ it prints the pattern and stops searching
other lines in the file.
3. Write a command to print the file names in a directory that does not contain the word "july"?
grep ," july Q
The *,"* option makes the grep command to print the filenames that do not contain the specified
pattern.
5. Write a command to print the line num%ers along with the line that has the word "july"?
grep ,n july filename
The *,n* option is used to print the line num%ers in a file. The line num%ers start from 1
7. Write a command to print the lines that starts with the word "start"?
grep *4start* filename
The *4* sym%ol specifies the grep command to search for the pattern at the start of the line.
18. Write a command to print the lines which end with the word "end"?
grep *endJ* filename
The *J* sym%ol specifies the grep command to search for the pattern at the end of the line.
11. Write a command to select only those lines containing "july" as a whole word?
grep ,w july filename
The *,w* option makes the grep command to search for e6act whole words. If the specified pattern is
found in a string$ then it is not considered as a whole word. ?or e6ample: In the string
"mikejulymak"$ the pattern "july" is found. -owe&er "july" is not a whole word in that string.
9ecommended 9eading:
0wk =ommand in !ni6
=ron jo%s in !ni6
/=< =ommand A6amples in !ni6
//- =ommand A6amples in !ni6
?ind =ommand A6amples
If you like this article$ then please share it or click on the google @1 %utton.
Amail This BlogThisC /hare to Twitter /hare to ?ace%ook /hare to <interest
8 comments "a%els: !ni6$ !ni6 Inter&iew ;uestions
UNIX INTERVIEW QUESTIONS ON AWK COMMAND
0wk is powerful tool in !ni6. 0wk is an e6cellent tool for processing the files which ha&e data
arranged in rows and columns format. It is a good filter and report writer.
1. -ow to run awk command specified in a file?
awk ,f filename
'. Write a command to print the sIuares of num%ers from 1 to 18 using awk command
awk *BA>I+ R forDi[1UiW[18Ui@@E Rprint "sIuare of"$i$"is"$iQiUSS*
(. Write a command to find the sum of %ytes DsiFe of fileE of all files in a directory.
ls ,l V awk *BA>I+ Rsum[8S Rsum [ sum @ J1S A+G Rprint sumS*
). In the te6t file$ some lines are delimited %y colon and some are delimited %y space. Write a
command to print the third field of each line.
awk *R ifD J8 \ .:. E R ?/[":"U S else R ?/ [" "U S print J( S* filename
1. Write a command to print the line num%er %efore each line?
awk *Rprint +9$ J8S* filename
2. Write a command to print the second and third line of a file without using +9.
awk *BA>I+ R9/[""U?/["Tn"S Rprint J'$J(S* filename
3. Write a command to print Fero %yte siFe files?
ls ,l V awk *.4,. Rif DJ1 C[8 E print J7 S*
5. Write a command to rename the files in a directory with "Nnew" as postfi6?
ls ,? V awk *Rprint "m& "J1" "J1".new"S* V sh
7. Write a command to print the fields in a te6t file in re&erse order?
awk *BA>I+ RL9/[""S R forDi[+?Ui]8Ui,,E print Ji$" "U print "Tn"S* filename
18. Write a command to find the total num%er of lines in a file without using +9
awk *BA>I+ Rsum[8S Rsum[sum@1S A+G Rprint sumS* filename
0nother way to print the num%er of lines is %y using the +9. The command is
awk *A+GRprint +9S* filename

You might also like