Cracking IT Interview



wc command:

Counting Lines, Words & Characters:


It displays the number of lines, words, and characters contained in given input file.  A "line" is defined as a string of characters delimited by a newline character (\n) while pressing enter for line change. A "word" is defined as a string of characters delimited by white space characters.






Syntax:

wc [option] filename


Example:

$cat language (sample file)
C|primary|4|3
C++|primary|3|3
JAVA|secondary|2|2
SQL|secondary|3|2
UNIX|primary|4|4
$
$wc language
       5       5      84 language
$


Here, it gives output as number of lines=5, number of words=5, number of bytes/ character=84



Options to use:


  • wc -L - The number of characters in the longest input line is written to screen


  • wc -l  -count the total number of lines in file and written in screen 


  • wc -w  - count total number of words and printed on screen


  • wc -c   - count total number of characters

            


Examples:


$wc -L language
      18 language
$

$cat file3.txt(sample file)
ls
cd
$wc -l file3.txt
       2 file3.txt
$

$wc -w file3.txt
       2 file3.txt
$

$wc -c file3.txt
      18 file3.txt 
$


it must print 4, but it is printing 18 so there must be hidden characters like spaces or tabs. check it by giving "cat -e filename" , $ sign will appear at the end of line


$cat -e file3.txt
ls      $
cd                                      $
$

$wc -L file3.txt
      13 file3.txt
$







tr command : Translate Characters


"tr" command is used to translate characters in a file. It just replace the existing characters with the given new characters. It is only for display purpose and doesn't make any change in file until you use $$ to redirect the change into the file. 


Syntax:

tr 'string1' 'string2' < filename


Example:

$cat file3.txt (sample file)
ls
cd
$tr 'l' 's' < file3.txt
ss
cd
$cat file3.txt
(no change in original file)
ls
cd
$

$tr '\t' '*' < file3.txt(translating tab space into visible * symbol)
ls*
cd       ****
$
$tr ' ' '-' < file3.txt
(trnaslating blank space into visible - symbol)
ls
cd-------
$

$tr '[a-z]' '[A-Z]' < file3.txt
LS
CD
$


Options to use:


  • -C  - complement the selection, Complement the set of characters in string1, e.g :  "-C ab" includes every character except for `a' and `b'


  • -c  - Same as "-C" but complement the set of values in string1


  • -s   - Suppress, Squeeze multiple occurrences of the characters listed in the last string in the input into a single instance of the character 


  • -d   - delete all the occurrence of given string 



Example:


$cat file4.txt  (sample file)
lssssssssss
ccccddddddd

aaaeeeehhhiiiiiiooooouuuuuu
mmkkdddiiirrruuuueeeeee

$
$tr -c '[a,e,i,o,u]' '*' < file4.txt
*************************aaaeeee***iiiiiiooooouuuuuu********iii***uuuueeeeee*

$
$tr -C '[a,e,i,o,u]' '*' < file4.txt
*************************aaaeeee***iiiiiiooooouuuuuu********iii***uuuueeeeee*$
$
$tr -s 'a' < file4.txt
lssssssssss
ccccddddddd

aeeeehhhiiiiiiooooouuuuuu
mmkkdddiiirrruuuueeeeee
$tr -s '[a-z]' < file4.txt
ls
cd

aehiou
mkdirue
$tr -d '[a-z]' < file4.txt





$tr -d '[a,e,i,o,u]' < file4.txt
lssssssssss
ccccddddddd

hhh
mmkkdddrrr
$


NEXT ->