 So I'm back into my home directory Let's go Into my its352 directory. What's in there my xyz dot doc? Make sure you have a file with a few lines in it. It doesn't matter what's in it I'll just check mine xyz All right, my file has some text in there one thing That we need to know is how to process text files So we've seen some commands for do dealing with text files displaying showing the head the tail word count It's very powerful if on a command line if we can automatically process text files to help configuring our computer So the next thing we're going to do is first search through a text file and One way to search through a text files called a program called grep And a simpler way to use it grep, then you specify a pattern What do you want to search for? Maybe I want to search for the word example in my file And grep will return all of the lines in that file that match that pattern In my file yours may be different. So maybe your Word you search for can be different depends on what's in your file Mine returns three lines inside the file which contain the word example. So very simple Search through the file return all the lines which contain that pattern we can Use wildcards there if we want what did I do there? That's a bad example Don't use a wildcard there. Let's avoid that one the grep So what I did there was a mistake for what we want to do grep has a number of options where we can specify regular expressions and use wildcards and other special characters to match different patterns But we will not deal with those right now It's take some time to look at the syntax. So let's Simply stick with searching through for particular words in a file for now So grep can search through files We can do an inverted search Find all the lines that don't match that word Again all the lines the minus V for inverted is all the lines that don't have that word and it shouldn't show the ones with example So a very quick use of grep to search for a file. We'll see some other examples later next thing Up until now all of the programs we've been running We've interacted with with those programs by running it and then they usually show something on the screen So Grep for example printed out the answer on the screen the screen is how we get the output of this program But in fact, we don't have to print it on the screen We can put it inside a file So we're going to look at how we can put the output of a program in a file rather than printing it on the screen And it's quite easy in fact Do that again Grep I want to search for all the lines in my file which don't contain the word lines If I do that Those lines are printed on the screen What I can do is redirect the output of that program using the greater than sign Into a file. I can name it what I like my output txt Run it see what happens It doesn't print anything on the screen the grep program run runs But the output of that program is redirected from the screen Which is called the standard output the standard location to put the output and is instead sent into a file Now let's look inside our file my out We can use less Look inside your file see what you get You see the output of grep So this concept is called redirection We redirect the output of a command to some other location and the very common way We do it is redirect the output of the command from the screen to a file LS we know has LS minus L as an option. So the long output We can LS a particular directory LS minus L slash shows from the root directory We can also recursively LS a Recursive LS means show me the contents of that directory and its subdirectories and their subdirectories and their subdirectories It keeps going. That's what the minus uppercase R does with LS Let's list all the files on a hard disk. It may take some time to list them. You see It's going through the hard disk listing all of the files To stop that because I'm in too impatient. You can try control C Control C kills or cancels the the program from running That was LS minus L minus R uppercase R slash Maybe I wanted know all the files on the hard disk instead of show them on the screen Put them inside a file Now what's happening? Here are some errors being displayed the files themselves are not being displayed But the error messages are What's happened? when a program runs that program Produces two types of output the program produces output this the standard output from the program the normal output it it It produces when there's no problems But if there are error messages, they are also printed on the screen But the error messages are Considered separate from the standard output What you see on the screen now are the set of error messages from the command I run With redirection and the greater than sign it only just shows the normal output and puts it into this file The error messages are still printed on the screen The concept and it's on one of the slides on the the website The concept is with any command when we run a command there are in fact three ways three Interfaces to that command. There's the input to the command some commands take you or accept input from you We haven't seen any yet. We may see some later Some commands we can supply input. It's called the standard input Then the command runs and the command does something and it produces two type of output The normal output the standard output, which is the expected output LS shows a list of files Grep shows a list of lines that match some criteria But if there's errors, it may also print error messages. There goes my mouth and That's referred as the standard error so There are three interactions to a command the standard input the standard output and the standard error normally The standard input comes from what we type Both the standard output and standard error are printed on the screen after when the command runs That's what normally happens with redirection using the greater than sign The standard input is normal that doesn't change the error messages are still displayed on the terminal But with redirection with a greater than sign the standard output is saved in a file So instead of printing on the terminal it prints it into a file so we can look at it later It's very useful to get a record of what's happened You don't want to look at it as it goes. You want to look at it later redirect into a file There are different options You can redirect both the standard output and the standard error into a file note the difference Here's the ampersand character and the greater than With just greater greater than the standard output is saved to a file, but not the errors With this one both the output and the errors are saved into a file and there are other variations One of them The common one we'll use is this just greater than sign run a command. Don't print the output on the screen print it into a file Another one which I don't have on the slides if you use two greater than signs It appends to the file one greater than sign overwrites the contents of the file to Adds to the end of the exist of the file if it exists So let's try those two commands again the others with the ampersand will not try I think will not use Let's try a couple more redirections to file Just check that command. I run All files dot txt. It's only 30 megabytes In size that if we have a quick look at it Not all of it, of course It's the list of all files and directories on my hard disk Except for those which I didn't have permission to look at I'm gonna delete that if you created one delete it So you don't take up space We'll do a few more simple redirections and To do them. Let's introduce some other commands Who are you logged in as? If you forget who you are then ask the computer computer who am I? Okay, who am I tells you the current user you're logged in as in my other terminal I'm logged in as the instructor. Okay. There are different users on here We can redirect the output of who am I into a file? All right, so instead of printing on the screen. It just goes inside that file another command echo echo Echoes that string onto this the terminal very simple Whatever we type after the echo is printed Does nothing other than print on the screen? It's like a printf statement in a in a programming language or a print statement So we can echo into a file and if we want to append to a file instead of instead of overwrite it Use two greater than science name already contains student and Now I'm gonna append to the end of that Hello Two greater than signs echoes one of them overwrites just to confirm that if you just use a single greater than sign You'll delete what was in the file before So the greater than sign the two greater thans and there are some other variations are called redirection redirect especially the output to a file next and another new concept is That when we run commands Often we'd like to do more complex things than than what we've done so far and we can do that by running multiple multiple commands Not just one after the other but run a command Take the output of that command and use it as input to the next command and this is called a pipe Combine multiple commands together using a pipe Let's see some examples What's my IP address? I told you at the start ten dot ten dot sixteen dot two oh one. What's yours? Do you know the command to find it? Who am I tells you who you're logged in as your IP address is which is not the topic for today? I think we may have seen it last semester. You may have seen me use IF config You don't need to remember that yet, but it's useful for the example IF config shows you details about your interfaces and If you scroll up, you'll see your IP address somewhere Which interface does it has an IP address for you? I don't have scroll on my terminal. So I will scroll up here. I have config. I note eth zero ten dot ten dot sixteen dot two oh one and I note that's the internet address I net address I Don't want all this other information. I just want my IP address So what we can do is maybe try grep run IF config and take the output of IF config and Then use that as an input to grep searching for I net address try that where the Way to combine the two commands is this vertical bar which is shift back Not backspace. What's that backslash backslash is the key shift backslash This is called the pipe character It means take the output of the first command and use it as input to the next and It returns all lines which were output from IF config which match I net ADDR and We see to there Can we improve upon that? What if I just want the IP address Now let's run IF config again But we can run IF config with a sub option with the specific interface. I want This is a little bit outside of the scope today. We'll cover it later But with IF config it shows I have ETH zero one two and LO I'm looking just for ETH zero So I have config ETH zero and now grep into I net ADDR the internet address and I get a single line Which includes my IP address plus my broadcast address and a network mask. I just want the IP address So let's try and do some operations on that single line. There's different ways to do it We can take that output and pipe it into another command. This other command we can use one is called cut cut Takes a string of text and splits it into chunks where we can specify the delimiter let's say is the Colon character that is the chunks are separated by that character and The chunks are actually called fields This is going to take This line of text and cut it up into fields separated by the colon characters So this would be field one field two field three I Want field two? That's what the minus F does What do we get? Can we do another cut and just get the IP address? Let's pipe that into another cut the delimiter. What is a space maybe? field one Now I've extracted my IP address from I have config and it's and removed all other information there may be better ways to do that but and it's May only work in certain cases, but it's an illustration that we can combine multiple commands together Using the pipe operator that vertical bar and in doing that I've introduced this new command of text processing called cut and the a simple way used to use cut We specify the thing that separates the fields the delimiter And it can only be a single character and then we specify the fields that we want and Cut is given on the the Linux reference sheet as well Many commands can be combined using the pipe operator As long as one produces output and the other takes some input What's the port number used for a web server? Remember a common quiz question not this week later weeks the port number for a web server Not 50 stars with an eight eighty Different applications have different port numbers some we won't remember on the Linux operating system. There's a file that List the set of port numbers. It's in the ETC directory, and it's the file is called services Again, this is outside of the scope for today, but we'll use it as the example We can see the port numbers of different services secure shell servers use port 22 HTTP if you scroll down uses port 80 and others Queue to quit maybe we can search through Rather than having to browse through let's look for all the lines that contain HTTP And we get well we get a few extra lines that we don't really want here If we want to fill it out we can actually specify the specific word of HTTP It gets rid of some and then we could actually we don't even need the word we could Pipe that into a and get rid of the comments for example Separate the fields by the hash character grabbing only the first field Maybe you want to sort them So all I'm doing is building up one larger command by combining them a smaller ones with pipe operations sort the output HTTP dash old becomes before HTTPS Maybe we just want to cut get rid of everything before the after the slash So just another example of building up and getting information from a text file, especially using pipe grep cart sort and some of the other text operation Commands can be used in the same manner