Linux Filters

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

LINUX

FILTERS
• Linux Filter commands accept input data from stdin (standard input) and produce

output on stdout (standard output).

• It transforms plain-text data into a meaningful way and can be used with pipes to

perform higher operations.


Linux Filter Commands
1.Cat 7. Paste
2.Tail 8. Unique
3.Head 9. TR
4.Tee 10.WC
5.Sort 11.Grep
6.Cut
CAT

• Displays the text of the file line by line.


• cat > filename
-It is used to create the file
• cat filename
-It displays the contents
• cat >> filename
-It appends the file
Options:
• -A
- shows all the content

• -b
- omits line numbers for blank spaces

• -e or –E
- displays $ at the end of each line prior to net line

• -n
- displays line numbers

• -s
- if the o/p has multiple lines , it replaces with one empty line.
TAIL

• This command prints the last few lines of files


• Syntax : tail [options] filename
• tail – n myfile

-(number of lines to print)


• tail + n myfile
-prints from beginning of the file
HEAD

• This command is reverse of TAIL command it prints the first few lines of files

• Syntax : head -n filename

by default n value is 10

• We can also specify the number of lines

Ex: head -20 filename


TEE

This command writes the output both to the file and to the screen.

• Syntax : ls|tee file

• file1.txt | tee - a file2.txt

-It basically do not overwrite the file but append to the given file.

• Ls|tee file1 file2 file3

-To write the output to multiple files


SORT

• It is used for ordering of file ,sorts the context of the files line by line

typically in alphabetical order

• Line starting with number appears before the line starting with alphabet.

• Syntax : sort [options]file(s)


Options:
• -n:
-sorts numerically
• -r :
- in reverse order
• -u:
-removes repeated lines
• -k n:
-sorts on nth field
• -f:
-case insensitive sort
• -k m,n:
-starts sort on mth field and ends sort on nth field.
CUT

•It is a text processing command used to extract portion of text from a file by
selecting columns or fields.
•To extract only a desired column from a file, use -c option
$ cut –c 2 filename
•Range of characters can also be extracted by specifying start and end
positions delimited with ‘-’
$ cut –c 1-3 filename
$ cut –c 4- filename(starts from 4th position to end of the line)
$ cut –c -8 filename(ends at 8th position)
• To extract the specific bytes, you need to follow -b option with the list of byte
-List without ranges
$ cut -b 1,2,3 filename.txt
-List with ranges
$ cut -b 1-3,5-7 filename.txt
• cut uses tab as a default field delimiter but can also work with other delimiter by
using -d option

Syntax:$cut -d "delimiter" -f (field number) file.txt

EX:
$ cut -d " " -f 1 filename.txt
-here it is considered space as a field separator or delimiter
PASTE

• This command used for pasting the contents of file.


• Syntax: $paste file file2
• -d (delimiter):
Paste command uses the tab delimiter by default for merging the files.
$ paste -d "|" file file2
• -s (serial):
We can merge the files in sequentially manner using the -s option.
It reads all the lines from a single file and merges all these lines into a single
line with each line separated by tab. And these single lines are separated by newline.
$ paste -s file file2 file3
TR

• TR command is used to Translate from upper to lower case and viceversa


• tr [:lower:] [:upper:] <filename
WC

• WC command is used to wordcount from a file


• Syntax: wc filename
• Options:
-l : displays number of lines in a file
-w : number of words
-c: count of bytes
-m: total number of characters
-L: prints only the length of longest line
GREP

• The grep filter searches a file for a particular pattern of characters, and displays all lines that
contain that pattern.
• Syntax : grep [options] pattern [files]
Options:
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-r : recursive search
• To count the number of matches
grep –c “pattern“ filename
• Displays only the filenames which matches the given pattern
grep –I “string“ filename
grep –rI “string“ filename (exact match)
grep –rIw “string“ filename (exact word)
grep –rIwb “string“ filename (exact position)
Thank You!*

You might also like