Tải bản đầy đủ - 0 (trang)
Chapter 12. Showing What's in a File

Chapter 12. Showing What's in a File

Tải bản đầy đủ - 0trang

SummaryBox

Thischaptertalksaboutthemanywaysofdumpingafiletothescreen.Most

usersknowthebruteforceapproachprovidedbycat(Section12.2),butthere's

moretoitthanthat:

Pagerssuchasmoreandless(Section12.3)thatgiveyoumorecontrol

whenlookingthroughlongfiles.

Findingoutwhattypeofdataafilecontainsbeforeopeningit(Section

12.6).

Lookingatjustthebeginningorendofafile(Section12.8through

Section12.12).

Numberinglines(Section12.13).

—TOR



12.2WhatGoodIsacat?

Thecatcommandmaywellbethefirstcommandnewusershearabout,ifonly

becauseofitsoddname.catstandsforconcatenateor,assomewouldsay,

catenate.Bothwordsmeanthesamething:toconnectinaseries.Thecat

commandtakesitsfilenameargumentsandstringstheircontentstogether.

Essentially,cattakesitsinputandspitsitoutagain.

cathasmanyuses,butthefourmostbasicapplicationsaredescribedinthe

followinglist.Inmanyways,theydon'tillustratecatsomuchastheyillustrate

theshell'soutputredirection(Section43.1)mechanism.

1. Firstform:



%catfile

%catfile1file2file...

Usethisformtodisplayoneormorefilesonthescreen.Theoutput



doesn'tpausewhenthescreenisfull.Asaresult,ifyourfilesaremore

thanonescreenfullong,theoutputwillwhizbywithoutgivingyoua

chancetoreadit.Toreadoutputbyscreenfuls,useapagersuchasless

(Section12.3).[1]

Secondform:



%catfile(s)>new_file

Usethisformwhenyouwanttocombineseveralsmallerfilesintoonelargefile.

Besurethedestinationfiledoesnotalreadyexist;otherwise,itwillbereplaced

bythenewcontents(effectivelydestroyingtheoriginal).Forexample,the

command:



%catchap1chap2chap3>book

createsanewfile,book,composedofthreefiles,oneaftertheother.Thethree

componentfilesstillexistaschap1,chap2,andchap3.

Thirdform:



%catfile>>existing_file

%catfiles>>existing_file

Usethisformtoaddoneormorefilestotheendofanexistingfile.Forexample:



%catnote1note2>note_list

%catnote3>>note_list

Fourthform:



%cat>newfile

Usethisformasaquick-and-dirtywaytocreateanewfile.Thisisusefulwhen

youaren'tyetfamiliarwithanyofthestandardtexteditors.Withthiscommand,

everythingyoutypeatthekeyboardgoesintothenewfile.(Youwon'tbeableto

backuptoapreviousline.)Tofinishyourinput,enterCTRL-donalineby



itself.

Well,thatwasjustincasetherearesomebeginnersonboard.Section12.4,

Section12.7,andSection12.13givesomemoreusefultipsaboutcatoptions.

—DG



12.3"less"isMore

ThemostpopularpagerforUnixsystemswasoncethemorecommand,so

namedbecauseitgaveyou"onemorescreen."moreisubiquitous,butalso

somewhatlimitedinitscapability.Thelesscommand(sonamedbecause,of

course,"lessismore!")ismorecommonlyused.lessisafull-featuredtextpager

thatemulatesmorebutoffersanextendedsetofcapabilities.

Oneparticularlyimportantfeatureoflessisthatitdoesnotreadallofitsinput

beforestarting,whichmakesitfasterthananeditorforlargeinput.lessalso

offersmanyusefulfeaturesandisavailableforalmosteveryoperating

environment.Asanextrabonus,itisinstalledbydefaultonmostfreeUnixes.

lessbeginsexecutionbyfirstexaminingtheenvironmentinwhichitisrunning.

Itneedstoknowsomethingsabouttheterminal(orwindow)inwhichitsoutput

willbedisplayed.Oncethat'sknown,lessformatsthetextanddisplaysthefirst

screen'soutput.Thelastlineofthescreenisreservedforuserinteractionwith

theprogram.lesswilldisplayacolon(:)onthefirstcolumnofthelastlineand

leavethecursorthere.Thiscolonisacommandprompt,awaitingcommand

inputfromtheuser.Mostcommandstolessaresingle-characterentries,andless

willactuponthemimmediatelyandwithoutasubsequentcarriagereturn(thisis

knownascbreakmode).Themostbasiccommandtoless(andmore)isasingle

