SED Command Practise Worksheet PDF

Title SED Command Practise Worksheet
Course Fundamentals of Computing
Institution University of Wolverhampton
Pages 9
File Size 190.6 KB
File Type PDF
Total Downloads 101
Total Views 175

Summary

Commands practise sheet...


Description

SED COMMAND PRACTICE WORKSHEET Sedi saSt r eam Edi t orusedf ormodi f yi ngt hefil esi nuni x( orl i nux) .Whenev ery ouwantt omak e changest ot hefil eaut omat i cal l y ,sedcomesi nhandyt odot hi s .Mos tpeopl enev erl ear ni t s power ;t heyj ustsi mpl yusesedt or epl acet ex t .Youcandomanyt hi ngsapar tf r om r epl aci ngt ex t wi t hsed.Her eIwi l ldescr i bet hef eat ur esofsedwi t hex ampl es . Consi dert hebel owt ex tfi l easani nput .

>cat file.txt unix is great os. unix is opensource. unix is free os. learn operating system. unixlinux which one you choose.

Sed Command Examples 1.Repl ac i ngors ubs t i t ut i ngst r i ng Sedcommandi smost l yusedt or epl acet het exti nafi l e.Thebel owsi mpl esedcommand r epl acest hewor d" uni x"wi t h" l i nux"i nt hefi l e.

>sed 's/unix/linux/' file.txt linux is great os. unix is opensource. unix is free os. learn operating system. linuxlinux which one you choose.

Her et he" s"speci fi est hesubs t i t ut i onoper at i on.The" / "ar edel i mi t er s .The" uni x"i st hesear c h pat t er nandt he" l i nux"i st her epl acements t r i ng. Bydef aul t ,t hesedcommandr epl acest hefir stoccur r enceoft hepat t er ni neachl i neandi twon' t r epl acet hesecond,t hi r d. . . occur r encei nt hel i ne. 2.Repl aci ngt hent hoccur r enceofapat t er ni nal i ne. Uset he/ 1,/ 2et cflagst or epl acet hefi r s t ,secondoccur r enceofapat t er ni nal i ne.Thebel ow commandr epl acest hesecondoccur r enceoft hewor d" uni x"wi t h" l i nux"i nal i ne.

>sed 's/unix/linux/2' file.txt unix is great os. linux is opensource. unix is free os. learn operating system. unixlinux which one you choose.

3.Repl aci ngal l t heoccur r enceoft hepat t er ni nal i ne. Thesubs t i t ut eflag/ g( gl obalr epl acement )s peci fiest hesedcommandt or epl aceal lt he occur r encesoft hes t r i ngi nt hel i ne.

>sed 's/unix/linux/g' file.txt linux is great os. linux is opensource. linux is free os. learn operating system. linuxlinux which one you choose.

4.Repl aci ngf r om nt hoccur r encet oal loccur r encesi nal i ne. Uset hecombi nat i onof/ 1,/ 2et cand/ gt or epl aceal l t hepat t er nsf r om t hent hoccur r enceofa pat t er ni nal i ne.Thef ol l owi ngsedcommandr epl acest het hi r d,f our t h,fi f t h. . ." uni x"wor dwi t h " l i nux"wor di nal i ne.

>sed 's/unix/linux/3g' file.txt unix is great os. unix is opensource. linux is free os. learn operating system. unixlinux which one you choose.

5.Changi ngt hesl ash( / )del i mi t er Youcanuseanydel i mi t erot hert hant hesl ash.Asanex ampl ei fy ouwantt ochanget hewebur l t oanot herur las

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

I nt hi scaset heur l consi st st hedel i mi t erchar act erwhi chweused.I nt hatcasey ouhav et o escapet hesl ashwi t hbacksl ashchar act er ,ot her wi set hesubst i t ut i onwon' twor k . Usi ngt oomanybacksl ashesmak est hesedcommandl ookawkwar d.I nt hi scasewecan changet hedel i mi t ert oanot herchar act erasshowni nt hebel owex ampl e.

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

