Monday, 12 September 2016

Basic Linux commands:

Sort Command: print the sorted output
 
#sort [OPTIONS] [file name ]
 
  Options : 
 -r  - Reverse Sorting
 -n  - Sort Numerically
 -f  - Case Insensitive Sorting
 -c  - To check that whether a text file is sorted or not
 -u  - To remove duplicate lines from  sorted output.
 -k  - To Sort on a column or word positions in a lines of Text file
 -h  - To sort human readable data (GB , MB , TB )
 -M  - To sort month of an year.
 
If you want to sort the contents of a text file, then you need to use the following command
 
# sort [file name 1] > [file name 2]  ( "cat file name 2" - then you will see the changes)
 
Grep Command : grep prints lines of input matching a specified pattern.
 
 
 #grep [OPTIONS] PATTERN [file name]
 
  Grep Variant:
 
  egrep :- Same as grep -E
  Fgrep :- Same as grep -F
  Rgrep :- Same as grep -r
 
 
#grep – helps  : - Print a help message briefly summarizing command line options and exit.
 
#grep -V (capital V) :- Print the version of grep and exit.
 
Options:
 
  -v  :- Shows all the lines that do not match the searched string.
  -c  :- Displays only the count of matching lines.
  -n  :- Shows matching line and its number.
  -i  :- Match both (upper and lower) case .
  -l  :- Shows just the name of the file with string.
  -w  :  Match entire pattern of a word
 
Example 1 : 
 
#grep Apple myfile.txt  :-  it will display the name of Apple that contains on myfile.txt file.
#grep Apple *  :-  This command will search the given string or pattern in all the files of your present
                   working directory (pwd), and then it will print the filename and lines of those files
                   What match with the given string or pattern.
 
 
#grep Apple test file * :- This command with search the given string or pattern in those files , whose name is start with " testfile" and then it will print the filename and lines of those files  what match with the given string or pattern.
 
 
Highlighting the search using GREP_OPTIONS:-
 
Example 2:-
 
#export GREP_OPTIONS='--color=auto|always|never'
#grep 'Banana' myfile.txt
 
#export GREP_OPTIONS='--color=always'
# grep dhcp dhcpd.conf.sample
 
This will display the name of dhcp in red color .if we do "never" then it will not display in color.
 
Example 3:-
 
Case incentive search using grep -i :
 
Syntax:-
 
#grep -i "string" FILENAME
 
eg.  #grep -i apple myfile.txt   :- It will display all lowercase and uppercase Apple
if #grep apple myfile.txt  :- It will display only lowercase apples
 
Example 4:-
 
Display N lines after match
 
Syntax:-
#grep -A <N> " string' FILENAME
eg:- #grep -A 5 'Banana' myfile.txt
#grep -A Banana myfile.txt:- This will display five lines after Banana
 
Example 5 :-
Display N lines before match: - Syntax:-#grep - B  “string"  file name
 
 
 e.g.  #  grep _B 5 Banana  myfile.txt    :-  This line will display 5 lines before matching .
 
 
 
Example 5 :-
Display N lines before match: - Syntax:-#grep - B  “string"  file name
 
 e.g.  #  grep _B 5 Banana  myfile.txt    :-  This line will display 5 lines before matching .
 
 
Example 6:-
 
Display N lines arouns match
 
Syntax: -
 
  grep -C <N> " string" FILENAME
 
e.g.   #grep -C 'Banana' myfile.txt
 
Display N lines after match and before match.
 
 
Example 7:
 
To search for multiple patterns or string at one time :-
 
Syntax :-
 
#grep -e pattern1 -e pattern2 -e pattern 3 FILENAME
 
e.g.      #grep -e 'gauva' -e 'mango' -e 'apple' myfiletxt
 
To search case insentive string, use the following command
 
  #grep -i -e 'gauva' -e 'mango' -e 'apple' myfiletxt
 
 
Example 8:-
 
To search for multiple patterns or strings at one time :
Syntax :-
 
   #egrep 'pattern1|pattern2|pattern3' FILENAME
 
  e.g  .   #egrep 'summer|mango|apple' myfile.txt
 
To search case incentive string, use the following command
 
 
   #egrep -i 'summer|mango|apple' myfile.txt
 
 
 
Example 9:-
Inverse match using grep -v ( It will not print the name which is match)
 
 
Syntax :-
 
 grep -v 'string' filename
 
e.g.   #grep -v 'Banana' myfile.txt
 
To search case incentive string , use the following command
 
 
e.g .   #grep -vi 'Banana' myfile.txt
 
 
It will display "banana" & 'Banana" .
 
 Example 10 :-
 
 
 #grep -vi -e 'Apple' -e 'Banana' myfile.txt
 
 Output :-  lower and upper case "Banana" & "Apple" will not be displayed .
 
 
 
Example 11:-
 
#grep -vi -e 'Apple' -e 'Banana' myfile.txt | we -l
 
