Filter Commands in Unix | Cut command with Example | Paste Command With Example | Translate (tr) Command with Example

Oracle partitioning

Filter Commands in Unix :

In Previous articles i have given the overview of most commonly used unix commands,Working with unix directories. In this article i will explain some important Filter Commands in unix with real life examples.Filter commands are basically used to filter the words,files,lines in the  files e.t.c. In this article i will try to explain Simple filter commands with real life examples.You will find following commands with examples in this article:

1.Cut

2.Paste

3.tr

4.Banner

 

Flat files in unix :

Before starting with the commands first we need to understand what exactly the flat files in unix. We will use the same flat files in our examples.The file is said to be a flat file If,

Filter Commands

a) In a file data entered by using delimiter is known as flat file

b) The default delimiter is Tab key

c) Delimiter means field separator(it Should be ,(Comma);Pipe |,Space)

d) Enter key means record separator

Real Life Example:

Create a Student flat file with Student_Id, Name, Ph_Nos, Course and Loc with default delimiter (Tab Key).

$ cat>Student

101      Amit      9988554411     Unix     Pune

102      Rahul    8899774444    Oracle  Mumbai

103      Deepu    88777444444  dba      Hydrabad

104      Ram       9868536451    perl      UK

105      Sonali    9868587458    unix     US

106      Reshma  7788444444  oracle  Algeria

107      Kishore  9868597851    dwh     Hydrabad

Ctrl+d

Creating Flat File using Comma Delimiter :

Create Emp flat file with EmpNo, Ename, Sal and DeptNo with comma delimiter.

$cat>Emp

101,Sonali,50000,10

102,Pradnya,60000,20

103,Amit,80000,10

104,Rohit,65000,30

105,Ankit,50000,20

Ctrl+d

1.Cut Command with Examples :

Cut command is filter command which is used to fetch sections of the specified file.Cut command is used to extract specific fields and characters from the given file.You can be able to select the fields or columns from the line specifying the delimiter.

Real life Examples:

Creation of file :

$cat>Employee.txt

Amit,10000,10

Pradnya 2000,5

1.1.Cut Command to fetch character by position :

Syntax :

$Cut -c Position Filename

Example :

$Cut -c 3 Employee.txt

The above command will display  3rd character of Employee.txt

Output:

i

a

1.2.Cut Command to fetch character by Range:

Syntax :

Using -C option :

$Cut -c Range(- Operator used) Filename

Using -F Option :

$Cut -f Range(- Operator used) Filename

Example :

$Cut -c 1-3 Employee.txt

$Cut -f 1-3 Employee.txt

The above command fetches the text which has range 1 to 3.

Output :

Ami

Pra

1.3.Cut Command to fetch separate character in given Range:

Syntax :

Using -C option :

$Cut -c Range(, Operator used) Filename

Using -F Option :

$Cut -f Range(, Operator used) Filename

Example :

$Cut -c 1,3 Employee.txt

$Cut -f 1,3 Employee.txt

The above command fetches the chaaracters which has position 1 and postion 3.

Output :

Ai

Pa

1.4.Cut Command to fetch separate character in given Range using Delimiter:

Syntax :

Using -C option :

$Cut -c Range(Delimiter) Filename

Using -F Option :

$Cut -f Range(Delimiter) Filename

Example :

$Cut -c 1,3  ‘,’Employee.txt

$Cut -f 1,3 ‘,’ Employee.txt

The above command fetches the chaaracters which has position 1 and postion 3 which has comma Delimiter.

Output :

A,i

P,a

1.5.Cut Command used to display first field in etc/passwd file command :

The passwd file is delimeter file which is seperated by colon (:) delimitor. The Cut command is used to display the first field of passwd file.

Example :

$Cut -d ‘:’ -f  etc/passwd

Output :

@

It dispays first character of the passwd file which is ‘@’.

2.Paste Command with Examples :

Paste command is one of the useful and most used linux or unix command which is used to merge the characters or lines of 2 different files.Paste command sequencially writes the corresponding lines from each specified file,

Syntax :

Paste Options File1..File2…File n

Following are important options of Paste Command

  1. -d -Specify the list of file delimiters
  2. -s- Paste one file at a time instead of parallel
  3. –help – You will get help for paste command using the above option.

Create following 2 files for Example :

$cat>States

MH

AP

Ctrl+d

$cat>Cities

Pune

Hydrabad

Ctrl+d

2.1.Merge Files parallel :

Syntax:

Paste File1 File2..FileN

Example:

$ paste States Cities

The above paste statement Merges 2 files in following fashion.By Default it has taken tab (Space) as delimiter

Output :

MH Pune

AP Hydrabad

2.2.Merge Files parallel Specifying the Delimiter:

Syntax:

Paste -d “Delimeter” File1 File2..FileN

Example:

$ paste -d ‘,’ States Cities

The Above statement takes comma as delimeter and merges two files.

Output :

MH,Pune

AP,Hydrabad

2.3.Merge Files Sequencially :

Syntax:

Paste -s  File1 File2..FileN

Example:

$ paste -s  States Cities

Above statement merges 2 Files sequencially named States and Cities.

Output :

MH AP

Pune Hydrabad

2.4.Merge Files Sequencially  using Delimeter:

Syntax:

Paste -s -d [Delimeter] File1 File2..FileN

Example:

$ paste -s -d ‘,’  States Cities

Above statement merges 2 Files sequencially named States and Cities.

Output :

