Monday, March 18, 2019


Sort Command: -
Q) Sort a file f.txt in alphabetical order.
Ans - $ sort f.txt<Enter – Key>
Q) Sort a file f.txt in reverse alphabetical order.
Ans - $ sort –r f.txt<Enter – Key>
Whereas –r = reverse
Q) Sort a file f1.dat in ascending order of number present inside the file as 1st character.
Ans - $ sort –n f1.dat<Enter – Key>
Q) Sort a file f1.dat in descending order of number present inside the file as 1st character.
Ans - $ sort –nr f1.dat<Enter – Key>
Q) Find the unique lines present inside the file file3 ?
Ans - $ sort –u file3<Enter – Key>
Q) Check whether the file file2 is already sorted or not ?
Ans - $ sort –c file2<Enter – Key>
If you get no result in above command, then the file content is present in sorted order otherwise not sorted and you will get 1st dis order row.

Uniq Command: -
Q) What is uniq command ?
Ans – Uniq Command removes duplicates and give only unique lines of a file.
Q) What is the disadvantage of Uniq Command ?
Ans – It compares only adjacent lines and that’s why sort command is used along with uniq command.
Q) Hare Krishna Hare Krishna
     Krishna Krishna Hare Hare
How Many Hare’s and Krishna’s present in the above quote ?
Ans – Create a file f1 with the above quote.
Then, $ cat f1 | tr –s ‘ ‘ ‘\n’ | sort | uniq –c<Enter – Key>
Q) How to get unique lines present inside the file f1 ?
Ans - $ sort f1 | uniq –u<Enter – Key>
Q) How to get duplicate lines present inside the file file4 ?
Ans - $ sort file4 | uniq –d<Enter – Key>

No comments:

Post a Comment

Interview Questions: - (UNIX - 0) UNIX: -   Q) How to get inode number of a file/directory? Ans – $ ls –i <File/Directory_Name...