This NHPR episode 1900-197 entitled Introduction to FN, Part 3 and in part of the series, Learning FN. It is posted by name Maurice and in about 64 minutes long. The summary is, looking at some more said commands and just MS-M. This episode of HPR is brought to you by an honest host.com. Get 15% discount on all shared hosting with the offer code HPR-15, that's HPR-15. Bit your web hosting that's honest and fair at an honest host.com. This is HAKA Public Radio. My name is Dave Morris and I'm talking this time about said and this is part 3 of the series I've called Introduction to said. The last episode we looked at said at a more advanced level, perhaps most people have delved into, certainly there was a point where I hadn't gone much further than that. We looked at all of the command line options that were going to cover, so there were a few more that you can look up in the manual if you want to. We concentrated, continued concentrating on the S command for substitution. We did this in a bit more detail and we dug very deeply into regular expressions. So it's time now to move on to more said commands and give you some examples on how to use them. So as I said we've concentrated on the S command because really that's the command that most people want to use on the command line, but there are a lot more in said. Remember said's an editor. It's not a programming language as such. There are some things in said I have to admit which is very difficult to do and there's a point at which you wonder whether said is the right tool. I don't think we'll reach that point today in this episode, but in the next episode and beyond that will be a question. Let's look at some of the more commands within said and the first point about them is that they operate on individual lines or ranges of lines within the file that you're processing. So we need first of all to talk about line addressing, or the subject of selecting lines. Now put together a table of the different ways in which you can address a line. It is referred to as line addressing in the documentation. It's just a summary for reference, but what I'll do is I'll dive into the more detailed explanation in this talk. There's full show notes with this. There's pretty much always is with what I do these days. So you can follow along with them and hopefully use them as a reference point. They throw and if you want to get deeper into said, we want to use it more often. So one of the paradoxes with doing stuff like this is that when you want to demonstrate stuff, sometimes you need to come up with workable examples to demonstrate it with. And I've reached a point where I need to use a command to haven't told you about yet in order to really demonstrate stuff. So there's a command p command. These are all single letter commands in said as you've gathered. Then what it does it prints the line or lines that we've addressed. It's similar to the flag that we saw on the S command in the last episode, but it's function is the same, but it's actually a standalone command as well. It makes sense to use the p command if you're using the minus n option to the command itself to the said command. And you remember that said by default prints everything that it processes, which is referred to as auto printing. Often we want to switch that off, as we've seen already. So first half of addressing then, selecting a line by number. So the address itself simply consists of a number and matches the line with that number in the input stream. So my example, demonstration of how you would do that is you would you would enter the command on the command line said space minus n e space, quote, single quote, one p, close quote, space, said underscore demo two dot txt. So what that says is, on line one, print that line. Remember I just said the p was for printing the line. So the line with the address is one. So it will just print the first line of that file. Because we have the minus n flag, it won't auto print the rest. Now, remember you can give said multiple input files. And it normally treats them as one continuous stream. So in that mode, the address, the numeric address, will match that line number once. So I have the many files you'd give it. There'll just be one line one in that stream. But this will be so last time. There's a minus i option and a minus s option. And when you use either of those, a said treats multiple input files separately. So I've given an example where I've used the minus s option. And I've asked for line five to be printed. I won't read this one out because it's fairly obvious. And I've given it two files, said demo one said demo two. And what you get is two lines printed out, each of which is line five on these files. So the same line because these two files are actually the same bit of text but one's longer than the other. So that's specific line addressing line number. There's also a way in which you can select every end line starting at a number. You have a need to use this myself. But I could imagine that it's probably just my lack of imagination. But anyway, this is a canoe extension. So it's not in standard said. And what you do is you specify a starting line number and then the size of the step to the next line number. So the lines are selected through the input stream by adding the starting point to end times the size of the step. So you actually enter this one by using the sequence starting number and then a tilde sign and then the step size. So one tilde two means start at line one, then step two to get line three, then another two to get line five. And so on. So in other words, every odd number line. So if you want to think of it in a arithmetic way, it's line one plus one times two. Remember two is the step and that's at where n is one and the answer to that is three. Then the next calculation is one, the starting line plus two times two, which is four plus one, which is five. So that's how it gets to line three, line five, then it would be seven and so on. I don't know if that if it helps to think about that way. Some people think better that way than I'm not one. But I thought I'd mention it anyway. I won't carry on with that because it's quite hard to visualize in speech, but it's written out that way in the notes. So if you used two tilde three as your address expression, it means start at line two and then step three lines to get to the next one, which should be five and then three more lines to get to eight and so forth throughout the file. Every third line throughout the input stream. I did use there is an example of doing this, but it's later on in the in these notes, example two, so I put a reference into that. The next type of addressing is selecting the last line of the file and in this case you use the dollar symbol as an address. The dollar symbol matches the last line of a five or more accurately, as I've said here, the last line of the stream of data. If you're giving it multiple files, then it means the last line of the last file. But if you use either minus i or minus s, then the files are treated separately and every file will have a last line. So example three later on in the in the notes shows how this would work. The next we are selecting by regular expressions. We have an address which consists of a regular expression. So you would you would type in slash a regular expression close slash as you're address. There's an example here which I put in which is said space minus n e, space open quote slash HPR in capital slash p space, said demo one.txt, what that's saying is print out every line which contains HPR and you'll see in my example that there are four lines which contain the string HPR and capitals. Now I could have introduced this subject of delimitors when we were talking about the S command, but I didn't want to complicate matters. But we're getting into complications now. So I'm going to have to talk about alternative delimitors. Normally we've seen regular expressions enclosed in slash characters. We've done that all the way up until now. But this can get difficult. Say you want to use a file path, containing slash as your regular expression. Then every slash you type in the regular expression needs to be preceded by a backslash. So it doesn't said, doesn't think it's the closing delimitor. So it's useful under those circumstances to use alternative delimitors. So when you do this, you have to proceed the opening delimitor with a backslash. So I've shown an example in the notes here. If you wanted to use something like ETC slash password as your regular expression, you would type slash ETC backslash slash password pwd slash. Okay, so they escape the slash that needs to exist in the middle. Or you could do backslash and then you're opening delimitor, which in this example is hash mark ETC slash pwd closing delimitor, which is a hash mark again. So they're both the same length. So there's no particular advantage using one of the other but you can see it's useful. It will be useful to have these alternatives. Now this, the need to put a backslash when you change the delimitor, is only the case instead when you are using regular expressions as addresses. If you use the S command and there's another command we look at later, there are other commands like this. I think it's just one more forgotten that takes regular expressions or expressions with delimitors. Anyway, if you use the S command, you don't have to put a backslash between your in front of your alternative. That's really because said can't easily, it's not really designed to be able to easily spot the fact that using an alternative delimitor when you just a straight regular expression. Other command systems, other languages can do this but said I guess due to its vintage doesn't work that way. So if I've shown an example where said space minus n e space open quote S and then a vertical bar is a delimitor, HPR vertical bar, banana vertical bar p, close quote space, said underscore demo one dot txt. So what that's doing is it's saying replace all this as the HPR with banana will be. So that works that's fine. If you in my second example use a backslash in front of that vertical bar, you get back an error from said says un terminated S command and when I tried this I wondered what's it doing. I wasn't quite clear about this until I experimented with it. What it's doing is it's it's assuming that backslash is the opening delimitor. So I included in here an example where the backslash is actually used as the delimitor. And it will be included in an example where the letter p is of the used as the delimitor and both of those work fine. But the backslash vertical bar one does not. And what's it going to include here the fact that an empty regular expression has a special meaning instead. Again we could have looked that looked at that in the context of the S command. It's the same there but I'm leaving it till just now so that hopefully it's not too much of a shock. What this means is that all this is said's why at least of representing the last regular expression that matched. So I've got an example that uses three S commands and the first changes the space on each line to an asterisk. The second changes the second space to an underscore and the third changes the third space to a plus. The second and third S commands have empty regular expressions so they use the previous matching one. So I've trying to read this out but you might be better just just checking it out in the notes. So I've got said space minus E space open called S slash space slash asterisk slash semi colon. So that's replacing the first instance of a space bar and asterisk. Then with one other S which is S slash slash so it's an empty regular expression underscore slash semi colon S slash slash plus slash and so that does the the three replacements for three spaces with the different characters space said underscore demo one dot txt and you'll see in the line that follows it that the three spaces have been changed in this way. There is a potential problem when you use empty regular expressions. I hadn't appreciated this until I read the documentation a bit more detail. I'm thought about it. One I've done is I've included the recommendation or the comment from the Genu said manual and it says that note that modifiers to regular expressions are evaluated when the regular expression is compiled thus it's invalid to specify them together with an empty regular expression. That makes sense because the regular expressions got to be stored somewhere so if you use one and then you keep referring back to it it's got to be held somewhere and the regular expressions are sort of a language so it is compiled in the sense of a programming language so that's something just to be aware of. I'll try and refer to this later in another show just to see if I can demonstrate it a little bit more but it seems a little bit too advanced for what we're doing just at the moment. So let's talk about modifiers in the context of regular expressions. There are two that we'll look at in this context and we've already seen in the S command you can use a capital I or lowercase I flag to mean that the regular expression part of the S command is not case sensitive. Well there's also an a capital I modifier, no lowercase I modifier for a dress regular expression and this modifier has the same effect so I've shown a simple example which goes like this said space minus n e space open quote slash lowercase hpr slash capital I p close quote space said underscore demo one dot txt. So what that's doing is selecting all of the lines that contain the string hpr but it's using the don't care about case modifier and so it's returning all of the ones which have got uppercate uppercate hpr. There are no lowercase ones so it's a bit of a daft demonstration I suppose but it makes the point anyway. Now there's another modifier to these regular expressions which is capital m. Now this is relevant to stuff that we're not going to be doing today. I thought I'd put it in because I wanted to keep all this regular expression thing together in the same place so it's relevant to text in the pattern space that contains multiple new lines well we've not seen how you you construct such a circumstance yet so we'll look at this in the next episode and the next thing about addressing is that you can also specify a range. A dress range matches the lines in the input data from a starting position up to and including that's an important point an ending position. The range is written as two addresses of any of the types you've seen separated by a comma. So my simple example is said space minus n e space open quote one comma three p close quote blah blah blah. Well it means is lines one two three inclusive print and I actually included the first three lines off the file to show you probably a bit of overkill there really but never mind. And I had a minus n option there to prevent automatic printing otherwise you'd have just seen those lines repeated twice but there are other forms so my next example is said space minus n e space quote slash up arrow capital W e we slash comma dollar p close quote so there are two expressions there one is the regular expression for the word we with the with the capital W to occur beginning of the line and the second in the range is dollar which is the end of the file so it prints from a line beginning with the word we up into the end of the file which I carefully chose so it was just the next line so you only get two lines in the example. The next one is said space minus n e space quote slash up arrow or so complex capital W h a t slash comma slash so complex or up arrow produced slash p. So this is a range where you're starting with the line that begins with the word what with the capital W and the first first position and ending with the line that begins produced again in the the first column and again that's some these are two consecutive lines just again to point proof point so we've seen you know numeric ranges we've seen regular expression and end of file range we've seen two regular expression ranges you can there are some extras that a canoe said provides in terms of ranges I'm not going to look at them in this series I think I might change them I don't this actually because it depends whether I stumble over an example that would be quite good to use them but for the moment I'm not going to mention them this episode's going to be pretty huge anyway if I need to I'll refer to them later on in the series so the next topic under the addressing heading is the process of what's referred to as negating an address match so all of the address types that we've seen so far can be quotes negated I'm not sure that's a best word but it is what's used in the canoe manual so I'm sticking to it so for example using a line number so you're looking for example for a given line you want to print it if you negated that means to match all the lines other than that one all lines but the selected line and you you perform this negation by adding an exclamation character after the address in the long note there's the first example example one contains a number negation example and in this this particular bit of the notes I've got a demonstration where it's we're using the addressing form where we match every in-th line starting at a specific line which is the number tilde number form so I've got an example here where there is we're using said space minus n e space open quote two tilde two exclamation mark p we're doing that against said underscore demo one dot tx t so what that will do is it will print all the odd number lines in this file which has got 13 lines I think you've seen this file often enough so I've not actually included it in the in the notes if you don't negate then you get all the even numbered lines so you're getting the the logical opposite topic I guess is one way putting it if you negate the last line of the file using the dollar then it means all lines except the last line negating a regular expression on the other hand means all the lines that don't match it so there's an example here said space minus n e space open quote slash square bracket open square bracket capital A high-fin capital z slash exclamation mark p so what that will do is it will print out all of the lines of the file which is the good old said demo one again it will print out all the lines that don't contain an a capital letter so I did another example here using the same idea just to really try and add some more examples of things doing stuff the said things doing stuff to uh to file and this one may be desired again said space minus n e space open quote slash open square bracket capital A high-fin capital z closed square bracket slash exclamation mark so that's the that's the addressing thing that you want to do and that's then followed we haven't seen this before but it's logical um by an s command so we've got s slash back slash b remember that's the word boundary slash double u that's a word character slash that's the end of the regular expression then as the replacement part we've got back slash u which means do an uppercase to the thing that follows uppercase the first letter of it and we follow that with an ampersand which means everything we matched in the regular expression you want that in the replacement so the effect of that is to find all word boundaries in the line so the starts of all words and then replace the thing that comes back just be that one letter of each word with its uppercase equivalent and then after the closing slash of the replacement we've got a lowercase g and lowercase p we're doing this on said demo one again so what we see then is it finds all the lines that don't contain capitals then it goes through with the s command replacing all words with an uppercase version capitalised version of the of the same word so you get back two lines and it's got everywhere to got a capital first letter I don't know quite why you'd want to do that but you never know I just wanted to emphasize to how you can associate addresses with all of the the various commands pretty much always maybe one or two that don't take addresses on front of them but the general rule is that commands can be preceded by addresses all the times we're using the s command in episode one two we didn't know about addresses yet I decided not to introduce addresses until this episode because it seemed you know if you didn't want a copy on episode three then you'd to have enough said to to survive with so if negation is used with an address range then it applies to the range it's not possible to negate the individual addresses in the range but quite sure what that would mean because yeah you can only negate the whole thing so what that means is match all the lines outside the range so I've got an example of how you what sort of such a command would look like and it's reiteration of the one which starts with the word what and ends with the word produced we did it a little bit earlier except I've popped a negation in that after the second regular expression so it will match the rest of the file except for the two lines that we saw in the last instance of this example so I'm hoping that you'll if you're not clear about any of these you'll be able to just go and type them in yourself and see what happens the said demo one is obviously available for download I think we saw that in episode one so that's addressing under the heading of other commands so I needed to do tell you about that before I started on the various other commands that we're going to look at today so we're now going back to to commands and the like and the the topic now is comments in scripts and it's possible to add comments to a said script it doesn't really make a lot of sense when you're building a script on the command line and it's more appropriate when you're putting stuff in a file the hash mark is used for this so the hash character begins a comment and then the comment continues to the end of the line the new line in other words no I thought what I do would be to add in this point a reference to the fact that you can build said files we've seen how you can put commands in a file but I've got an example here of how you can create a file which becomes executable you remember hopefully you're aware that if you put the so called crunch bang which is a hash mark in an exclamation mark on the first line of such a file and follow it by the path to the actual them command then the and you make it executable the unit system or the unit system will if you call it as if it's a program will invoke that particular command interpreter whatever it is to process it so the same applies to set so the first I've got an example here where I'm using cat a cat command and it's redirecting standard in into a file I'm calling demo dot said now the first line this is this is me typing in these lines one trying to represent here is hash exclamation mark slash bin slash said as the path to my said it might be different with you but that's the sort of generic place that said lives follow that with a space and a minus f you need to have the minus f because otherwise said is not invoked in the mode to read a file it's going to read the very file that this hash bang line has just been inserted so my next line in this file is a comment hash space b and then one three three seven which is lead speed for lead the be lead is what I typed here then there's just one expression one come on in this file and it is s slash hacker with a complete slash and then the the lead speed version of hacker which is h4x0 r x or I suppose you say it yeah I don't know close slash g so that's going to find all instances of hacker and place it with the replace it with the lead version then I represent the fact that I would have stopped feeding stuff to this file through cat by putting in a control d so you'd actually press control d as the end of file signal to cat and the next command is ch mod this is the thing where you can change the permissions on a file and I'm using the the simpler form u plus x that is I want to give execute access to to me or whoever's doing it follow that with the space and then the name of the file demo dot said and the next line is me invoking the command that I've created or the command file dot slash it's in the current directory so dot slash means it's here dot slash is the current directory demo dot said space said underscore demo one dot txt so it's running this which is just a simple thing but you know what I'm just making the point and it runs and it finds hacker on some so I find it lots of times I don't think I put all of the instances in I don't know why I didn't add them all anyway you can see for yourself in the notes what's happened it's changed hacker into its lead form so that's how you add comments now let's look at the quit command and this consists of the single lowercase cube and that causes said to exit you might say well said exit anyway so yeah but this a lot of this causes it to exit early it can take an address but only a single address meaning exit when this line is reached it's not really makes sense to make sense to make it to range would it quit on every line in this range no but I'm assuming I'm actually testing this the single address means anything like a number a dollar or a regular expression and exit yeah I'm pretty certain it does like I said and I actually test this the current patent space is printed unless you've got the minus n option which stops it's it's a form of water printing so it's being prevented if you have that option so I've got an example here which is doing another one of these lead speak the thing is and what this one does it replaces radio with what I assume is the lead form r4d10 and it does it well let me read the thing out because I don't want to summarize it too much it's said space minus n e space open quote s slash radio with a capital R slash capital R4d10 slash GP so that's going to do all instances on all lines and it's going to print it after it's done then the next command is separate by semicolon all in the in the same quotes which is 3Q close quote and we're using said demo one dot txt so it will find hack above it radio and change it and there's two instances of radio on that line then it will do radio free camera current fix that binary revolution radio from the next line and then it will reach line 3 and we've been told don't print it because we've got minus n and just quit so it doesn't go any further there any other instances of radio it will ignore them because you it's avoided the said session I should have said that the the Q command in Genus said has been enhanced to allow an exit code on the end so if you were writing something in a bash script for example you could capture the the termination code when said quit it and you could take some action on the basis of which which quit was was initiated or something like that never done it myself but you could do if you wanted to there's another example of the exactly the same thing without using Q which where the the expression I won't read the whole line is open quote one comma three s slash radio by r4d10 slash GP close quote so that says just do the replacement on line 133 why would you use the Q command why would you bother well it came up a while ago in the context of looking for stuff in a big file if you have a gigantic log file for example and you want to pick out a given line and do something with it or give a pick out a range of lines etc and that range of lines or the given line matching line occurs fairly early and you still got several million more lines to go through you don't really want to leave said to it just walk through the rest of the file and not find it if you know there's only one instance of the one range of this particular thing in the file it makes a lot more sense to say when you've done that just quit so that's where it can be useful said you come on prompt will come back much quicker as a consequence of doing that there will be examples later I'm hoping I haven't written them yet that use in maybe episode four or even five that use these types of techniques and demonstrate such things so the next command we have in this list of fairly commonly used said commands is one that deletes the patent space it's the lowercase d it at least the patent space and causes said to start the next cycle by reading the next line if there is one that is so this command can be proceeded by any of the addressing types that we've seen earlier on and the effect is to emit the lines in question from the output stream effectively deleting them into you moving from one file to another effectively so my little example here is one which will emit stroke delete all lines beginning with the capital H so I've got said space minus E is no end here space open quote slash circumflex capital H slash d close quote space said underscore demo.txt so all lines begin with h will not be shown now why is there no minus n you might ask well what we're doing here is we're just letting said walk through the file and print automatically everything that it sees except when it the the script sees a line beginning with h it deletes it so it deletes it from the patent space and auto printing is the process that happens at the end of a cycle when there's no more commands to apply to a line it will print the patent space well it won't be a patent space when there's a line starting with an h I'm saying this because I had to think twice about it myself so I've put another example which uses negation with an exclamation mark so taking the exact of the same command putting an exclamation mark before the d that will delete all lines that don't begin with an h so you end up with all the all of the lines that match that next one is the p command we've already seen which is to print the patent space it's equivalent to the p flag that we saw with the exc command but it's a command all by itself lowercase p and we've seen several times now and it can be preceded by any of the addresses that we've seen it's only relevant when using the minus n option to switch off auto printing if you leave that on and you just see the same lines twice because they get auto printed as well as being printed by this so for example to print lines one to five of a file you would do said space minus n e space quote one comma five p quote in the name of the file and that's equivalent to the head command you head minus five will show you the first five lines of a file so said can do the same thing a bit more typing it's the last command I want to talk about today and it's a command it's a little bit odd it's classified as a commonly used frequently used command I have to admit that I have not used this in my previous usage of set of use it now since I've learned about it but but I couldn't immediately see the use for it but I've come up with an example of it later on it's the end command it's a single n and it can be it can be preceded by addresses if auto printing is enabled that is there's no minus n then it prints the patent space so it doesn't override the minus n it prints it and then goes and gets the next line from the input stream you say well why what was yeah well that was my thought as well I copied in the description from the GNU manual and I hope to this might make more sense and perhaps I'm making let me read it to you if auto print is not disabled print the patent space then regardless replace the patent space with the next line of input if there's no more input then said exits without processing any more commands so not completely sure why you've used that but I've got an example coming up in a moment so which I think is valid we're probably going to look at this in more detail in later later episodes I should say actually what I'm planning to do is to look through some of the more hairy examples of set which are available in the GNU documentation and try and explain them I will work out how they work first but that's my plan is to explain them to you and demonstrate the useful these commands that way okay final point then in this episode before I go through the examples and that is the subject of grouping commands you might want to perform several said commands on a given input line or set of lines so you need a means of grouping them well it's done by using open brace or open curly bracket and close curly bracket around the set of commands that you want to group together so I've got a little example which does this just just to make the point really and it's a command line example which uses a range of addresses the same range we saw before the line that begins with the word what the capital W and up to the line that begins with produced on column one so the thing that follows that range is an open brace open curly bracket followed by an S command so the S command is S slash circumflex slash greater than sign back slash t close slash what that means is replace the start of the line there's no actual physical characters there remember the the circumflex means the start of the line it's the anchor for that so the effect is to put stuff on the front of the line and the stuff that I'm putting on the front of the line is the greater than sign followed by back slash t which is a tab character so this S command is then followed by semicolon p and then close curly bracket close brace so for each line in this range I want to put a greater than sign and a tab and print it and it's just since we've got a minus n flag minus n option we are not seeing any other lines you'll see that what happens is the upper two lines that we saw before in the other variants of this example with the start propended to it so my final demonstration of grouping is as I put in the notes fairly useless it's just a demonstration of what can be done it shows the contents of a command file and I've included this file in the on the HPR site it's called demo two dot said and what it's doing is it contains two groups it's quite complex actually to read out it's got two groups and various addressing to a two regular expression addresses the purpose of it the first regular expression matches the line that contains the letter A followed by B within five characters and then C within five characters and the second regular expression is similar but matches only A and B with with up to five characters in between them the first group uses four S commands to mark the line with G1 short is processed by group one and then it uses other S commands to highlight all of the characters A being C by putting square brackets around them to make the stand out then it's going to print the result the second group does the same it uses G2 to signal what's there and just highlights A and B so rather than just go through this line by line I thought if I'd try and explain the the components in a general way let's look at the first group the regular expression is lawyer case A dot back slash curly open curly bracket one comma five back slash closed curly bracket that's the first bit of it so what that means is a letter A followed by any old arbitrary set of letters from one to five right one to five arbitrary letters then after the after that comes a B dot back slash open brace one comma five back slash closed brace then a C so saying A followed by any arbitrary characters from one to five a B followed by any arbitrary characters from one to five followed by a C so just looking for the sequence A B and C in a line with a most far characters between them so after the regular expression is an open brace open curly bracket which starts the group and the group simply consists of a bunch of S commands so the first one is S slash circumflex slash G1 colon space closed slash so it's saying put G1 on the start of the line then we've got S slash A lower case A slash open square brackets A close square bracket A slash G so all instances of A replace by an A in square brackets then the same for B and C three three different S commands follow that with P this is being done in the assumption that you're running said with an a minus N option that only these lines are being printed and now I then followed that with an N command the N command is there to ensure that a given line if it matches a regular expression is not then processed by the other regular expression since there are two and they sort of overlap one another so the second ring of the expression and group in this file is almost the same except that it's using an A followed by an arbitrary number of characters followed by a B and if you can hear the cat in the background she's always about there was intruding yeah you're I'm talking about so anyway the contents of this group is slightly different in that it it pre-pens G2 on the front of the line and it only deals with A's and B's it's also got a P and an N in it so I've got a example of how one would run this and you do it by calling the command said split space minus N F and then space demo two dot said space said underscore demo dot txt so we're just produces two lines the first one begins with G2 so it matched the second group and it's got A's and B's bracketed in square brackets the second line begins with G1 and it's got A's B's and C's bracketed so it's complex but you can fairly useless but you know this is all about learning to do bizarre things in order to learn I hope anyway so as I say the demo file is is available if you want to mess around with it and there are links to it in the in the notes okay let's have a look at the examples I'm gonna zip through some of these fairly quickly because they're fairly simple for example one it's a couple of scripts which print all but line one of a file the file being the famous said underscore demo one dot txt and one way to do it is to put in the expression one exclamation mark p meaning print everything but line one the other way of doing it is to use two comma dollar p which means print from line two to the end of the file so that's just to really make the point that the two things are complementary sort of logical compliment is that the right term example two is the demonstration of the first tilde step form of addressing the thing we start with a number a line number and then go steps which is the second number represents the one I've done is I have set up a little example to look at line one started line one and then go in steps of five and I thought it might be useful if you could actually see the line numbers that it generates so what I did was I created a script for an example a pipeline really which uses the nl command nl is for numbering lines so I used nl space minus w3 that means I want each number to be three characters wide minus ba which is I want you to number all lines including blank lines normally it doesn't do this it it it skips blank lines with numbering number coins to that but anyway then the file we're working on is said underscore demo one dot txt pipe that into said space minus n e space quote one tilde five p close quote so what you see is lines one six and eleven of the file and I made the note that if we use one tilde five exclamation mark p that gives you the compliment of that the reverse of it or the negation of it we'd see all lines except one six and eleven look that helps I certainly would have appreciated that in an example myself so I hope you find useful example three is the demonstration of the dollar address last line of input thing and I think I've already demonstrated this elsewhere tonight what I've done is to examine two files said demo one and said demo two which really the same file ones just got more texting than the other so what I've asked for is dollar p which is print me the last line so running said space minus n e space quote dollar p quote then the names of the two files that way they all appears one stream and I see the last line of the last file contribute one show a year as the last line if on the other hand I use an s minus s in that list of options so it's minus s n e I usually concatenate them to save type in all the high-fins and spaces and then I've got quote dollar p quote the two file names again then I see two lines one is detail on a topic and then X one is contribute one show a year so the first line is the last line of the first file second line is the last line of the second file that's of course we use the minus s option which sees the files that separate you remember that I'm sure example for demonstrates the regular expression form of addressing just uses a simple regular expression which is it's looking for the the string long ln g so we've got said space minus n e space quote slash ln g slash p quote and we're using said demo one and we see three lines coming back which have the word long in them so it's a bit like using grep on the file a little bit more typing them now that example uses the forward slash characters as the most examples we've seen remember I mentioned that you can change those to something else if you want to so the second example in this second instance in this example so for example I don't know what you call it is said space n e minus n e sorry space quote back slash hash ln g hash so the hash is the alternative to limiter but I had to introduce it by a back slash so that said new one I was doing so that's pretty simple example five shows an address range and I'm using a few other bits and pieces here I'm using the range one comma slash hpr I've got lower case hpr followed by after the closing slash a capital i so remember that means in this regular expression don't bother about the case and then I've got a piece so the whole line is said space minus n e space quote one comma slash hpr and lower case slash capital i lower case p quote and then the file is said demo one and I get back two lines hack a public radio is the one has got hpr in it that's the first line of the file I think and then there's another instance of hpr that comes back and they're both in capitals we saw that before pretty much so the second part of example five uses a range or we're using two regular expressions and so I've got I'll just read it out maybe and explain it after said space minus n e space quote slash circumflex what wht in lower case slash capital i comma slash circumflex produced all in other case slash capital i p close quote and it said demo one what I get back is five lines which are the lines between the line beginning with what and ending with produced but I've told said that I don't care what the case of the two actually is now what's actually happened here and it's really hard to see as I realize when I did this is that it's found two instances because it doesn't care about case said it's found two instances of a range beginning with what and ending with produced or at least beginning with what so I thought well it would be nice if there was a way of putting a line number on the end of everything on the end obviously because you need to you need to do that if you do it before you give it to said otherwise it will mess up the regular expression of things so I digress and used a bit of another facility another command called or kwk which has the capability of processing text and reform adding into it very things I won't explain what it's doing it might be good idea if I think about doing an orc series at some stage or somebody else should do one perhaps anyway the orc call takes every line of the file and puts a line number on it in brackets and puts it right at the far end after column 75 and then I feed that and this is said demo one is usually I feed that to said using exactly the same expressions I did in the last time but now you can see that it's returned lines 7 and 8 which are the ones that begin with what and end with produced and then it finds another what on line 11 and it carries on through to 13 it never finds produced because there isn't another one but it hits the end of the file at that point there's 13 lines along if you recall back to episode one sure if you remember that so that's why it's doing this I thought it might be useful just for you to be able to visualize what is up to. Example 6 is me having a go at Ken's text in the package which is in said demo 2.txt the grammatical mistakes he gave me a hard time in the last community news about doing this to his text but it's done with the best possible taste of course so I've got a file of commands called example underscore 6.cd and it's included with this episode and this is an example of a group and the group in question is controlled by a single regular expression it's fairly silly it's just there for demonstration purposes really the group consists of slash circumflex space asterisk dollar slash and it's followed by an exclamation mark meaning not that so what I'm looking for is any line which contains zero or more spaces and that's all so the show is got a start of line in an end of line marker so I'm happy to cope with any lines which contain spaces as well sometimes that happens and you can't see the spaces necessarily this you look very closely with an editor or something and I've said do this for all lines which are not blank it's a sort of make work type of exercise really because there's no really need for it but it demonstrates it anyway and in it I've got a bunch of s commands which are changing the various grammar things that I hammered on about in episode 1 and so on you can do this yourself if you want to or mess around with them and do other things of them so that you would invoke this with the command said space minus f space example underscore six dot said space said underscore demo two dot txt so this should process the entire file is there's there's some some quote corrections in there that are not really corrections just me messing around with the text to make it suit my taste I suppose so example seven is a case of an executable said script and we looked at this idea before and what I've done here is to show you the contents of this thing it's another file that's being included in this episode so the the demonstration what it contains shows me typing the the text in cat greater than example underscore seven dot said which is the name of the file so I would then type in a hash an exclamation mark slash bin slash said space minus nf we never saw minus nf last time did we think we just did a minus f for this one applies the the do not auto print flag as well so when you run this it will never print anything unless the the said commands themselves cause it to to print think I did that don't think I did that before would do that so what I've got here is a single regular expression address and it consists of circumplex dot back slash open brace open curly bracket 75 comma 80 slash closed curly bracket dollar slash so interpreting that what it means is any line which has got between 75 and 80 characters on it between the start of line and end of line the m those two anchors so I'm interested in any of those lines then inside the group that is controlled I have an s command which says s slash dollar slash then five spaces slash what this is saying is when you find such a line add five spaces to the end of it then the next desk command is s slash circumplex back slash open parentheses dot slash open curly bracket 80 curly bracket back slash closed parentheses dot asterisk slash so that is an s expression an s command which with a rather nasty regular expression inside it the heart of this regular expression is dot a dot followed by one of those curly bracketed numbers so it's saying any character any 80 characters and the 80 characters are to be candid from the start of the line which is why there's a circumplex the 80 characters are enclosed in these parentheses which cause it to be a regular expression group and the rest of the line I'm care about though it's actually referred to with the dot asterisk at the end so the replacement part of this s expression is vertical bar back slash one vertical bar slash to the whole expression is saying the whole command is saying find me a line while we've already found a line and we on this line take the first 80 characters starting in the beginning of the line and put vertical bars around them I know there's going to be 80 characters there because I've deliberately padded each line with five characters so it'll either be 80 characters long because I selected at the 75 to 80 range it'll either be 80 characters long because it started at 75 or it will be somewhere between 80 and 85 characters long so I can definitely chop off 80 characters off the front of it and put vertical bars around the last command in this group is p so we're printing the result okay so this is an executable file and I've shown a bit later on having explained what it contains ch mod u plus x we saw that before make it executable and it's called example underscore seven dot said as I say in the next line is dot slash example seven dot said and we're running it against said underscore demo one dot txt so you get back four lines which have vertical bars at the beginning and the end and they're all in a nice neat line vertical line okay so that script is available for you to play around with if you wish it's another one of these sort of useless fairly useless things but it's a it's a fun demonstration of what can be done all right with that that's the end of this episode I hope you found it useful okay bye you've been listening to HECK a public radio at HECK a public radio dot org we are a community podcast network that release the shows every weekday Monday through Friday today show like all our shows was contributed by a HPR listener like yourself if you ever thought of recording a podcast and click on our country building to find out how easy it really is HECK a public radio was found by the digital dot pound and the informomicon computer club and it's part of the binary revolution at the rev.com if you have come in some today show please email the host directly leave a comment on the website or record a follow up episode yourself unless otherwise status today show is released on the creative comments attribution share a light for the dot org license