Sed Command in Unix :
In Previous articles i have explained about file commands in unix,directory commands,Filter commands in unix.In this article i will explain Sed Command in unix which will help to modify the files in unix.Sed command in unix is basically used to replace and find the text but sed command also used to do many things apart from replacing the text.It is used to search and replace a string and it is multi-purpose filter string.
Syntax:
Sed “s/old string/new string/g”
s: means substitution
g: means every line all occurrences
without g: means every line 1st occurrences
Real Life Examples with options :
Create a file :
$cat>Sed_File
a b c complexsql d e complexsql f complexsql
complexsql a b complexsql
complexsql abc complexsql
complexsqlabccomplexsql
technologiesabc
technologyabc
techie
tech
22
333
4444
55555
Ctrl+d
Examples:
1) sed “s/complexsql/Amit/g” Sed_File:
The above command replaces the word complexsql to Amit.Here s stands for substitution and g stands for every line.
2) sed “s/complexsql/Amit/gi” Sed_File:
The above command adds the new line before word complexsql and replaces word complexsql to Amit.
3) sed “s/complexsql/Amit/” Sed_File:
The above command directly substitudes the word complexsql to Amit.
4) sed “s/complexsql/Amit/i” Sed_File
5) sed “s/^tech/Amit/g” Sed_File:
It replaces “tech” with Amit
6) sed “s/^tech$/Amit/g” Sed_File :
It replaces exactly “tech” with Amit by checking uppercase and lowercase letters.
7) sed “s/\<tech\>/Amit/g” Sed_File :
It replaces exactly “tech” with Amit.This is another syntax for replacing the words by checking case sensitivity.
8) sed “s/^$/***Blank Lines***/g” Sed_File:
It replaces all Empty Lines with “***Blank Lines***”
9) sed “s/\<complexsql\>//gi” Sed_File :
It deletes “complexsql” word from the file
10) sed “s/[0-9][0-9]/*/g” Sed_File:
It replace 2 digit number with “*”
11) sed “s/[0-9]/@/g” Sed_File:
It replaces “n” digits with “@”
12) sed “s/[0-9]{3,4}/@/g” Sed_File
13) sed -n “3p” Sed_File:
It Prints 3rd line
14) sed -n “3,5p” Sed_File:
It Prints 3rd to 5th lines
15) sed -n “1p” Sed_File:
It Prints 1st line from a file
15A) head -1 Sed_File:
It Prints 1st line from a file
16) sed -n ‘$p’ sed_file:
It Prints Last Record from a file
16A) tail -1 Sed_File:
It Prints Last Record from a file
17) $sed -n ‘1,$p’ Sed_File:
It Prints 1st to Last Record from a file
18) $ sed -n ‘1p :
It Prints 1st& Last Record from a file
> $p’ Sed_File
19) $ sed -n ‘1p;$p’ Sed_File:
It Prints 1st& Last Record from a file
20) $ sed ‘1d’ Sed_File:
It Deletes first line from a file
21) $ sed ‘4d’ Sed_File:
It Deletes 4th line from a file
22) $ sed ‘3,5d’ Sed_File :
It Deletes 3rd to 5th Lines from a file
23) $ sed ‘$d’ Sed_File:
It Deletes Last Line from a file
24) $ sed ‘3,5w Temp_File’ Sed_File:
It Writes/Copies 3rd to 5th line from Sed_File to Temp_File
Piping (|):
It is used to combine two or more commands
Note: Pipe always execute left side command & O/P goes to right side as I/P
1) Count number of users connected to the server?
$ who | wc –l
2) Count number of files in current directory?
$ ls | wc–l
3) Count total number of sub-directories in current directory?
$ ls -l | grep ‘^d’ | wc–l
4) Display today’s date day?
$ date | cut -c 1-3
tee:It is used to write data to the file as well as to the screen.
1) $ cat Amit| tee Temp
It copies Amitfile data to Temp and also displays data to the screen
2) $ cat Amit| tee Temp Temp1
It copies Saif file data to Temp, Temp1 and also displays data to the screen
Head:It displays FIRST ‘n’ lines from a file
$ head -1 Amit
It displays FIRST line from the file.
$ head -3 Amit
It displays FIRST 3 lines from the file.
Tail:It displays LAST ‘n’ lines from a file
$ tail -1 Amit
It displays LAST line from the file.
$ tail -3 Amit
It displays LAST 3 lines from the file.
Note: By Default, head & tail displays 10 lines of a given file.
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 this article is useful for unix learners.If you like this article dont forget to comment in comment section.