Skip to main content

Posts

Showing posts with the label UNIX

UNIX : wc command

The wc command is used unix to get the word command,number of lines , character count etc. syntax : > wc [options] The following are the option of a wc command Options Description -c print the byte count in input -m print the character count in input -l print the newline count in input -L print the length of the longest line in input -w print the word count i n input For example consider the input file sample.txt >cat sample.txt apple orange mango kiwi Cherry Now lets look at the output if you give a wc command on it. 1. wc command > wc sample.txt 5 5 31 sample.txt  The output is printed like number of lines, words, and bytes in the file. 2.  wc command to find number of line in a file - this will count for number of \n characters in the file and give that as output.  > wc -l sample.txt 5 sample.txt  3.

UNIX : How to split word with multiple delimeters

If multiple delimiters  are present in a string then the below technique can be used to take the required word Lets say I need to get the word mango.Then > echo "apple,orange mango-strawberry" | sed 's/[ ,-]/|/g' | cut -d"|" -f1 mango Use the sed to convert every delimiter to a same one.Use regular expression to convert it in sed.Then cut the required word.

UNIX : grep command examples

GREP (Global Regular Expression and Print) is a utility used to search for lines containing the string in a file. Syntax of GREP is :  grep [option] [pattern] [ files] 1. Search for lines in a file that contains a word . > grep "word" <filename> 2. How to search for more than one matching string in a file? >egrep "a|b|c" <filename>

UNIX interview questions

1. Display 5th line of a file. cat <file> | head -5 | tail -1 2. How to reverse a string in unix? echo "reverse this string!" | rev 3. How to select last word of a line? echo "i need the last word" | rev | cut -f1 -d' ' | rev 4. Select nth value from a comma seperated line. echo "a,b,c d,e,f,g ,h"| cut -f<n> -d',' 5. How to insert a header record to a file. sed -i "1i HEADERTEXT" <outputfile>