Loading...
Basic Linux

Filter Commands

cut:

It divides a string or output.
syntax:

cut [option]  Filename

option:
-d   specifies a delimiter
-f   Displays a particular
-c   Displays a character

Displays the third field of the text using space as a delimiter:

$cut  -d " " -f3 file_example

Display third and fourth fields:

$cut  f3,4  file_example

Displays first to firth characters:

$cut  -c  1-5 file_example

WC:

It provides a word or line count.
Syntax:

wc  [option]  file

Option:
-l lines
-w words
-c characters

Display lines, words and characters

$wc  example
$wc  -l  example
$wc  -w  example

Diff:

Display different lines between two files.

$diff   file1   file2

cmp:

It compares two file character by character.

$cmp   file1  file2

Note: If file are same it doesn’t return any output otherwise it displays line number and character position.

tr:

It translate character by character.

$tr "a e i o u" "A E I O U" <sample
$tr   "a-z"   "A-Z"<sample
$tr   -s  " "<sample
$tr    -d  "a e i o u" <sample
$tr   ","  "\t" <sample

aspellcheck:

To check the spelling mistakes but not grammatical mistake

$aspellcheck sample
$aspellcheck test

Head:

Display top 10 lines of the file

$head sample

Display top 5 line

$head  -5 sample

Tail:

Display last 10 lines of the file.

$tail  sample

Display last 5 lines

$tail -5 sample

File is open continuously

$tail  -f sample

Piping( | ):

Combine the two or more commands in to a single line.
Here the first command output it taken as the next commandinput

$ls  -l| wc  -l
$cat  example|cut  -d  " " -f3 file_example
$cat example |head  -20

&&:

Combine commands

$echo "This is  text file" >file_example && cut -f3 example

To verify

$cat file_example
$echo "My original text" >> file_example && cat file_example

More:

To see the contents of a file in the form of page wise

$more example

Less:

To display file contents in page wise. But we can go to all directions

$less example

Options:
f -> Forward direction
d -> Backward direction
vi -> vi editor mode
q -> To quit

Tee:

It is used to write the data into the files as well as on the screen

$cat sample|tee file1 file2 file3

To verify

$cat file1
$cat file2

Sort:

Sort the output of a command or file.
Syntax:

sort [options] FILE

Options:
-r -> Sorts in reverse order
-b -> Ignores leading blanks
-n -> compares according to numerical string value

Examples:

$sort example

Reverse Order

$sort  -r example

Display Numeric

$sort  -n example

Unique Lines

$sort  -u

Ignores Case

$sort  -f example

Uniq:

List all the unique lines in a file or command output

$uniq  example

Display non duplicate lines

$uniq   -u example

Display only duplicated lines

$uniq   -d example
$uninq file_example> uniq_file && cat uniq_file

In above command to view uniq lines in the sample file, create a new file based on output and view the contents of this new file.

Sed (Stream Editor):

To search and replace strings or patterns in the given file.
Sed is a multi-purpose filter command.
Syntax:

sed "s/old string name/ new string name/g" <filename>

s -> substitution.
g -> Global occurrence in every line.
Example:

$sed   "s/unix/linux/g"  sample

Ignore case

$sed   "s/unix/linux/gi"  sample
$sed   "s/unix/linux/"   sample
$sed   "s/^unix/linux/gi"   sample

Delete a a word from a file

$sed   "s/unix//gi"   sample

Leave a Reply

Your email address will not be published. Required fields are marked *