space,whichinstructsthepagertomoveaheadinthetextbyonescreen.Table

12-1listscommonlyusedlesscommands.

Table12-1.Commonlyusedlesscommands

Command



Space



Description



Scrollforwardonescreen.



d



Scrollforwardone-halfscreen.



RETURN



Scrollforwardoneline.



b



Scrollbackwardonescreen.Unlikemore,whilelessisreadingfrompipes

Section1.5),itcanredrawthescreenandreadpreviouspages.



u



Scrollbackwardone-halfscreen.



y



Scrollbackwardoneline.



g



Gotothebeginningofthetext(couldbeslowwithlargeamountsoftext).



G



Gototheendofthetext(couldbeslowwithlargeamountsoftext).



/pattern Searchforwardforpattern,whichcanbearegularexpression.

?pattern Searchbackwardforpattern,whichcanbearegularexpression.



n



Searchforthenextoccuranceofthelastsearch,inthesamedirection:

forwardinthefileiftheprevioussearchwasusing/andbackwardsin

thefileiftheprevioussearchwasusing?.



N



Searchforthepreviousoccuranceofthelastsearch.Seeearlier.



h



Displayahelpscreen.



:n



Displaynextfilefromcommandline(two-charactercommand).



:p



Displaypreviousfilefromcommandline(two-charactercommand).



lesshasarichcommandset,anditsbehaviorcanbemodifiedasneededforyour

use.Thelesskeyprogramletsyoumakecustomkeydefinitions,andyoucan

storeyourfavoritesetupoptionsintheLESSenvironmentvariable(Section

35.3).Seethelessmanpageforfurtherdetails.

Oneofthebigadvantagesoflessisthatitdoesn'trequireanyrelearning;less

doestherightthingwhenyouusemore,vi(Section17.2),oremacs(Section

19.1)file-browsingcommands.Incidentally,italsoprotectsyoufromterminal

controlsequencesandotherobnoxiousthingsthathappenwhenyoutrytoview

abinaryfile,becauseitescapesnonprintingcharacters(Section12.4).

—JD



12.4ShowNonprintingCharacterswithcat-vor

od-c

EspeciallyifyouuseanASCII-basedterminal,filescanhavecharactersthat

yourterminalcan'tdisplay.Somecharacterswilllockupyourcommunications

softwareorhardware,makeyourscreenlookstrange,orcauseotherweird

problems.Soifyou'dliketolookatafileandyouaren'tsurewhat'sinthere,it's

notagoodideatojustcatthefile!

Instead,trycat-v.ItshowsanASCII("printable")representationofunprintable

andnon-ASCIIcharacters.Infact,althoughmostmanualpagesdon'texplain

how,youcanreadtheoutputandseewhat'sinthefile.Anotherutilityfor

displayingnonprintablefilesisod.Iusuallyuseits-coptionwhenIneedtolook

atafilecharacterbycharacter.

Let'slookatafilethat'salmostguaranteedtobeunprintable:adirectoryfile.

ThisexampleisonastandardV7(UnixVersion7)filesystem.(Unfortunately,

someUnixsystemswon'tletyoureadadirectory.Ifyouwanttofollowalongon



oneofthosesystems,tryacompressedfile(Section15.6)oranexecutable

programfrom/bin.)Adirectoryusuallyhassomelonglines,soit'sagoodidea

topipecat'soutputthroughfold:



%ls-fa

.

..

comp

%cat-v.|fold-62

M-^?^N.^@^@^@^@^@^@^@^@^@^@^@^@^@>^G..^@^@^@^@^@^@^@^@^

M-a

comp^@^@^@^@^@^@^@^@^@^@^@^@MassAveFood^@^@^@^@^@hist^@

@^@^@^@^@^@^@^@

%od-c.

0000000377016.\0\0\0\0\0\0\0\0\0

0000020>007..\0\0\0\0\0\0\0\0

0000040341\ncomp\0\0\0\0\0\0

0000060\0\0MassAveFoo

0000100\0\0hist\0\0\0\0\0\0

0000120

EachentryinaV7-typedirectoryis16byteslong(that'salso16characters,in

theASCIIsystem).Theod-ccommandstartseachlinewiththenumberof

bytes,inoctal,shownsincethestartofthefile.Thefirstlinestartsatbyte0.The

secondlinestartsatbyte20octal(that'sbyte16indecimal,thewaymostpeople

count).Andsoon.Enoughaboutodfornow,though.We'llcomebacktoitina

minute.Timetodissectthecat-voutput:

You'veprobablyseensequenceslike^Nand^G.Thosearecontrol

