Sed CMD Unix E-G

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

5/16/2015

Home

SedCommandinUnixandLinuxExamples

DataWarehouse

Informatica

InformaticaScenarios

Oracle

Unix

Hadoop

Subscribe

SedCommandinUnixandLinuxExamples

Search...

Search

SedisaStreamEditorusedformodifyingthefilesinunix(orlinux).Wheneveryouwantto
makechangestothefileautomatically,sedcomesinhandytodothis.Mostpeoplenever
learnitspowertheyjustsimplyusesedtoreplacetext.Youcandomanythingsapartfrom
replacingtextwithsed.HereIwilldescribethefeaturesofsedwithexamples.
Considerthebelowtextfileasaninput.

>catfile.txt
unixisgreatos.unixisopensource.unixisfreeos.
learnoperatingsystem.
unixlinuxwhichoneyouchoose.

SedCommandExamples

PopularPosts

1.Replacingorsubstitutingstring

SedCommandinUnixandLinuxExamples

Sedcommandismostlyusedtoreplacethetextinafile.Thebelowsimplesedcommand
replacestheword"unix"with"linux"inthefile.

TopExamplesofAwkCommandinUnix
CutCommandinUnix(Linux)Examples
FindCommandinUnixandLinuxExamples

>sed's/unix/linux/'file.txt
linuxisgreatos.unixisopensource.unixisfreeos.
learnoperatingsystem.

InformaticaScenarioBasedInterviewQuestionswith
AnswersPart1
DateFunctionsinHive

linuxlinuxwhichoneyouchoose.
SQLQueriesInterviewQuestionsOraclePart1
GrepCommandinUnixandLinuxExamples

Herethe"s"specifiesthesubstitutionoperation.The"/"aredelimiters.The"unix"isthe
searchpatternandthe"linux"isthereplacementstring.

StringFunctionsinHive
TypesofDimensionsindatawarehouse

Bydefault,thesedcommandreplacesthefirstoccurrenceofthepatternineachlineandit
won'treplacethesecond,third...occurrenceintheline.
2.Replacingthenthoccurrenceofapatterninaline.
Usethe/1,/2etcflagstoreplacethefirst,secondoccurrenceofapatterninaline.Thebelow
commandreplacesthesecondoccurrenceoftheword"unix"with"linux"inaline.

>sed's/unix/linux/2'file.txt
unixisgreatos.linuxisopensource.unixisfreeos.
learnoperatingsystem.
unixlinuxwhichoneyouchoose.

3.Replacingalltheoccurrenceofthepatterninaline.

http://www.folkstalk.com/2012/01/sedcommandinunixexamples.html

1/6

5/16/2015

SedCommandinUnixandLinuxExamples

HaveQuestions?FollowMe
Thesubstituteflag/g(globalreplacement)specifiesthesedcommandtoreplaceallthe
occurrencesofthestringintheline.

vijaybhaskar
Addtocircles

>sed's/unix/linux/g'file.txt
linuxisgreatos.linuxisopensource.linuxisfreeos.
learnoperatingsystem.
linuxlinuxwhichoneyouchoose.

4.Replacingfromnthoccurrencetoalloccurrencesinaline.
Usethecombinationof/1,/2etcand/gtoreplaceallthepatternsfromthenthoccurrenceof
apatterninaline.Thefollowingsedcommandreplacesthethird,fourth,fifth..."unix"word
with"linux"wordinaline.

318havemeincircles

Viewall

>sed's/unix/linux/3g'file.txt
unixisgreatos.unixisopensource.linuxisfreeos.
learnoperatingsystem.
unixlinuxwhichoneyouchoose.

5.Changingtheslash(/)delimiter
Youcanuseanydelimiterotherthantheslash.Asanexampleifyouwanttochangetheweb
urltoanotherurlas

>sed's/http:\/\//www/'file.txt

Inthiscasetheurlconsiststhedelimitercharacterwhichweused.Inthatcaseyouhaveto
escapetheslashwithbackslashcharacter,otherwisethesubstitutionwon'twork.
Usingtoomanybackslashesmakesthesedcommandlookawkward.Inthiscasewecan
changethedelimitertoanothercharacterasshowninthebelowexample.

>sed's_http://_www_'file.txt
>sed's|http://|www|'file.txt

