Wednesday, July 17, 2013

UniX Exercise 1



  1. Write a command to print the ouput “Today is Tuesday, 5th day of month, year”.
Sol.            date +”Today is %a, %d of %b, %Y”

  1. Assume that files in a directory are “filename_dd_mm_yy”, write a command to list the files created today.
Sol.            ls *_`date +”%d_%m_%y”`

  1. Write a command to copy a file x to y using cat.
Sol.            cat x > y

  1. Write a command to copy the contents of 2 files into a third file.
Sol.            cat x y > z

  1. Write a command to copy the contents of x, user input, y into a file.
Sol.            cat x - y > z

  1. Write a command to copy the contents of x and y prefixing the file name before starting file contents.
Sol.            ls x > z; cat x >> z; ls y >> z; cat y >> z

                                          (Or)

                  (ls x; cat x; ls y; cat  y) > z

  1. Write a command to generate the following into a file
Files of the directory on

           

Total files in the directory are
Sol.            (echo –e “Files of the directory `pwd` on `date +%D`\n\n”;
 ls -l;echo –e “\n\n Total files in the directory are `ls –l | wc –l`”) > z

  1. Assume package name of each java program is in line 1, write a command to display all package names.
Sol.            head -1 *.java

  1. Write a command to display top 3 files which were created recently.
Sol.            ls -ltr | head -4 | tail -3

                                          (Or)

ls -ltr | tail +2 | head -3

  1. Write a command to display lines 23 to 44 of file y.
Sol.            head -44 y | tail +23
  1. Write a command to count number of words in last but one line of file y.
Sol.            tail -2 y | head -1 | wc -w

  1. Write a command to display 15th line of every file in the current dir.
Sol.            cannot be solved with commands need to write shell script.

  1. Write a command to display the total number of bytes used by all files of the current directory.
Sol.            wc –c * | tail -1

                              (or)
                 
                  cat * | wc -c
     
  1. Write a command to copy file x to y only if y not exits.
Sol             ls y || cp x y

  1. Write a command to convert all characters of file x to capitals.
Sol             cat x | tr “a-z” “A-Z”

                              (or)
                 
                  tr “a-z” “A-Z” < x

  1. Write a command to count number of spaces in line 15 of file x.
Sol             head -15 x | tail -1 | tr –cd “ “ | wc -c

  1. Write a command to convert all tabs to spaces of a file.
Sol             cat x | tr “\t” “ “

  1. Write a command to join all lines of a file.
Sol             cat x | tr “\n” “ “

  1. Write a command to join lines 15 & 24 of a file.
Sol             (head -15 x | tail -1; head -24 x | tail -1) | tr “\n” “ “

(or)
                  echo ‘head -15 x | tail -1` `head -24 x | tail -1`


  1. Write a command to display machine hardware type.
Sol             uname –a | cut –d” “ –f6

  1. Write a command to display file permissions and filename.
Sol             ls -l | tr -s " " | cut -d" " -f1,9

  1. Write a command to display the file mask applied for file owner.
Sol             umask | cut –c 1

  1. Write a command to display empno, empname and sal from emp.db.
Sol             cut -d"|" -f1-2,6 emp.db

  1. Write a command to display the total time taken for the above query.
Sol             time cut -d"|" -f1-2,6 emp.db | tail -3 | head -1

  1. Write a command to calculate total amount used by company as salaries.
Sol             echo `cut -d"|" -f6 emp.db | tail +3 | tr "\n" "+"` 0 | bc

  1. Write a command to calculate total amount used by company as salaries along with the message “Total Salary of all employees is : ”.
Sol             echo -e "Total salary of all employess is  : \c";
echo `cut -d"|" -f6 emp.db | tail +3 | tr "\n" "+"` 0 | bc

  1. Write a command to count number of employees in department 10.
Sol             cut -d"|" -f8 emp.db | grep -wc 10

  1. Write a command to list all employees in the department in which SMITH is working.
Sol             cut -d"|" -f2,8 emp.db  | \grep -w `cut -d"|" -f2,8 emp.db  |
grep -w SMITH | cut -d"|" -f2

No comments:

Post a Comment