characters.

Anothercharacterlikethisis^@,thecharacterNUL(ASCII0).Therearea

lotofNULsinthedirectory;moreaboutthatlater.ADELcharacter(ASCII

177octal)isshownas^?.CheckanASCIIchart.



cat-vhasitsownsymbolforcharactersoutsidetheASCIIrangewiththeir

highbitsset,alsocalledmetacharacters.cat-vprintsthoseasM-followed

byanothercharacter.Therearetwooftheminthecat-voutput:M-^?and

M-a.

Togetametacharacter,youadd200octal.Foranexample,let'slookatMa.Theoctalvalueoftheletterais141.Whencat-vprintsM-a,itmeans

thecharacteryougetbyadding141+200,or341octal.

YoucandecodethatthecharactercatprintsasM-^?inthesameway.The

^?standsfortheDELcharacter,whichisoctal177.Add200+177toget

377octal.

Ifacharacterisn'tM-somethingor^something,it'saregular

printablecharacter.Theentriesinthedirectory(.,..,comp,

MassAveFood,andhist)areallmadeofregularASCIIcharacters.

Ifyou'rewonderingwheretheentriesMassAveFoodandhistarein

thelslisting,theansweristhattheyaren't.Thoseentrieshavebeendeleted

fromthedirectory.UnixputstwoNUL(ASCII0,or^@)bytesinfrontof

thenamesofdeletedV7directoryentries.

cathastwooptions,-tand-e,fordisplayingwhitespaceinaline.The-voption

doesn'tconvertTABandtrailing-spacecharacterstoavisibleformwithoutthose

options.SeeSection12.5.

Next,od-c.It'seasiertoexplainthancat-v:

od-cshowssomecharactersstartingwithabackslash(\).Itusesthe

standardUnixandCabbreviationsforcontrolcharacterswhereitcan.For

instance,\nstandsforanewlinecharacter,\tforatab,etc.There'sa

newlineatthestartofthecompentry—seeitintheod-coutput?That

explainswhythecat-voutputwasbrokenontoanewlineatthatplace:cat

-vdoesn'ttranslatenewlineswhenitfindsthem.

The\0isaNULcharacter(ASCII0).It'susedtopadtheendsofentriesin



V7directorieswhenanameisn'tthefull14characterslong.

od-cshowstheoctalvalueofothercharactersasthreedigits.Forinstance,

the007means"thecharacter7octal."cat-vshowsthisas^G(CTRL-g).

Metacharacters,theoneswithoctalvalues200andhigher,areshownasMsomethingbycat-v.Inod-c,you'llseetheiroctalvalues—suchas

341.

EachdirectoryentryonaUnixVersion7filesystemstartswithatwo-byte

"pointer"toitslocationinthedisk'sinodetable.Whenyoutypeafilename,

Unixusesthispointertofindtheactualfileinformationonthedisk.The

entryforthisdirectory(named.)is377016.Itsparent(named..)isat

>007.Andcomp'sentryis341\n.Findthoseinthecat-voutput,if

youwant;andcomparethetwooutputs.

Likecat-v,regularprintablecharactersareshownasisbyod-c.

Thestrings(Section13.15)programfindsprintablestringsofcharacters(such

asfilenames)insidemostlynonprintablefiles(suchasexecutablebinaries).

—JP



12.5What'sinThatWhitespace?

Thecat-voption(Section12.4)showsanASCIIrepresentationofunprintable

andnon-ASCIIcharacters.cathastwooptionsfordisplayingwhitespaceina

line.Ifyouusethe-toptionwith-v,TABcharactersareshownas^I.The-e

optioncombinedwith-vmarkstheendofeachlinewitha$character.Some

versionsofcatdon'trequirethe-vwiththoseoptions.Let'scompareaone-line

