'Unix' in a File
Total Page:16
File Type:pdf, Size:1020Kb
============================ AWK COMMANDS ================================ 1) find the total occurances of pattern of 'unix' in a file. ==> $awk '/unix/ {count++} END{print count}' nova.txt 2) find the total line of a file. ==> $awk 'END{print NR}' nova.txt 3) print the total number of even line of a file. ==> $awk 'NR % 2' nova.txt 4) print the total number of odd line of a file. ==> $awk 'NR % 2 == 1' nova.txt 5) print the sums of the field of every line of a file. ==> $awk '{ sum = $3+sum } END{print sum}' emp.txt 6) display those words whose length greater than 10 and consist of digits only in file awk ==> $awk '/^[0-9]+$/ length<10 {print}' t3.txt 7) display those words whose length less than 10 and consist of alpha only in file awk ==> $awk '/^[a-zA-Z]+$/ length >3 {print}' t3.txt 8) print odd number of word in each line ==> awk -F\| '{s="";for (i=1;i<=NF;i+=2) { {s=s? $i:$i} print s }}' nova.txt { s = ""; for (i = 1; i <= NF; i+=2) { s = s ? $i : $i } print s } 9) print even number of word in each line ==> awk -F\| '{s="";for (i=0;i<=NF;i+=2) { {s=s? $i:$i} print s }}' nova.txt ==> { s = ""; for (i = 0; i <= NF; i+=2) { s = s ? $i : $i } print s } 10) print the last character of a string using awk ==> awk '{print substr($0,length($0),1)}' nova.txt ============================ MIX COMMANDS ================================ 1) Assign value of 10th positional parameter to 1 variable x. ==> x=${10} 2) Display system process; ==> ps -e ==> ps -A 3) Display number of process; ==> ps aux 4) to run a utility x1 at 11:00 AM ==> at 11:00 am x1 5) Write a command to print only last 3 characters of a line or string. ==> grep -o '...$' nova.txt ==> sed 's/.*\(...\)/\1/' nova.txt 6) To delete all vowels from the file. ==> sed 's/[aeiouAEIOU]//g' 7) Count total number of word from file. ==> wc -w nova.txt 8) to delete all special character from file. ==> cat nova.txt | sed 's/[^a-zA-Z]//g' 9) print only special character from file ==> cat nova.txt | sed 's/[a-z0-9A-Z]//g' 10) to print only digit character from file. ==> cat nova.txt | sed 's/[^0-9]//g' 11) display i-node number of all file from current directory. ==> ls -i 12) Which command is used to convert from lower case to upper case in file. ==> cat nova.txt | tr “[a-z]†“[A-Z]†==> cat nova.txt | tr “[:lower:]†“[:upper:]†13) write command to translate white space to tabs from string. ==> echo "Welcome To nova one click solution" | tr [:space:] '\t' o/p: Welcome To nova one click solution 14) write a command to display line which end with 'India'. ==> grep "India$" nova.txt ============================ OTHER QUESTIONS ================================ 1) WHAT IS STICKY BIT? ==> Sticky Bit is mainly used on folders in order to avoid deletion of a folder and it’s content by other users though they having write permissions on the folder contents. If Sticky bit is enabled on a folder, the folder contents are deleted by only owner who created them and the root user. No one else can delete other users data in this folder(Where sticky bit is set). This is a security measure to avoid deletion of critical folders and their content(sub-folders and files), though other users have full permissions. 2) what is command substitution? ==> "Command substitution" is the name of the feature of the shell language that allows you to execute a command and have the output of that command replace (substitute) the text of the command. There is no other feature of the shell language that allows you to do that. 3) explain tr command? ==> The tr command in UNIX is a command line utility for translating or deleting characters. It supports a range of transformations including uppercase to lowercase, squeezing repeating characters, deleting specific characters and basic find and replace. It can be used with UNIX pipes to support more complex translation. tr stands for translate. Syntax : tr [OPTION] SET1 [SET2] Option : - c : complements the set of characters in string.i.e., operations apply to characters not in the given set - d : delete characters in the first set from the output. - s : replaces repeated characters listed in the set1 with single occurrence - t : truncates set1 Exa : cat nova.txt| tr "[a-z]" "[A-Z]" 4) explain pr command? ==> pr (Unix) pr is a command used to paginate or columnate files for printing. It can also be used to compare two files side by side, as an alternative to diff. It is a required program in a POSIX-compliant environment and has been implemented by GNU as part of the GNU Core Utilities. This command helps in formatting the file for printing on the terminal. There are many options available with this command which help in making desired format changes on file. Option : - x Divides the data into 'x' columns - h "header"Assigns "header" value as the report header - t Does not print the header and top/bottom margins - d Double spaces the output file - n Denotes all line with numbers - l page length Defines the lines (page length) in a page. Default is 56 - o margin Formats the page by the margin number Exa : pr -x nova.txt 5) Explain links in unix? ==> A link in UNIX is a pointer to a file. Like pointers in any programming languages, links in UNIX are pointers pointing to a file or a directory. Creating links is a kind of shortcuts to access a file. Links allow more than one file name to refer to the same file, elsewhere. There are two types of links : A) Hard Links B) Soft Link or Symbolic links A) Hard Links - Each hard linked file is assigned the same Inode value as the original, therefore they reference the same physical file location. Hard links more flexible and remain linked even if the original or linked files are moved throughout the file system, although hard links are unable to cross different file systems. - ls -l command shows all the links with the link column shows number of links. - Links have actual file contents - Removing any link, just reduces the link count, but doesn’t affect other links. - We cannot create a hard link for a directory to avoid recursive loops. - If original file is removed then the link will still show the content of the file. - Command to create a hard link is: ln [original filename] [link name] B) Soft Links or Symbolic links - A soft link is similar to the file shortcut feature which is used in Windows Operating systems. Each soft linked file contains a separate Inode value that points to the original file. As similar to hard links, any changes to the data in either file is reflected in the other. Soft links can be linked across different file systems, although if the original file is deleted or moved, the soft linked file will not work correctly (called hanging link). - ls -l command shows all links with first column value l? and the link points to original file. - Soft Link contains the path for original file and not the contents. Removing soft link doesn’t affect anything but removing original file, the link becomes “dangling†link which points to nonexistent file. - A soft link can link to a directory. - Link across filesystems: If you want to link files across the filesystems, you can only use symlinks/soft links. - Command to create a Soft link is: ln -s [original filename] [link name].