6.Using&asthematchedstring
Theremightbecaseswhereyouwanttosearchforthepatternandreplacethatpatternby
addingsomeextracharacterstoit.Insuchcases&comesinhandy.The&representsthe
matchedstring.

>sed's/unix/{&}/'file.txt
{unix}isgreatos.unixisopensource.unixisfreeos.
learnoperatingsystem.
{unix}linuxwhichoneyouchoose.
>sed's/unix/{&&}/'file.txt
{unixunix}isgreatos.unixisopensource.unixisfreeos.
learnoperatingsystem.
{unixunix}linuxwhichoneyouchoose.

http://www.folkstalk.com/2012/01/sedcommandinunixexamples.html

2/6

5/16/2015

SedCommandinUnixandLinuxExamples

7.Using\1,\2andsoonto\9
Thefirstpairofparenthesisspecifiedinthepatternrepresentsthe\1,thesecondrepresents
the\2andsoon.The\1,\2canbeusedinthereplacementstringtomakechangestothe
sourcestring.Asanexample,ifyouwanttoreplacetheword"unix"inalinewithtwiceasthe
wordlike"unixunix"usethesedcommandasbelow.

>sed's/\(unix\)/\1\1/'file.txt
unixunixisgreatos.unixisopensource.unixisfreeos.
learnoperatingsystem.
unixunixlinuxwhichoneyouchoose.

Theparenthesisneedstobeescapedwiththebackslashcharacter.Anotherexampleisifyou
wanttoswitchthewords"unixlinux"as"linuxunix",thesedcommandis

>sed's/\(unix\)\(linux\)/\2\1/'file.txt
unixisgreatos.unixisopensource.unixisfreeos.
learnoperatingsystem.
linuxunixwhichoneyouchoose.

Anotherexampleisswitchingthefirstthreecharactersinaline

>sed's/^\(.\)\(.\)\(.\)/\3\2\1/'file.txt
inuxisgreatos.unixisopensource.unixisfreeos.
aelrnoperatingsystem.
inuxlinuxwhichoneyouchoose.

8.Duplicatingthereplacedlinewith/pflag
The/pprintflagprintsthereplacedlinetwiceontheterminal.Ifalinedoesnothavethe
searchpatternandisnotreplaced,thenthe/pprintsthatlineonlyonce.

>sed's/unix/linux/p'file.txt
linuxisgreatos.unixisopensource.unixisfreeos.
linuxisgreatos.unixisopensource.unixisfreeos.
learnoperatingsystem.
linuxlinuxwhichoneyouchoose.
linuxlinuxwhichoneyouchoose.

9.Printingonlythereplacedlines
Usethenoptionalongwiththe/pprintflagtodisplayonlythereplacedlines.Herethen
optionsuppressestheduplicaterowsgeneratedbythe/pflagandprintsthereplacedlines
onlyonetime.

>sedn's/unix/linux/p'file.txt
linuxisgreatos.unixisopensource.unixisfreeos.
linuxlinuxwhichoneyouchoose.

http://www.folkstalk.com/2012/01/sedcommandinunixexamples.html

3/6

5/16/2015

SedCommandinUnixandLinuxExamples

Ifyouusenalonewithout/p,thentheseddoesnotprintanything.
10.Runningmultiplesedcommands.
Youcanrunmultiplesedcommandsbypipingtheoutputofonesedcommandasinputto
anothersedcommand.

>sed's/unix/linux/'file.txt|sed's/os/system/'
linuxisgreatsystem.unixisopensource.unixisfreeos.
learnoperatingsystem.
linuxlinuxwhichoneyouchosysteme.

Sedprovideseoptiontorunmultiplesedcommandsinasinglesedcommand.Theabove
outputcanbeachievedinasinglesedcommandasshownbelow.

>sede's/unix/linux/'e's/os/system/'file.txt
linuxisgreatsystem.unixisopensource.unixisfreeos.
learnoperatingsystem.
linuxlinuxwhichoneyouchosysteme.

11.Replacingstringonaspecificlinenumber.
Youcanrestrictthesedcommandtoreplacethestringonaspecificlinenumber.Anexample
is

>sed'3s/unix/linux/'file.txt
unixisgreatos.unixisopensource.unixisfreeos.
learnoperatingsystem.
linuxlinuxwhichoneyouchoose.