filewithoutandwiththe-t-e(whichmayhavetobetypedseparately,bythe

way;-tewon'tworkonsomeversions):



%catafile

Thisisaone-linefile-boring,eh?

%cat-v-t-eafile



ThiS^Hsis^Iaone-linefile^I-boring,eh?$

Althoughyoucan'ttellitfromplaincat,there'sabackspace(CTRL-h)before

thefirsts,twoTABsthattakeuponlyonecolumnofwhitespaceeach,and

sevenspacesattheendoftheline.Knowingthiscanhelpyoudebugproblems

inprintinganddisplayingfiles.It'salsoahelpforshellprogrammerswhoneed

toparseorsorttheoutputofotherprograms.

—JP



12.6FindingFileTypes

ManydifferentkindsoffilesliveonthetypicalUnixsystem:databasefiles,

executablefiles,regulartextfiles,filesforapplicationslikeStarOffice,tarfiles,

mailmessages,directories,fontfiles,andsoon.

Youoftenwanttochecktomakesureyouhavetheright"kind"offilebefore

doingsomething.Forexample,you'dliketoreadthefiletar.Butbeforetyping

moretar,you'dliketoknowwhetherthisfileisyoursetofnoteson

carbon-basedsludgeorthetarexecutable.Ifyou'rewrong,theconsequences

mightbeunpleasant.Sendingthetarexecutabletoyourscreenmightscrewup

yourterminalsettings,logyouoff,ordoanynumberofunpleasantthings.

Gotohttp://examples.oreilly.com/upt3formoreinformationon:file

Thefileutilitytellsyouwhatsortoffilesomethingis.It'sfairlyself-explanatory:



%file/bin/sh

/bin/sh:sparcdemandpagedexecutable

%file2650

2650:[nt]roff,tbl,oreqninputtext

%file0001,v

0001,v:asciitext

%filefoo.sh

foo.sh:shellcommands



fileisactuallyquitecleverthoughitisn'talwayscorrect—someversionsare

betterthanothers.Itdoesn'tjusttellyouifsomething'sbinaryortext;itlooksat

thebeginningofthefileandtriestofigureoutwhatit'sdoing.So,forexample,

youseethatfile2650isannroff(Section45.12)fileandfoo.shisashellscript.

Itisn'tquitecleverenoughtofigureoutthat0001,visanRCS(Section39.5)

archive,butitdoesknowthatit'saplainASCIItextfile.

Manyversionsoffilecanbecustomizedtorecognizeadditionalfiletypes.The

file/etc/magictellsfilehowtorecognizedifferentkindsoffiles.[MyLinux

systemhasthefilecommandfromftp://ftp.astron.com/pub/file/,whichusesa

multiple-databaseformat.It'supdatedfairlyoftentounderstandnewfile

formats.—JP]It'scapableofalot(andshouldbecapableofevenmore),but

we'llsatisfyourselveswithanintroductoryexplanation.Ourgoalwillbeto

teachfiletorecognizeRCSarchives.

/etc/magichasfourfields:



offsetdata-typevaluefile-type

Theseareasfollows:



offset

Theoffsetintothefileatwhichmagicwilltrytofindsomething.Ifyou're

lookingforsomethingrightatthebeginningofthefile,theoffsetshouldbe

0.(Thisisusuallywhatyouwant.)



data-type

Thetypeoftesttomake.Usestringfortextcomparisons,bytefor

bytecomparisons,shortfortwo-bytecomparisons,andlongforfourbytecomparisons.



value

Thevalueyouwanttofind.Forstringcomparisons,anytextstringwilldo;

youcanusethestandardUnixescapesequences(suchas\nfornewline).



Fornumericcomparisons(byte,short,long),thisfieldshouldbeanumber,

expressedasaCconstant(e.g.,0x77forthehexadecimalbyte77).



file-type

Thestringthatfilewillprintifthistestsucceeds.

So,weknowthatRCSarchivesbeginwiththewordhead.Thiswordisright

atthebeginningofthefile(offset0).Sinceweobviouslywantastring

comparison,wemakethethefollowingadditionto/etc/magic:



0stringheadRCSarchive

Thissays,"ThefileisanRCSarchiveifyoufindthestringheadatanoffsetof

0bytesfromthebeginningofthefile."Doesitwork?



%fileRCS/0001,v

RCS/0001,v:RCSarchive

AsIsaid,thetestscanbemuchmorecomplicated,particularlyifyou'reworking

withbinaryfiles.Torecognizesimpletextfiles,thisisallyouneedtoknow.

—ML



12.7SquashExtraBlankLines

Readingoutputwithlotsofemptylinescanbeawasteofscreenspace.For

instance,someversionsofman(Section2.1)showalltheblanklinesbetween

manualpages.Tostopthat,readyourfileorpipeitthroughcat-s.(Many

versionsofless(Section12.3)andmorehaveasimilar-soption.)The-soption

replacesmultipleblanklineswithasingleblankline.(Ifyourcatdoesn'thave-s,

seethesedalternativeattheend.)

cat-smightnotalwaysseemtowork.Theproblemisusuallythatthe"empty"

lineshaveSPACE,TAB,orCTRL-mcharactersonthem.Thefixistoletsed

"erase"lineswiththoseinvisiblecharactersonthem:



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 12. Showing What's in a File

Tải bản đầy đủ ngay(0 tr)

×