apifert.blogg.se

Cat and grep command in linux
Cat and grep command in linux












cat and grep command in linux

For example, imagine you need to write code verifying that all content in the body of an HTTP POST request is free of script injection attacks. Use of regular expressions in the real world can get much more complex-and powerful-than that. For instance, using regular expressions, you could find all the instances of the word cat in a document, or all instances of a word that begins with c and ends with t. $ cat Documents/war_and_peace.A regular expression (also called a regex or regexp) is a rule that a computer can use to match characters or groups of characters within a larger body of text. $ cat Documents/war_and_peace.txt | grep Bolkonski | wcĬount all the number of times Napeleon is mentioned

cat and grep command in linux

$ cat Documents/war_and_peace.txt | grep Bolkonski | lessĬount all the lines which contain the word Bolkonski Or, use ‘less’ to page through the results: $ cat Documents/war_and_peace.txt | grep Bolkonski Show all the lines which contain the word Bolkonski Some more examples of what we have learned. To exit this less command press the ‘q’ key.

cat and grep command in linux

When you press the ‘b’ key you will go one page back. This way you can slowly page through long result lists. Pressing spacebar again you see again a next page, etc etc. When you press the spacebar the next page will be displayed. Your screen will now show the first page of War and Peace. With the less command you page through output. When experimenting with commands like cat and grep you might want to inspect intermediary results. To finish this tutorial I will learn you one more UNIX command: less. This proves ‘peace’ is mentioned only 110 times and ‘war’ 297 times! $ cat Documents/war_and_peace.txt | grep -i -ow peace | wc Now we can try to find out if War and Peace is more about ‘war’ than ‘peace’ by counting the number of times ‘peace’ is mentioned: $ cat Documents/war_and_peace.txt | grep -i -ow war | wc You can go this by adding the -i option to the grep command like this: What about ‘war’ at the beginning of the sentence like ‘War’ or someone shouting ‘WAR!’? To have a correct count we need to be case-insensitive. , then you will count all these ‘war’ lines. , then you will see many lines containing ‘war’, ‘war’, ‘war’ (one line for every occurence of the word ‘war’ in War and Peace). $ cat Documents/war_and_peace.txt | grep -ow war , then you will see the complete War and Peace in the output. You can experiment with these commands to better understand what happens. With the pipe ‘|’ symbol we send all the ‘war’ words to the wc command which will count the number of ‘war’-s. With the pipe ‘|’ symbol we send all the text of this document to the grep command, where we use the -ow option to search for the word ‘war’. With the cat command we read the document Documents/war_and_peace.txt. We count 274 occurrences of the word ‘war’ in War and Peace. $ cat Documents/war_and_peace.txt | grep -ow war | wc Type the following commands at the UNIX prompt and I will explain in a moment what happens.

cat and grep command in linux

You need to use a new UNIX command, grep, to do this trick. We can count the number of times the word ‘war’ is mentioned in this novel. The file Documents/war_and_peace.txt contains the English translation of War and Peace. And the third number counts the number of characters in Documents/war_and_peace.txt.įive hundred sixty-three thousand two hundred and ninety words counted with one simple command! This is the power of command line processing. The second number counts the number of words in Documents/war_and_peace.txt. The first number counts the number of lines in the file Documents/war_and_peace.txt. The output contains three numbers: 64620, 563203. We will use a pipe to count the number of lines, words and characters in a file with the UNIX wc command like this: With this pipe symbol ‘|’ you can glue the output of one command to another command. To use a pipe in a command you have to find a funny little key on your computer which has this sign ‘|’. In UNIX it is possible to glue the output from one command to the input of another command this is called a pipe. We provided cat command with one argument ‘Documents/war_and_peace.txt’ which is the filename that contains the complete text of War and Peace (this text I downloaded from the Gutenberg Project for you). “What was that?!” you might wonder? Well, that was the complete War and Peace running across your screen. With this command you can read War and Peace in 2 seconds! Lets try it out, type ‘ cat Documents/war_and_peace.txt‘ on the UNIX prompt and press ‘enter’. In our Catmandu project the cat command is our favorite. Today we will show you how many words there are in Tolstoy’s War and Peace. First, as always, you need to startup your Virtual Catmandu (hint: see our day 1 tutorial) and open the UNIX prompt (hint: see our day 2 tutorial). As a result you should see this on your screen:įirst we are going to learn you a new UNIX command, cat. Yesterday we gave you a little introduction into UNIX programming.














Cat and grep command in linux