Theabovesedcommandreplacesthestringonlyonthethirdline.
12.Replacingstringonarangeoflines.
Youcanspecifyarangeoflinenumberstothesedcommandforreplacingastring.

>sed'1,3s/unix/linux/'file.txt
linuxisgreatos.unixisopensource.unixisfreeos.
learnoperatingsystem.
linuxlinuxwhichoneyouchoose.

Herethesedcommandreplacesthelineswithrangefrom1to3.Anotherexampleis

>sed'2,$s/unix/linux/'file.txt
linuxisgreatos.unixisopensource.unixisfreeos.
learnoperatingsystem.
linuxlinuxwhichoneyouchoose.

Here$indicatesthelastlineinthefile.Sothesedcommandreplacesthetextfromsecond
linetolastlineinthefile.

http://www.folkstalk.com/2012/01/sedcommandinunixexamples.html

4/6

5/16/2015

SedCommandinUnixandLinuxExamples

13.Replaceonalineswhichmatchesapattern.
Youcanspecifyapatterntothesedcommandtomatchinaline.Ifthepatternmatchoccurs,
thenonlythesedcommandlooksforthestringtobereplacedandifitfinds,thenthesed
commandreplacesthestring.

>sed'/linux/s/unix/centos/'file.txt
unixisgreatos.unixisopensource.unixisfreeos.
learnoperatingsystem.
centoslinuxwhichoneyouchoose.

Herethesedcommandfirstlooksforthelineswhichhasthepattern"linux"andthenreplaces
theword"unix"with"centos".
14.Deletinglines.
Youcandeletethelinesafilebyspecifyingthelinenumberorarangeornumbers.

>sed'2d'file.txt
>sed'5,$d'file.txt

15.Duplicatinglines
Youcanmakethesedcommandtoprinteachlineofafiletwotimes.

>sed'p'file.txt

16.Sedasgrepcommand
Youcanmakesedcommandtoworkassimilartogrepcommand.

>grep'unix'file.txt
>sedn'/unix/p'file.txt

Herethesedcommandlooksforthepattern"unix"ineachlineofafileandprintsthoselines
thathasthepattern.
Youcanalsomakethesedcommandtoworkasgrepv,justbyusingthereversingthesed
withNOT(!).

>grepv'unix'file.txt
>sedn'/unix/!p'file.txt

The!hereinvertsthepatternmatch.
17.Addalineafteramatch.
Thesedcommandcanaddanewlineafterapatternmatchisfound.The"a"commandto
sedtellsittoaddanewlineafteramatchisfound.

http://www.folkstalk.com/2012/01/sedcommandinunixexamples.html

5/6

5/16/2015

SedCommandinUnixandLinuxExamples

>sed'/unix/a"Addanewline"'file.txt
unixisgreatos.unixisopensource.unixisfreeos.
"Addanewline"
learnoperatingsystem.
unixlinuxwhichoneyouchoose.
"Addanewline"

18.Addalinebeforeamatch
Thesedcommandcanaddanewlinebeforeapatternmatchisfound.The"i"commandto
sedtellsittoaddanewlinebeforeamatchisfound.

>sed'/unix/i"Addanewline"'file.txt
"Addanewline"
unixisgreatos.unixisopensource.unixisfreeos.
learnoperatingsystem.
"Addanewline"
unixlinuxwhichoneyouchoose.

19.Changealine
Thesedcommandcanbeusedtoreplaceanentirelinewithanewline.The"c"commandto
sedtellsittochangetheline.

>sed'/unix/c"Changeline"'file.txt
"Changeline"
learnoperatingsystem.
"Changeline"

20.Transformliketrcommand
Thesedcommandcanbeusedtoconvertthelowercaseletterstouppercaselettersby
usingthetransform"y"option.

>sed'y/ul/UL/'file.txt
Unixisgreatos.UnixisopensoUrce.Unixisfreeos.
Learnoperatingsystem.
UnixLinUxwhichoneyoUchoose.

Herethesedcommandtransformsthealphabets"ul"intotheiruppercaseformat"UL"

NewerPost

Home

pnrstatus

http://www.folkstalk.com/2012/01/sedcommandinunixexamples.html

OlderPost

privacypolicy

6/6

You might also like