Sed Command in Unix with examples | Stream Editor in Unix | Pipe Command with Examples | Head and Tail command with examples

Sed Command

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:

  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 this article is useful for unix learners.If you like this article dont forget to comment in comment section.

 

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

Be the first to comment

Leave a Reply

Your email address will not be published.


*