Ubuntu Tutorial

Pipe (|) : it is used to combine 2 or more commands. the output of first command given as input to other command. OR the standard output of the first program (on the LHS of the pipe) is given as the standard input of the second program (on the RHS of the pipe).

sort command : sort command is used to print input in sorted order. it sort input in dictionary order ( based on first character or number of word and number of characters). it print first numeric letters then lowercase letters and then uppercase letters.

Note : as you can see following example, 04 is shown before 1 due to the ordering of sort command.

syntax : sort option files....

image
sort command

following table shows some options for sort command.

Option Description
-c or --check or --check=diagnose-first check for sorted input; do not sort
-C or --check=quiet or --check=silent like -c, but do not report first bad line
-d or --dictionary-order consider only blanks and alphanumeric characters
-f or --ignore-case fold lower case to upper case characters
-g or --general-numeric-sort compare according to general numerical value. number is sorted
based on numbering system. output is
(0 1 1DF. 04 4 4 5 7 23GLKJ 34 35 56 78 90 254ASKLD)
-M or --month-sort print sorted months (january, february, march....)
-h or--human-numeric-sort compare human readable numbers (example : 2K 1G)
-n or --numeric-sort compare according to general numerical value. number is sorted
based on numbering system. output is
(0 1 1DF. 04 4 4 5 7 23GLKJ 34 35 56 78 90 254ASKLD)
-R or --random-sort print shuffle or random content or text
-r or --reverse reverse the result of comparisons
-u or --unique with -c, check for strict ordering; without -c, output only the first of an equal run
-V or --version-sort natural sort of (version) numbers within text

using | (pipe) with sort command : see following example, command ls -1R will get execute first. this output is given as input to sort -d command which will sort output based on dictionary order. and print this sorted output on terminal.

image
sort command with pipe(|)

grep command : it searches for particular pattern in text, one or more files and gives line matches with pattern.

syntax : grep option pattern files......

see following example grep command searches string asd in file and print complete file with matched string asd.

image
grep command

following are some options for grep command.

Option Description
-c or --count suppress normal output. print a count of matching lines for each input file.
With the -v or --invert-match option , count non-matching lines.
-e PATTERNS or --regexp=PATTERNS search multiple pattern. combined with the -f (--file) option, search for all patterns given
-H or --with-filename print the file name for each match. This is the default
when there is more than one file to search.
-h or --no-filename suppress the prefixing of file names on output. This is the default
when there is only one file (or only standard input) to search.
-i or --ignore-case ignore case sensitivity
-L or --files-without-match suppress normal output. print the name of each input file from which no output
would normally have been printed. The scanning will stop on the first match.
-l or --files-with-matches suppress normal output. print the name of each input file from which output would
normally have been printed. The scanning will stop on the first match.
-m NUM or --max-count=NUM stop reading a file after NUM matching lines.
-n or --line-number print line number with output line.
-o or --only-matching print only the matched (non-empty) parts of a matching line,
with each such part on a separate output line.
-r or --recursive read all files under each directory, recursively, following symbolic links only if they
are on the command line. Note that if no file operand is given, grep searches the
working directory.
-R or --dereference-recursive read all files under each directory, recursively. Follow all symbolic links, unlike -r.
-v or --invert-match select non-matching lines
-w or --word-regexp select text or lines that matches full word instead of string.
select only those lines containing matches that form whole words
-x or --line-regexp match full line. Select only those matches that exactly match the whole line

following are some examples for grep command

  • grep "^pre" file1.txt : matches string that start with(^) pre in file1.txt
  • grep "post$"" file1.txt : matches string that end with($) post in file1.txt
  • grep "[d-h]" file1.txt : matches string or line that containing any of d-h(d,e,f,g,h) character in file1.txt
  • grep "*[0-9]" file1.txt : matches string or line that that start with number 0 to 9 in file1.txt

using | (pipe) with grep command : see following example, command ls -1R will get execute first. it will give list of all files present in directory( including all subdirectories ). this output is given as input to grep -i .js command which will match case insensitive(-i) .js string and print output on terminal.

image
grep command with pipe