unix - class6 - sed - detail

13
UNIX - sed Non-Interactive Stream Editor Presentation By Nihar R

Upload: nihar-ranjan-paital

Post on 16-Apr-2017

1.004 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: UNIX - Class6 - sed - Detail

UNIX - sed

Non-Interactive Stream Editor

Presentation By

Nihar R Paital

Page 2: UNIX - Class6 - sed - Detail

Nihar R Paital

Introduction

Developer : Lee E. McMahon

Developed during : 1973-1974

Developed at : Bell Labs

Category : UNIX Utility

Supported by: All UNIX flavors

Page 3: UNIX - Class6 - sed - Detail

Nihar R Paital

For practicing the commands please create a file test1.txt and copy the below contents into test1.txt

sssprint Telecom3sprint Telecomsprint

we are working in sprint

IBM0IBM

nihar,rahulk,sam,papauWe are working in IBM6We are working in IBMnihar:rahulk:sam:papaunihar,rahulk,samWe are working in IBM India

Page 4: UNIX - Class6 - sed - Detail

Nihar R Paital

Transformation using input file$ sed -e ‘s/oldval/newval/g’ inputFile > outputFileExample:$ sed -e ‘s/mumbai/pune/g’ stud.txt > stud.txt.bak

Transformation using a filter in a pipeline$ generate_data | sed -e ‘s/oldval/newval/g’Example:$ cat stud.txt | sed -e ‘s/mumbai/pune/g’ > stud.txt.bak

It performs basic text transformation on an inputstream ( A file / input from a pipeline )

Page 5: UNIX - Class6 - sed - Detail

Nihar R Paital

Main options of sed

p - print

d - delete

s - substitute

Page 6: UNIX - Class6 - sed - Detail

Nihar R Paital

Below special characters have vital role using sed

Character Description

^ Matches the beginning of the line. $ Matches the end of the line. . Matches any single character. * Will match zero or more occurrences of the

previous character. [ ] Matches all the characters inside the [ ].

Page 7: UNIX - Class6 - sed - Detail

Nihar R Paital

Examples (^ Matches the beginning of the line)

$ sed '/^sprint/p' test1.txtWill Print the lines 2 times which contain sprint as a first ward of the line

$ sed -n '/^sprint/p' test1.txtWill Print the lines which contain sprint as a first ward of the line

$ sed 's/^sprint/IBM INDIA/g' test1.txtWill Substitute “IBM INDIA” in place of “sprint” if it appears at beginning of line.

$ sed '/^sprint/d' test1.txtWill delete all lines containing “sprint” as a first word of the line.

Page 8: UNIX - Class6 - sed - Detail

Nihar R Paital

Example ($ Matches the end of the line)

$ sed '/sprint$/p' test1.txtWill Print the lines 2 times which contain sprint as a last ward of the line

$ sed -n '/sprint$/p' test1.txtWill Print the lines which contain sprint as a first ward of the line

$ sed 's/sprint$/IBM INDIA/g' test1.txtWill Substitute “IBM INDIA” in place of “sprint” if it appears at end of line.

$ sed '/sprint$/d' test1.txtWill delete all lines containing “sprint” as a first word of the line.

$ sed -n '/^$/p' test1.txtWill delete all blank lines

Page 9: UNIX - Class6 - sed - Detail

Nihar R Paital

Example (. Matches any single character)

$ sed -n '/./p' test1.txtIt will print the lines which at least contains a single Character. It can be used for ignoring all blank lines.

$ sed -n '/../p' test1.txtIt will print the lines which at least contains a single Character.

$ sed -n '/IB./p' test1.txtIt will print the lines which at least contains a single Character after the String IB”.

Page 10: UNIX - Class6 - sed - Detail

Nihar R Paital

Example (* Will match zero or more occurrences of the previous character)

$ sed -n '/ */p' test1.txtIt will print all lines like “cat test1.txt”

$ sed -n '/ */p' test1.txtIt will print the lines which contains at leaset 2 characters except delimited lines.

$ sed -n '/.*,.*,.*/p' test1.txtIt will print the lines where the strings are separated by at least 3 commas(,).

$ sed -n '/.*,.*,.*/p' test1.txt | cut -d"," -f 3It will print the 3rd filed of the lines where the strings are separated by at least 3 commas(,).

$ sed -n '/We*/p' test1.txtIt will print all the lines where at least the string “We” is present

Page 11: UNIX - Class6 - sed - Detail

Nihar R Paital

Example ([ ] Matches all the characters inside the [ ])

$ sed -n '/[abc]/p' test1.txtThis will print all the lines which contains either a or b or c as part of the line.

$ sed -n '/^[0-9]/p' test1.txtThis will print all the lines which contains the digit 0 to 9 as part of line

$ sed -n '/^[^0-9]/p' test1.txThis will print all the lines which does not contains the digit 0 to 9 as part of line

$ sed 's/[abc]/we/g' test1.txtThis will replace the string “WE” where either a or b or c is present as part of the line.

Page 12: UNIX - Class6 - sed - Detail

Nihar R Paital

Sed – Line number manipulation

1 first line$ last linem,n m to n line Example:$ sed -n '3,7p' test1.txtIt will print the lines from 3 to 7$ sed -n 'n;n;n;p' test1.txt ( every 4th line ) It will print every 4th line of the file

Page 13: UNIX - Class6 - sed - Detail

Nihar R Paital