Wednesday, November 16, 2011

Power Tools


 Few of tools which are very important for day to day use.

  - Head
  - Tail
  - Grep
  - cut
  - wc
  - sort
  - diff
  - sdiff
  - aspell
  - tr
  - sed
  - awk
  
Head
 
- output the first part of files.Head without a number prints ten lines by default.
                                               
Syntax:  head [OPTION]... [FILE]...
                                               
examples:
1. First ten lines of a file.
                                                                                               
head -n 10 file1.txt                                                                                                    
cat file1.txt|head -n 10
                                                                                                                       
2. First ten characters of a file.
                                                     
head -c 10 file1.txt
cat file1.txt|head -c 10
                                                                                               
                                                 
 Tail
 
-  output the last part of files.Tail without a number prints ten lines by default.
                                   
Syntax: tail [OPTION]... [FILE]...
                                               
examples:
1. First ten lines of a file.
                                                                                                                                   
                                                                                                                                           tail -n 10 file1.txt
cat file1.txt|tail -n 10

2. First ten character of a file.
                                                                                                                                   
                                                                                                                                                tail -c 10 file1.txt
                                                                                                                                                cat file1.txt|tail -c 10
                                                                                                                                                                                   
3.For a output appended data as the file grows.
                                                                                                                                   
                                                                                                                                                tail -f file1.txt
 
  Grep

- print lines matching a pattern.
                                               
Syntax:
grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]
                                                                                                             
examples:
                                                                                     
1. To get a word from a file.
                                                                                                                                               
cat file1.txt|grep  "ost"
grep     "ost" file1.txt
                                                                                                                                                           
2. To get a word irrespective of case.
                                                                                                                                   
cat file1.txt|grep -i "ost"
grep "ost" file1.txt
                                                                                                                                                                                               
3.To select only those lines containing matches that form whole words.
                                                                                                                                               
cat file1.txt|grep -w "ost"
grep -w "ost" file1.txt
                                                                                                                                   
4.Print a count of matching lines for each input file.
                                                                                                                                   
cat file1.txt|grep -c "ost"
grep -c "ost" file1.txt
                                                                                                                                               


Few more options to work on:
                                                                                                           
- -A, --after-context=NUM   print NUM lines of trailing context.
- -B, --before-context=NUM  print NUM lines of leading context.
- -C, --context=NUM         print NUM lines of output context.
 - -x, --line-regexp         force PATTERN to match only whole lines.
- -V, --version             print version information and exit
                                                                                               




 Cut

- cut - remove sections from each line of files
                                                           
Syntax: cut [OPTION]... [FILE]...
                                                           
examples:
                                                           
1. To get first 10 characters of a line.
                                                                                                                                               
cut -c1-10 file1.txt
cat file1.txt|cut -c1-10
                                                                                                                                                                       
 2. Suppose the fields are seperated by delimiter.
                                                                                                                                                   
To take the first field in the /etc/passwd file
                                                                                                                                                    
                                                                                                                                                     cat /etc/passwd|cut -f 1 -d :                  
                                                                                                                                                    
                                                                                                                                                     This will extract the first field with delimiter as ":"
                                                                                                                                                    
                                                                                                                                                    
                                                                                                                                                    
 wc

- wc - print the number of bytes, words, and lines in files.
                                                                       
Syntax: wc [OPTION]... [FILE]...
                                   
examples:
                                                           
1. To get the byte count
                                                                                                                                               

wc -c file1.txt
                                                                                                                                                                                   
                                                                                                                                               
2. To print the character count.
                                                                                                                                               
                                                                                                                                                                                    wc -m file1.txt
                                                                                                                                                                                   
3. To print the newline counts.
                                                                                                                                               
                                                                                                                                                                                    wc -l file1.txt
                                                                                                                                                                                   
4. To print the length of the longest line.
                                                                                                                                                           
                                    wc -L file1.txt
                                                                                                                                                                                   
 Sort

- sort - sort lines of text files.
                                   
1.  To sort according to dictionary order.
                                                                                                                                   
sort -d file1.txt
                                                                                                                                                                                   
2. To sort ignoring the case.
                                                                                                                                   
sort -f file1.txt
                                                                                                                                                                                   
  3. To sort according to months.
                                                                                                                                   
sort -M file1.txt
                                                                                                                                                                                   
4. To sort numerically.
                                                                                                                                   
sort -n file1.txt
                                                                                                                                                                                   
5. To reverse sort.
                                                                                                                                   
sort -r file1.txt
                                                                                                                                   

                                                                                                                                   
 aspell                                                                                                             
                                                                                                                                                                                   
                                               
- aspell - For spell check
                                               
1. To interactivly check the spellings.
                                                                                                                                               
                                                                                                                                                aspell check file1.txt
                                                                                                                                                                       
                                                                                                                                                aspell check < file2.txt
                                                                                                                                                                       
tr

- tr - Used for changing the the case or translating.
                                               
tr '[A-Z]' '[a-z]' < file1.txt
                                                                                                                                   
This will change all the characters in the file "file1.txt" to capital letters.
                                                                                                                                   
sed

-sed - stream editior.
                                               
1. change words in a file.
                                                                                                           
                                                                                                                                                sed 's/ost/OST/g' file1.txt
                                                                                                                                                sed '1,10s/ost/OST/g' file1.txt
                                                                                                                                                sed '/ost/,/OST/s/class/tutor/g' file1.txt
                                                                                                                                               
awk

- awk - editor
                                   
1.Print the first field of the  file.
                                                                                                             
                                                                                                                                                cat file|awk '{print $1}'
                                                                                                                                               
2.Print the first fields of a passwd file.
                                                                                                             
cat /etc/passwd|awk -F: '{print $1}'

No comments:

Post a Comment