6.Usi ng&ast hemat cheds t r i ng Ther emi ghtbecaseswher ey ouwantt osear chf ort hepat t er nandr epl acet hatpat t er nby addi ngsomeext r achar act er st oi t .I nsuchcases&comesi nhandy .The&r epr esent st he mat chedst r i ng.

>sed 's/unix/{&}/' file.txt {unix} is great os. unix is opensource. unix is free os. learn operating system. {unix}linux which one you choose.

>sed 's/unix/{&&}/' file.txt {unixunix} is great os. unix is opensource. unix is free os. learn operating system. {unixunix}linux which one you choose.

7.Usi ng\ 1, \ 2andsoont o\ 9 Thefi r stpai rofpar ent hesi sspeci fiedi nt hepat t er nr epr esent st he\ 1,t hesecondr epr esent st he\ 2andsoon.The\ 1, \ 2canbeusedi nt her epl acementst r i ngt omak echangest ot hesour ce

s t r i ng.Asanex ampl e,i fy ouwantt or epl acet hewor d" uni x"i nal i newi t ht wi ceast hewor dl i k e " uni xuni x"uset hesedcommandasbel ow.

>sed 's/\(unix\)/\1\1/' file.txt unixunix is great os. unix is opensource. unix is free os. learn operating system. unixunixlinux which one you choose.

Thepar ent hesi sneedst obeescapedwi t ht hebacksl ashchar act er .Anot herexampl ei si fy ou wantt oswi t cht hewor ds" uni xl i nux"as" l i nuxuni x" ,t hesedcommandi s

>sed 's/\(unix\)\(linux\)/\2\1/' file.txt unix is great os. unix is opensource. unix is free os. learn operating system. linuxunix which one you choose.

Anot herexampl ei sswi t chi ngt hefir s tt hr eechar act er si nal i ne

>sed 's/^\(.\)\(.\)\(.\)/\3\2\1/' file.txt inux is great os. unix is opensource. unix is free os. aelrn operating system. inuxlinux which one you choose.

8.Dupl i cat i ngt her epl acedl i newi t h/ pflag The/ ppr i ntflagpr i nt st her epl acedl i net wi ceont het er mi nal .I fal i nedoesnothav et hesear ch pat t er nandi snotr epl aced,t hent he/ ppr i nt st hatl i neonl yonce.

>sed 's/unix/linux/p' file.txt

linux is great os. unix is opensource. unix is free os. linux is great os. unix is opensource. unix is free os. learn operating system. linuxlinux which one you choose. linuxlinux which one you choose.

9.Pr i nt i ngonl yt her epl acedl i nes Uset henopt i onal ongwi t ht he/ ppr i ntflagt odi spl ayonl yt her epl acedl i nes.Her et henopt i on suppr essest hedupl i cat er owsgener at edbyt he/ pflagandpr i nt st her epl acedl i nesonl yone t i me.

>sed -n 's/unix/linux/p' file.txt linux is great os. unix is opensource. unix is free os. linuxlinux which one you choose.

I fy ouusenal onewi t hout/ p,t hent heseddoesnotpr i ntanyt hi ng. 10.Runni ngmul t i pl esedcommands . Youcanr unmul t i pl esedcommandsbypi pi ngt heout putofonesedcommandasi nputt o anot hersedcommand.

>sed 's/unix/linux/' file.txt| sed 's/os/system/' linux is great system. unix is opensource. unix is free os. learn operating system. linuxlinux which one you chosysteme.

Sedpr ovi deseopt i ont or unmul t i pl esedcommandsi nasi ngl esedcommand.Theabov e out putcanbeachi evedi nasi ngl esedcommandasshownbel ow.

>sed -e 's/unix/linux/' -e 's/os/system/' file.txt linux is great system. unix is opensource. unix is free os. learn operating system. linuxlinux which one you chosysteme.

11.Repl aci ngs t r i ngonaspeci ficl i nenumber . Youcanr est r i ctt hesedcommandt or epl acet hes t r i ngonaspeci ficl i nenumber .Anexampl ei s

>sed '3 s/unix/linux/' file.txt unix is great os. unix is opensource. unix is free os. learn operating system. linuxlinux which one you choose.