MH,AP

Pune,Hydrabad

3.Translate Command (tr) in unix :

Tr  command is also mostly used  unix Filter Commands which stands for translate and used to translate file character by character.

Syntax :

Tr[Options] Data set1..Data set2…Data set N

Options of Translate command :

  1. -d -Which deletes the Characters in data set 1.
  2. -c -complements the set of characters in specified string
  3. -t : Which is used to truncate data set 1
  4. -s : Replaces the repeated characters listed in set 1 with single occurance.

3.1. Converts Lowercase letters to uppercase letters and uppercase letters to lowercase letters (method 1):

Tr command used to translate the lowercase letters in to uppercase letters.

Create file :

$ cat>Linux.txt

I am learning unix

Ctrl+d

Syntax for lowercase to uppercase :

tr “[:lower:]” “[:upper:]”   < File_name

Syntax for  uppercase to lowercase :

tr “[:upper:]” “[:lower:]”   < File_name

Example :

tr “[:lower:]” “[:upper:]”   < Linux.txt

Output :

I AM LEARNING UNIX

tr “[:upper:]” “[:lower:]”   < Linux.txt

i am learning unix

3.2. Converts Lowercase letters to uppercase letters and uppercase letters to lowercase letters  (Method 2):

Syntax for lowercase to uppercase :

tr “[a-z]” “[A-Z]”   < File_name

Syntax for  uppercase to lowercase :

tr  “[A-Z]”  “[a-z]” < File_name

Example :

tr “[a-z]” “[A-Z]”  < Linux.txt

Output :

I AM LEARNING UNIX

tr  “[A-Z]”  “[a-z]” <Linux.txt

i am learning unix

3.3 Replacing the Delimeter :

Tr command is also used to replace the delimeter in the file.

Syntax :

tr ‘ Delimiter used in file’ ‘Delimeter you want to use’ < File_name

Example :

tr ‘\t’ ‘,’ < Linux.txt

It replaces tab with comma.

Output :

I, am,Learning,Unix

3.4. Replacing non matching characters :

tr command is used to replace non-matching characters.

Example :

$echo “Amit” | tr -c “A” “P”

Output:

APPP

tr has replaced each and every character except character ‘A’ with P.

3.5.Delete Specific character from the File:

Example :

$tr -d ‘U’ <Linux.txt

Output:

I am learning nix

The character ‘U’ has been deleted.

3.6. Use of “aeiou” vowels :

With tr command you can use “aeiou”  vowels to delete or convert it to uppercase and lowercase letters.

Examples :

$ tr “aeiou” “AEIOU” <Linux.txt

It replaces Lower Case characters with Upper Case by considering vowels.

Output :

I Am lEArnIng UnIx

Example 2 :

$ tr -d “aeiou” “AEIOU” <Linux.txt

It deletes Lower Case “aeiou” characters from Sample file.

Output :

I m lrnng nx

4. Banner Command :

Banner: Banner command prints a message in large letters which look like banner.

$ banner Hello

Note:Banner can accommodate only 10 characters in one line.

Click on Topic You want to learn:

  1. History of SQL
  2. SQL Create Table(DDL in SQL)
  3. SQL DML Statements(INSERT,UPDATE,DELETE)
  4. SQL Select Statement Execution
  5. Operators in SQL
  6. Views in SQL
  7. Materialized View in SQL
  8. Joins in SQL
  9. Inner Join / Outer Join
  10. Full Outer Join / Cartesian Join
  11. Union and Union ALL
  12. Intersect and Minus
  13. Indexing in SQL
  14. Rank and Dense Rank
  15. SubQueries and Correlated Subqueries
  16. Parser and Optimizer
  17. Oracle 11 G new Features
  18. SQL Functions List
  19. Constraints in SQL
  20. Database Normalization
  21. Table Partitioning
  22. Pivot in SQL
  23. Difference Between Truncate,Delete and drop
  24. Oracle System Tables

Unix Tutorials :

1.What is unix?

2.Basic unix commands

3.File Commands in unix

4.Create File in Unix using multiple ways

5.Cat Command

6.Touch Command

7.Mkdir command

8.rmdir Command

9.pwd command

10.Cd Command

11.cut Command

12.paste Command

13.tr Command

14.Cp Command

15.wc command

16.cmp command

17.Rm Command

18.Grep Command

19.Egrep Command

20.FGrep Command

Hope you will get proper idea about 3 important Filter Commands in unix from this article.If you like this article dont forget to comment in comment section.

 

 

 

 

 

 

 

About admin 136 Articles
Oracle Consultant with Vast Experience in Oracle BI and SQL PLSQL Development

5 Trackbacks / Pingbacks

  1. Filter Commands in Unix | Sort command with Example | Uniq Command with Example | Date Command with Example
  2. Grep Command in unix | Grep Command with Examples | Grep Command with Options | Search String in Unix File | Practical Examples of Grep command | fgrep command with example | egrep command with example
  3. Database Normalization Examples | Normalization in Database with Examples | Database Normalization with Real Life Examples | Designing Normalized Database
  4. Interview Questions for HCL | SQL Interview Questions for HCL | Interview Questions Asked in HCL | HCL Interview Questions | 20 Most Important SQL Interview Questions for HCL
  5. Operators in SQL | SQL Operators | Arithmetic Operators | Logical Operators | Comparison Operators | Negation Operators | Operators in SQL with Examples

Leave a Reply

Your email address will not be published.


*