Advertisements
Home > Interview Q, Linux > Basic UNIX/Linux commands for Interview Questions – Part 2

Basic UNIX/Linux commands for Interview Questions – Part 2


Some basic UNIX/Linux commands (Part-2): [Part-1]
… from college notes !
 

Q36: Display record length of the 1st line of file emp.1st.

Ans: [student@localhost student]$ head -1 emp1.1st |wc –c
41
 

Q37: Display record length of the 1st line of file emp.1st & store result in a variable.

Ans: [student@localhost student]$ p=`head -1 emp1.1st|wc -c`
[student@localhost student]$ echo “$p”
41
 

Q38: Display line no 5 & 7 from emp2.1st

Ans: [student@localhost student]$ head -5 emp2.1st > pp
[student@localhost student]$ tail -1 pp >pp1
[student@localhost student]$ tail -1 emp2.1st >>pp1
[student@localhost student]$ cat pp1
4290|jayant choudhary|executive|production|07/09/50|6000
3564|sudhir agarwal|execute|personnel|06/07/47|7500
 

Q39: Get the year of joining from file emp2.1st of all the emp.

Ans: [student@localhost student]$ cut –d “|” -f 5.7, 5.8 emp2.1st
50
47
58
62
50
55
 

Q40: Create a file shortlist by starting 1st 5 lines of emp1.1st.

Ans: [student@localhost student]$ head -5 emp1.1st | tee shortlist
9876|barun sengupta|director|production|12/03/50|7000
2365|jai sharma|director|personnel|11/05/47|7800
5423|n.k. gupta|chairman|admin|30/08/56|5400
6213|karuna ganguly|g.m|accounts|05/06/62/6300\
4290|jayant choudhary|executive|production|07/09/50|60007
 

Q41: Extract field numbers 2 & 3 from shortlist save as cut1.

Ans: [student@localhost student]$ cut -d”|” -f 2,3 shortlist
barun sengupta|director
jai sharma|director
n.k. gupta|chairman
karuna ganguly|g.m
jayant choudhary|executivem
 

Q42: Extract fields 1,4,5 from shortlist save as cut2.

Ans: [student@localhost student]$ cut -d”|” -f 1,4,5 shortlist >cut2
[student@localhost student]$ cat cut2
9876|production|12/03/50
2365|personnel|11/05/47
5423|admin|30/08/56
6213|accounts|05/06/62/6300\
4290|production|07/09/50
 

Q43: Put all fields of cut1 & cut2 together.

Ans: [student@localhost student]$ paste -d”|” cut1 cut2
a.k. shukla|g.m|9876|production|12/03/50
sumit chakrabarty|d.g.m |2365|personnel|11/05/47
chanchal singhvi | director|5423|admin|30/08/56
s.n dasgupta|manager|6213|accounts|05/06/62/6300\
anil aggarwal|manager|4290|production|07/09/50
 

Q44: Sort on 2nd field of shortlist in two ways.

Ans:
a) [student@localhost student]$ sort -t”|” +1 shortlist
9876|barun sengupta|director|production|12/03/50|7000
2365|jai sharma|director|personnel|11/05/47|7800
4290|jayant choudhary|executive|production|07/09/50|6000
6213|karuna ganguly|g.m|accounts|05/06/62/6300\
5423|n.k. gupta|chairman|admin|30/08/56|5400

b) [student@localhost student]$ sort -t”|” -k2 shortlist
9876|barun sengupta|director|production|12/03/50|7000
2365|jai sharma|director|personnel|11/05/47|7800
4290|jayant choudhary|executive|production|07/09/50|6000
6213|karuna ganguly|g.m|accounts|05/06/62/6300\
 

Q45: Create a sorted output file named sortlist.

Ans: [student@localhost student]$ sort -o sortlist shortlist
[student@localhost student]$ cat shortlist
9876|barun sengupta|director|production|12/03/50|7000
2365|jai sharma|director|personnel|11/05/47|7800
5423|n.k. gupta|chairman|admin|30/08/56|5400
6213|karuna ganguly|g.m|accounts|05/06/62/6300\
4290|jayant choudhary|executive|production|07/09/50|6000
 

Q46: Sort on 3rd field & then by 2nd field of shortlist.

Ans: [student@localhost student]$ sort -t”|” -k 5.7,5.8 shortlist
2365|jai sharma|director|personnel|11/05/47|7800
4290|jayant choudhary|executive|production|07/09/50|6000
9876|barun sengupta|director|production|12/03/50|7000
5423|n.k. gupta|chairman|admin|30/08/56|5400
6213|karuna ganguly|g.m|accounts|05/06/62/6300\
 