Theabovesedcommandr epl acest hest r i ngonl yont het hi r dl i ne. 12.Repl aci ngs t r i ngonar angeofl i nes . Youcanspeci f yar angeofl i nenumber st ot hesedcommandf orr epl aci ngas t r i ng.

>sed '1,3 s/unix/linux/' file.txt linux is great os. unix is opensource. unix is free os. learn operating system. linuxlinux which one you choose.

Her et hesedcommandr epl acest hel i neswi t hr angef r om 1t o3.Anot herexampl ei s

>sed '2,$ s/unix/linux/' file.txt linux is great os. unix is opensource. unix is free os.

learn operating system. linuxlinux which one you choose.

Her e$i ndi cat est hel astl i nei nt hefil e.Sot hesedcommandr epl acest het extf r om secondl i ne t ol astl i nei nt hefil e. 13.Repl aceonal i neswhi chmat chesapat t er n. Youcanspeci f yapat t er nt ot hesedcommandt omat chi nal i ne.I ft hepat t er nmat choccur s , t henonl yt hesedcommandl ooksf ort hes t r i ngt ober epl acedandi fi tfinds,t hent hesed commandr epl acest hest r i ng.

>sed '/linux/ s/unix/centos/' file.txt unix is great os. unix is opensource. unix is free os. learn operating system. centoslinux which one you choose.

Her et hesedcommandfir stl ooksf ort hel i neswhi chhast hepat t er n" l i nux"andt henr epl aces t hewor d" uni x"wi t h" cent os" . 14.Del et i ngl i nes . Youcandel et et hel i nesafil ebyspeci f yi ngt hel i nenumberorar angeornumber s .

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

15.Dupl i cat i ngl i nes Youcanmak et hesedcommandt opr i nteachl i neofafil et wot i mes.

>sed 'p' file.txt

16.Sedasgr epcommand

Youcanmak esedcommandt owor kassi mi l art ogr epcommand.

>grep 'unix' file.txt >sed -n '/unix/ p' file.txt

Her et hesedcommandl ooksf ort hepat t er n" uni x"i neachl i neofafi l eandpr i nt st hosel i nest hat hast hepat t er n. Youcanal somak et hesedcommandt owor kasgr epv ,j ustbyusi ngt her ev er s i ngt hesedwi t h NOT( ! ) .

>grep -v 'unix' file.txt >sed -n '/unix/ !p' file.txt

The!her ei nv er t st hepat t er nmat ch. 17.Addal i neaf t eramat ch. Thesedcommandcanaddanewl i neaf t erapat t er nmat chi sf ound.The" a"commandt osed t el l si tt oaddanewl i neaf t eramat chi sf ound.

>sed '/unix/ a "Add a new line"' file.txt unix is great os. unix is opensource. unix is free os. "Add a new line" learn operating system. unixlinux which one you choose. "Add a new line"

18.Addal i nebef or eamat ch Thesedcommandcanaddanewl i nebef or eapat t er nmat chi sf ound.The" i "commandt osed t el l si tt oaddanewl i nebef or eamat chi sf ound.

>sed '/unix/ i "Add a new line"' file.txt "Add a new line" unix is great os. unix is opensource. unix is free os. learn operating system. "Add a new line" unixlinux which one you choose.

19.Changeal i ne Thesedcommandcanbeusedt or epl aceanent i r el i newi t hanewl i ne.The" c"commandt osed t el l si tt oc hanget hel i ne.

>sed '/unix/ c "Change line"' file.txt "Change line" learn operating system. "Change line"

20.Tr ansf or ml i k et rcommand Thesedcommandcanbeusedt oconv er tt hel owercasel et t er st ouppercasel et t er sbyusi ng t het r ansf or m" y"opt i on.

>sed 'y/ul/UL/' file.txt Unix is great os. Unix is opensoUrce. Unix is free os. Learn operating system. UnixLinUx which one yoU choose.

Her et hesedcommandt r ans f or mst heal phabet s" ul "i nt ot hei rupper c asef or mat" UL"...


Similar Free PDFs