wc-l  :-  It will display total number of line which does not match " Banana" & " Apple" .
 
 
Example 12 :-
 
Counting the number of matches using grep -c :-
 
 syntax:-
 
#grep - c 'string' FILENAME
e.g     #grep -c 'string' myfile.txt
 
 
eg.    # grep -c Apple myfile.txt 
 
 
It will display count of all Apple from that file.
#grep -ci Apple myfile.txt
 
 
It will display all Apple in upercase and lowercase.
 
Now if you are going to check how many lines are available in 'myfile.txt' that does not match with
given 'string ‘ or 'pattern' , to do so you need to use '-v' option with 'drep' command .
 
 
# grep -civ Apple myfile.txt
 
 
For multiple pattern :
 
#grep -ci -e 'Apple' -e 'Banana' myfile.txt
 
This command will print the total number of lines of 'myfile.txt' that match with the given 'string' or 'patterns'
 
 
Example 13 :
 
 Show line number while displaying the output using grep -n
 
Syntax:
 
#grep -n "string" FILENAME
 
 
eg :   #grep -n Apple myfile.txt
 
It will display  string Apple against its line number.
 
 
Example 14 :-
 
How to display only the file names which matches the given pattern using grep -l.
 
Syntax:- #grep -l 'Pattern' FILEPATTERN
 
eg:    #grep -l 'Apple' * mysolutions
 
 
It will display all Apple from mysolution
 
or    # grep -l Apple myfile *
 
This command will print the name of those files of '/etc/mysoultions' directory whose name start with
'myfile' and that contains the given " string" or "pattern"
 
 
Example 15:
 
Show only the matched string :-
 
Syntax :-
 
#grep -o 'pattern' FILENAME
 
eg.  #grep -o 'is.*line ' myfile.txt
 
 
#grep name test.txt1 
 
OUTPUT:
What is your name?
 What is your job?  it will display sentence which is having name word.
 
#grep -o name test.txt
 
OUTPUT:
name
name
 
#grep -o 'w', 'name' test.txt
 
OUTPUT:
 
What is your name?
What is your job ?   It will display word starting with "w" and finished with 'name"
 
 
PIPE COMMAND:
 
 
Pipe(l)  : A pipe is a chain of processes so that output of one process (stdout) is fed and input (stdin)
to another. UNIX shell has a special syntax for creation of pipelines. The commands are written in sequence
Separated by "|" . Different filters are used for Pipes like AWK, GREP
 
 
If you want to use two or more commands at the same time and run then consecutively , you can use pipes.
 
Syntax:
 
#command 1|Command 2
 
e.g.
 
#cat myfile.txt |grep -i  'a'
 
#cat myfile.txt | grep Hi
 
 
                                                      WC ( WORD COUNT):
 
 
The wc (word count) command in Unix/Linux operating system is used to find out the number of newline count,
word count, byte and characters count in a file specified by the file argument.
 
Syntax :-
 
#wc [OPTIONS] [FILENAME]
 
WC Command Options:-
 
-l :- Prints the number of lines in a file.
-w :- Prints the number of word in a file.
-c :- Displays the count of bytes in a file.
-m :- Prints the count of characters from a file.
-L :- Prints only the length of the longest line in a file.
 
eg :  #wc myfile.txt
 
 
OUTPUT : 19 31 176 myfile.txt
 
 
19 : Total number of Lines
31 : Total number of Words
176: Total number of Byte or Character or Filesize
 
 
To count number of Lines in a file use the option ‘-l’, which prints the number of Lines from a given file.
 
Syntax:- #wc –l [Filename]
Example:- [root@rhel6 Test]#wc -l myfile.txt 14 myfile.txt
 
In the output the first field represent the total number of Lines in a given file and the second one is the
name of the file.
 
 
To display the number of Words in a file use the option ‘-w’ with wc command, which prints the number of words from a given file.
 
Syntax:-
#wc –w [Filename]
 
Example:- [root@rhel6 Test]#wc -w myfile.txt 29 myfile.txt
In the output the first field represent the total number of Words in a given file and the second one is the name of the file.
 
 
To count the number of Bytes and Characters in a file use the option ‘-c’ or ‘-m’ with wc command, which prints the number of Bytes and Characters from a given file.
 
Syntax:-
 
#wc –c [Filename] OR #wc –m [Filename]
 
Example:- [root@rhel6 Test]# wc -c myfile.txt 162 myfile.txt OR [root@rhel6 Test]# wc -m myfile.txt 162 myfile.txt
 
In the output the first field represent the total number of Bytes and Characters in a given file and the second one is the Name of the file.
 
To display the Length of Longest Line (Number of characters) of a file, you have to use the ‘-L’ option with ‘wc’ command.
 
Syntax:-
 
#wc –L [Filename]
 
Example:- [root@rhel6 Test]#wc -L myfile.txt 23 myfile.txt
 
In the output the first field represents the total number of Characters of the longest line in a given file and the second one is the Name of the file.
 
 
 

No comments: