Friday, 8 January 2016

Unix Interview Questions

1. Delete All Spaces or tabs from a line
:- cat file.txt tr -d ' \t'
:- sed 's/[ \t]*$//' file_A > new_file_A
2. Delete consequtive Spaces from a line
:- cat file.txt tr -s ' \t'
3. Remove blank line from file
:- sed '/^$/d' file.txt
:- grep -v '^$' filename > newfilename
4. Search for a pattern in the context of multiple xml files

:- grep -lR 'pattern' * |egrep '(xml)$'
5. Print the fields from the 10th to the 12th in a file
:- cut -d',' -f10-f12 filename.csv

6. Print the line number with the line containing a pattern in a file

:- grep -nf pattern.txt file.txt
7. Searching for repetating lines in a file [- is used after -f to read data from stream]

:-  head -1  Crane4C1.dat| grep -nf -  Crane4C1.dat

8. Remove CTRL+M from a file

:- sed -e "s/^M//"

9. How to find CTRL+M from a file

:-  cat -v filename.txt

10. Print 10 to 20th line in a file
:- sed -n 10,20p filename.txt

11. Looping in AWK

12. Create duplicate record from a file using awk.



13. Show content of two tag in a xml file using sed.

sed -n '/<Tag>/,/<\/Tag>/p' file.xml

14. Find the counts of all the records of files in a directory.

wc -l `find /path/to/directory/*PATTERN* -type f`
 
15. Remove duplicate record from a file using sed.
 
sort file | uniq -d 
  

No comments:

Post a Comment