Cut Command with Examples :
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 like cut command with 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,
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
- -d -Specify the list of file delimiters
- -s- Paste one file at a time instead of parallel
- –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 :
- -d -Which deletes the Characters in data set 1.
- -c -complements the set of characters in specified string
- -t : Which is used to truncate data set 1
- -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.
I hope you like this article on Cut Command with Examples and other filter commands as well.
Click on Topic You want to learn:
- History of SQL
- SQL Create Table(DDL in SQL)
- SQL DML Statements(INSERT,UPDATE,DELETE)
- SQL Select Statement Execution
- Operators in SQL
- Views in SQL
- Materialized View in SQL
- Joins in SQL
- Inner Join / Outer Join
- Full Outer Join / Cartesian Join
- Union and Union ALL
- Intersect and Minus
- Indexing in SQL
- Rank and Dense Rank
- SubQueries and Correlated Subqueries
- Parser and Optimizer
- Oracle 11 G new Features
- SQL Functions List
- Constraints in SQL
- Database Normalization
- Table Partitioning
- Pivot in SQL
- Difference Between Truncate,Delete and drop
- Oracle System Tables
Unix Tutorials :
4.Create File in Unix using multiple ways
12.paste 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.