Q47: Sort according to year of birth in shortlist.

Ans: [student@localhost student]$ sort -t”|” -k 5.7,5.8 shortlist
2365|jai sharma|director|personnel|11/05/47|7800
4290|jayant choudhary|executive|production|07/09/50|6000
9876|barun sengupta|director|production|12/03/50|7000
5423|n.k. gupta|chairman|admin|30/08/56|5400
6213|karuna ganguly|g.m|accounts|05/06/62/6300\r of birth in shortlist.
 

Q48: Create a file dept.1st.

Ans: [student@localhost student]$ cat >dept.1st
 

Q49: Convert ‘|’ to ‘~’ in emp1.1st.

Ans: [student@localhost student]$ tr ‘|’ ‘~’ <emp1.1st
2233~a.k. shukla~g.m~sales~12/52/52~6000
5678~sumit chakrabarty~d.g.m ~marketing~19/04/43~6000
1006~chanchal singhvi ~ director~sales~03/09/38~6700
1265~s.n dasgupta~manager~sales~12/09/63~5600
2476~anil aggarwal~manager~sales~01/05/59~5000
6521~lalit chowdary~director~marketing~26/09/45~8200
2345~j.b.saxena~g.m~marketing~12/03/45~8000
0110~v.k.agarwal~g.m~marketing~31/12/40~9000
 

Q50: Display frequency of occurance of all the lines.

Ans: [student@localhost student]$ uniq –c
 

Q51: Display lines beginning with 2.

Ans: [student@localhost student]$ grep "^2" emp1.1st
2233|a.k. shukla|g.m|sales|12/52/52|6000
2476|anil aggarwal|manager|sales|01/05/59|5000
2345|j.b.saxena|g.m|marketing|12/03/45|8000
 

Q52: Display lines where salary lie between 7000 & 7999.

Ans: [student@localhost student]$ grep “7…$” emp1.1st
6521|lalit chowdury |director |marketing|26/09/45|8200
2345|j.b.saxena |g.m. |marketing|12/03/45|8000
0110|v.k.agarwal |g.m. |marketing|31/12/40|9000
 

Q53: Remove consecutive occurrences of spaces in emp1.1st.

Ans: [student@localhost student]$ tr –s ‘ ‘ <emp.1st
2233|a.k.shukla|g.m.|sales|12/12/52|6000
5678|sumit chakrobarty|d.g.m|marketing|19/04/43|6000
1006|chanchal singhvi|director|sales|03/09/38|6700
1265|s.n.dasgupta|manager|sales|12/09/68|5600
2476|anil aggarwal|manager|sales|01/05/59|5000
6521|lalit chowdury|director|marketing|26/09/45|8200
2345|j.b.saxena|g.m.|marketing|12/03/45|8000
0110|v.k.agarwal|g.m.|marketing|31/12/40|9000
 

Q54: Convert the contents of emp1.1st to uppercase.

Ans: [student@localhost student]$ sort –f emp1.1st
2233|A.K.SHUKLA |G.M. |SALES |12/12/52|6000
5678|SUMIT CHAKROBARTY |D.G.M |MARKETING|19/04/43|6000
1006|CHANCHAL SINGHVI |DIRECTOR |SALES |03/09/38|6700
1265|S.N.DASGUPTA |MANAGER |SALES |12/09/68|5600
2476|ANIL AGGARWAL |MANAGER |SALES |01/05/59|5000
6521|LALIT CHOWDURY |DIRECTO |MARKETING|26/09/45|8200
2345|J.B.SAXENA |G.M. |MARKETING|12/03/45|8000
0110|V.K.AGARWAL |G.M. |MARKETING|31/12/40|9000
 

Q55: Display lines containing jai sharma with a variable.

Ans: [student@localhost student]$ a=grep ‘jai sharma’ emp.1st
0
 

Q56: Display lines containing jai sharma without a variable.

Ans: [student@localhost student]$ grep ‘jai sharma’ emp.1st
 

Q57: Display lines containing ‘Agarwal’ , ‘agarwal’,& ‘aggarwal’ from emp.1st.

Ans: [student@localhost student]$ grep –e “Agarwal” -e “agarwal” –e
“aggarwal”
2476|anil aggarwal |manager |sales |01/05/59|5000
0110|v.k.agarwal |g.m. |marketing|31/12/40|9000
 

Q58: Display one copy of redundant records of file dept.1st.

Ans: [student@localhost student]$ uniq –d dept.1st
01|accounts |6213
02|admin |5423
03|marketing |6521
04|personnel |2365
05|production|9876
06|sales |1006
 


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: