1 / 53

fgrep

fgrep. fgrep searches for a string in a file. 8 lines match. But some are for “rudder” and some are for “rudderfish”. % fgrep rudder Verne.txt the helm shout, 'Our propeller and rudder are smashed!' " being undone by its rudder -- couldn't return to leeward after us.

kiet
Télécharger la présentation

fgrep

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. fgrep fgrepsearches for a string in a file. 8 lines match. But some are for “rudder” and some are for “rudderfish”. % fgrep rudder Verne.txt the helm shout, 'Our propeller and rudder are smashed!' " being undone by its rudder--couldn't return to leeward after us. turns on a horizontal plane, I use an ordinary, wide-bladed rudder geared to rudder cables running to the Nautilus's stern. rudder separated from the sternpost and still hanging from an so as not to miss its prey, went ahead or astern, obeyed its rudder, anal fins; black-tinted rudderfish that you catch by using torches, thin cries, black rudderfish like those I've already discussed, % fgrep rudderfish Verne.txt anal fins; black-tinted rudderfish that you catch by using torches, thin cries, black rudderfish like those I've already discussed, % fgrep "black rudderfish" Verne.txt thin cries, black rudderfish like those I've already discussed, %

  2. fgrep fgrepsearches for a string in a file. % fgrep rudder Verne.txt the helm shout, 'Our propeller and rudder are smashed!' " being undone by its rudder--couldn't return to leeward after us. turns on a horizontal plane, I use an ordinary, wide-bladed rudder geared to rudder cables running to the Nautilus's stern. rudder separated from the sternpost and still hanging from an so as not to miss its prey, went ahead or astern, obeyed its rudder, anal fins; black-tinted rudderfish that you catch by using torches, thin cries, black rudderfish like those I've already discussed, % fgrep rudderfish Verne.txt anal fins; black-tinted rudderfish that you catch by using torches, thin cries, black rudderfish like those I've already discussed, %fgrep "black rudderfish" Verne.txt thin cries, black rudderfish like those I've already discussed, % Only 2 lines match the longer string “rudderfish”.

  3. fgrep fgrepsearches for a string in a file. % fgrep rudder Verne.txt the helm shout, 'Our propeller and rudder are smashed!' " being undone by its rudder--couldn't return to leeward after us. turns on a horizontal plane, I use an ordinary, wide-bladed rudder geared to rudder cables running to the Nautilus's stern. rudder separated from the sternpost and still hanging from an so as not to miss its prey, went ahead or astern, obeyed its rudder, anal fins; black-tinted rudderfish that you catch by using torches, thin cries, black rudderfish like those I've already discussed, % fgrep rudderfish Verne.txt anal fins; black-tinted rudderfish that you catch by using torches, thin cries, black rudderfish like those I've already discussed, % fgrep "black rudderfish" Verne.txt thin cries, black rudderfish like those I've already discussed, % 1 match for “ black rudderfish”. Notice that we need the quotes ("..."), or else the multi-word string would look like separate arguments.

  4. cut cutand paste columns of characters (-c) or of fields (-f) from the inputted lines. Some other useful flags are --complement and -d % cut -c 4-5,10 abcdefghijklmno.txt dej % cut --complement -c 4-5,10 abcdefghijklmno.txt abcfghiklmno % cut -f6,7,8 --complement filelist … % cut -f2 fruits.txt apple banana cherry % cut -d " " -f2 fruits.txt banana

  5. cut cutand paste columns of characters (-c) or of fields (-f) from the inputted lines. Some other useful flags are --complement and -d % cut -c 4-5,10 abcdefghijklmno.txt dej % cut --complement -c 4-5,10 abcdefghijklmno.txt abcfghiklmno % cut -f6,7,8 --complement filelist … % cut -f2 fruits.txt apple banana cherry % cut -d " " -f2 fruits.txt banana

  6. cut cutand paste columns of characters (-c) or of fields (-f) from the inputted lines. Some other useful flags are --complement and -d % cut -c 4-5,10 abcdefghijklmno.txt dej % cut --complement -c 4-5,10 abcdefghijklmno.txt abcfghiklmno % cut -f6,7,8 --complement filelist … % cut -f2 fruits.txt apple banana cherry % cut -d " " -f2 fruits.txt banana

  7. cut cutand paste columns of characters (-c) or of fields (-f) from the inputted lines. Some other useful flags are --complement and -d But wait! I asked for field 2. If there was only one field, then shouldn’t there have been no output? % cut -c 4-5,10 abcdefghijklmno.txt dej % cut --complement -c 4-5,10 abcdefghijklmno.txt abcfghiklmno % cut -f6,7,8 --complement filelist … %cut -f2 fruits.txt apple banana cherry % cut -d " " -f2 fruits.txt banana That would have made sense, yes. But cut doesn’t make sense. If there are not enough fields, it just prints the original line. Actually, what happened here is that the line of the file only had one field (because it had no tab characters).

  8. cut cutand paste columns of characters (-c) or of fields (-f) from the inputted lines. Some other useful flags are --complement and -d % cut -c 4-5,10 abcdefghijklmno.txt dej % cut --complement -c 4-5,10 abcdefghijklmno.txt abcfghiklmno % cut -f6,7,8 --complement filelist … % cut -f2 fruits.txt apple banana cherry %cut -d " " -f2 fruits.txt banana

  9. Connecting commands by redirection Suppose we want to count the number of files beginning with “A” Thels command can list these files, but it can’t count them: % ls A* ABCD Afile APROG.c AZZZ ABD.txt AFILE2 APROG.x ACE AFILE3 Aqrs.txt Thewccommand counts things, but how to make it count these things? % wc –l ???? •We can solve this by redirection: % ls A* > tempfile % wc –l < tempfile 10 % 9

  10. Connecting commands by redirection Suppose we want to count the number of files beginning with “A” Thels command can list these files, but it can’t count them: % ls A* ABCD Afile APROG.c AZZZ ABD.txt AFILE2 APROG.x ACE AFILE3 Aqrs.txt Thewccommand counts things, but how to make it count these things? % wc –l ???? •We can solve this by redirection: % ls A* > tempfile % wc –l < tempfile 10 % 10

  11. Connecting commands by redirection Suppose we want to count the number of files beginning with “A” Thels command can list these files, but it can’t count them: % ls A* ABCD Afile APROG.c AZZZ ABD.txt AFILE2 APROG.x ACE AFILE3 Aqrs.txt Thewccommand counts things, but how to make it count these things? % wc –l ???? •We can solve this by redirection: % ls A* > tempfile % wc –l < tempfile 10 % 11

  12. Connecting commands by redirection Suppose we want to count the number of files beginning with “A” Thels command can list these files, but it can’t count them: % ls A* ABCD Afile APROG.c AZZZ ABD.txt AFILE2 APROG.x ACE AFILE3 Aqrs.txt Thewccommand counts things, but how to make it count these things? % wc –l ???? •We can solve this by redirection: % ls A* > tempfile % wc –l < tempfile 10 % Notice that ls is unique among common UNIX commands, in that the output is different when redirected than when sent to the screen: each file goes on its own line. Notice that this “<” was not actually needed, because wc will allow a file name as an argument. Some UNIX commands work like this, but others don’t. 12

  13. Lets do some more redirection % ls A* > tempfile % wc -l < tempfile 10 % rm -f tempfile % history > tempfile2 % tail -5 tempfile2 > tempfile3 % cat tempfile3 ls A* > tempfile wc -l < tempfile rm -f tempfile history > tempfile2 % head -4 tempfile3 > tempfile4 % 13

  14. Lets do some more redirection % ls A* > tempfile % wc -l < tempfile 10 % rm -f tempfile % history > tempfile2 % tail -5 tempfile2 > tempfile3 % cat tempfile3 ls A* > tempfile wc -l < tempfile rm -f tempfile history > tempfile2 % head -4 tempfile3 > tempfile4 % The history command lists all of the commands that you have typed 14

  15. Lets do some more redirection % ls A* > tempfile % wc -l < tempfile 10 % rm -f tempfile % history > tempfile2 % tail -4 tempfile2 > tempfile3 % cat tempfile3 ls A* > tempfile wc -l < tempfile rm -f tempfile history > tempfile2 % head -4 tempfile3 > tempfile4 % By using tail you will get just the last 4 lines that you typed 15

  16. Lets do some more redirection % ls A* > tempfile % wc -l < tempfile 10 % rm -f tempfile % history > tempfile2 % tail -4 tempfile2 > tempfile3 % cat tempfile3 135 ls A* > tempfile 136 wc -l < tempfile 137 rm -f tempfile 138 history > tempfile2 % head -4 tempfile3 > tempfile4 % Your numbers would be different, because your history will be of a different length. 16

  17. Lets do some more redirection % ls A* > tempfile % wc -l < tempfile 10 % rm -f tempfile % history > tempfile2 % tail -4 tempfile2 > tempfile3 % cat tempfile3 135 ls A* > tempfile 136 wc -l < tempfile 137 rm -f tempfile 138 history > tempfile2 % head -3 tempfile3 > tempfile4 % By using head, we can throw away the line that ran “history” 17

  18. Lets do some more redirection % wc -l < tempfile 10 % rm -f tempfile % history > tempfile2 % tail -4 tempfile2 > tempfile3 % cat tempfile3 135 ls A* > tempfile 136 wc -l < tempfile 137 rm -f tempfile 138 history > tempfile2 % head -3 tempfile3 > tempfile4 % cut--complement-c1-7tempfile4>tempfile5 % Let’s use cut to get rid of those numbers on the front. 18

  19. % tail -4 tempfile2 > tempfile3 % cat tempfile3 135 ls A* > tempfile 136 wc -l < tempfile 137 rm -f tempfile 138 history > tempfile2 % head -3 tempfile3 > tempfile4 % cut --complement-c1-7 tempfile4>tempfile5 % cat tempfile5 ls A* > tempfile wc -l < tempfile rm -f tempfile % Lets do some more redirection See, we took out the line numbers. 19

  20. Lets do some more redirection % cat tempfile3 135 ls A* > tempfile 136 wc -l < tempfile 137 rm -f tempfile 138 history > tempfile2 % head -3 tempfile3 > tempfile4 % cut --complement -c1-7 tempfile4>tempfile5 % cat tempfile5 ls A* > tempfile wc -l < tempfile rm -f tempfile % mv tempfile5 count_A_files % Let us give our script a more-descriptive name 20

  21. Now, lets run that script! % ls –l count_A_files -rw-r--r--1 English None 47 Mar 3 02:16 count_A_files % chmod u+x count_A_files % ls –l count_A_files -rwxr--r--1 English None 15 Mar 2 03:10 count_A_files % cat count_A_files ls A* > tempfile wc -l < tempfile rm -f tempfile % ./count_A_files 10 % 21

  22. Now, lets run that script! % ls –l count_A_files -rw-r--r--1 English None 47 Mar 3 02:16 count_A_files % chmod u+x count_A_files % ls –l count_A_files -rwxr--r--1 English None 47 Mar 3 02:16 count_A_files % cat count_A_files ls A* > tempfile wc -l < tempfile rm -f tempfile % ./count_A_files 10 % 22

  23. Now, lets run that script! % ls –l count_A_files -rw-r--r--1 English None 47 Mar 3 02:16 count_A_files % chmod u+x count_A_files % ls –l count_A_files -rwxr--r--1 English None 47 Mar 3 02:16 count_A_files % cat count_A_files ls A* > tempfile wc -l < tempfile rm -f tempfile % ./count_A_files 10 % 23

  24. Now, lets run that script! % ls –l count_A_files -rw-r--r--1 English None 47 Mar 3 02:16 count_A_files % chmod u+x count_A_files % ls –l count_A_files -rwxr--r--1 English None 47 Mar 3 02:16 count_A_files % cat count_A_files ls A* > tempfile wc -l < tempfile rm -f tempfile % ./count_A_files 10 % 24

  25. Can we make it more general? How often will we want to use this script? It only and always does one thing: count the number of files that begin with an “A” If we could passarguments, it would be more flexible (and therefore more useful) just like we pass arguments to UNIX commands To do this, use the $* symbols in your script When you run the script, the $* symbols will be replaced with all of the arguments 25

  26. A more-flexible version %cat countFiles ls $* > tempfile wc -l < tempfile rm -f tempfile %chmod u+x countFiles A* % ./countFiles A* 10 % ./countFiles * 17 % ./countFiles A* [^A]*.c 11 % See the difference?It has “$*” instead of “A*” 26

  27. % cat countFiles ls $* > tempfile wc -l < tempfile rm -f tempfile % chmod u+x countFiles % ./countFiles A* 10 % ./countFiles * 17 % ./countFiles A* [^A]*.c 11 % A more-flexible version Make it an executable. 27

  28. % cat countFiles ls $* > tempfile wc -l < tempfile rm -f tempfile % chmod u+x countFiles % ./countFiles A* 10 %./countFiles * 17 % ./countFiles A* [^A]*.c 11 % A more-flexible version If we pass in A* as the argument, then we get the same answer as before. 28

  29. % cat countFiles ls $* > tempfile wc -l < tempfile rm -f tempfile % chmod u+x countFiles % ./countFiles A* 10 %./countFiles *x* 4 % ./countFiles A* [^A]*.c 11 % A more-flexible version But, we can also use other arguments. 29

  30. % cat countFiles ls $* > tempfile wc -l < tempfile rm -f tempfile % chmod u+x countFiles % ./countFiles A* 10 % ./countFiles *x* 4 % ./countFiles A* [^A]*.c 11 % A more-flexible version But, If,we had not included this “[^A]”, we would have gotten an answer of 12, because the file named “APROG.c” would have been counted twice. Here, we pass two arguments. The meaning is: All files that begin with A or that end with .c In our directory, there are 11 such files 30

  31. See? Here are the 11 matches: % ls -lrt total 122 -rw-r--r--1 English None 134 Mar 3 22:07square.c -rwxr-xr-x1 English None 50209 Mar 3 22:07 square.x -rw-r--r--1 English None 5 Mar 3 22:09AZZZ -rw-r--r--1 English None 6 Mar 3 22:09Aqrst.txt -rw-r--r--1 English None 7 Mar 3 22:09AFILE3 -rw-r--r--1 English None 7 Mar 3 22:09AFILE2 -rw-r--r--1 English None 6 Mar 3 23:09 Afile -rw-r--r--1 English None 4 Mar 3 22:09 ACE -rw-r--r--1 English None 4 Mar 3 22:09ABD.txt -rw-r--r--1 English None 5 Mar 3 22:09ABCD -rwxr-xr-x1 English None 55Mar 3 22:10APROG.c -rw-r--r--1 English None 49786 Mar 3 22:10 APROG.x -rw-r--r--1 English None 2925 Mar 3 22:14 tempfile2 -rw-r--r--1 English None 94 Mar 3 22:14 tempfile3 -rw-r--r-- 1 English None 68 Mar 3 22:15 tempfile4 -rw-r--r--1 English None 47 Mar 3 22:16 count_A_files -rw-r--r--1 English None 47 Mar 3 22:18 count_files 31

  32. Can we avoid using that tempfile? Our script used a temporary file to pass information between the commands. 32

  33. Can we avoid using that tempfile? Our script used a temporary file to pass information between the commands. It wasn’t an elegant way to pass information. 33

  34. Can we avoid using that tempfile? Our script used a temporary file to pass information between the commands. It wasn’t an elegant way to pass information. Also, it could have side effects. 34

  35. Can we avoid using that tempfile? Our script used a temporary file to pass information between the commands. It wasn’t an elegant way to pass information. Also, it could have side effects. Consider this run case: %./countFiles * 18 % 35

  36. Can we avoid using that tempfile? Our script used a temporary file to pass information between the commands. It wasn’t an elegant way to pass information. Also, it could have side effects. Consider this run case: Hey! There are just 17 files! Why does it say 18? %./countFiles * 18 % 36

  37. Can we avoid using that tempfile? Our script used a temporary file to pass information between the commands. It wasn’t an elegant way to pass information. Also, it could have side effects. Consider this run case: Hey! There are just 17 files! Why does it say 18? There is a better way: usecommandpipes %./countFiles * 18 % 37

  38. Pipes You can string commands together into a single command using pipes ( | ) To count how many files are in a directory: ls | wc -l • To count how many words are in file1 and to put the result into a new file, file2: wc -w file1> file2 cat file1 | wc -w > file2 cat < file1 | wc -w > file2 38

  39. So how would you do it with pipes? The original code was: %cat countFiles ls $* > tempfile wc -l < tempfile rm -f tempfile % 39

  40. So how would you do it with pipes? The original code was: Q: What would the program look like if we could use pipes? %cat countFiles ls $* > tempfile wc -l < tempfile rm -f tempfile % 40

  41. So how would you do it with pipes? The original code was: Q: What would the program look like if we could use pipes? A: It would look like this: %cat countFiles ls $* > tempfile wc -l < tempfile rm -f tempfile % %cat countFiles_version2 ls $* | wc -l % 41

  42. Pipes • You can string commands together into a single command using pipes ( | ) • To count how many files are in a directory: ls | wc -l • To count how many words are in file1 and to put the result into a new file, file2: wc -w file1 > file2 cat file1 | wc -w > file2 cat < file1 | wc -w > file2 42

  43. Looking around the source code Show the .c files in this directory: type ls *.c How many .c files are in this directory? type ls *.c | wc Show the .c files exactly one directory inside: type ls */*.c How many .c files are exactly one directory inside? type ls */*.c | wc Show the .c files exactly two directories inside: type ls */*/*.c How many .c files are exactly two directories inside? type ls */*/*.c | wc Show all .c files inside this directory structure: type find . -name "*.c" How many total .c files inside this directory structure: type find . -name "*.c" | wc How would you accomplish these things in Windows? Hard. 43

  44. uniq uniqonly prints unique lines. This means that it erases duplicates. A useful flag is -c, which includes a count of the duplicates. But note: it does not search the whole file for duplicates, just the preceding line. This is why it is often piped from sort, so that the duplicates will be adjacent.

  45. Remember this slide? % wc -l < tempfile 10 % rm -f tempfile % history > tempfile2 % tail -4 tempfile2 > tempfile3 % cat tempfile3 135 ls A* > tempfile 136 wc -l < tempfile 137 rm -f tempfile 138 history > tempfile2 % head -3 tempfile3 > tempfile4 % cut--complement-c1-7tempfile4>tempfile5 % Let’s throw away the parts that don’t matter for this... 45

  46. Remember this slide? % wc -l < tempfile 10 % rm -f tempfile % history > tempfile2 % tail -4 tempfile2 > tempfile3 % cat tempfile3 135 ls A* > tempfile 136 wc -l < tempfile 137 rm -f tempfile 138 history > tempfile2 % head -3 tempfile3 > tempfile4 % cut--complement-c1-7tempfile4>tempfile5 % Let’s throw away the parts that don’t matter for this... 46

  47. Remember this slide? % history > tempfile2 % tail -4 tempfile2 > tempfile3 % cat tempfile3 135 ls A* > tempfile 136 wc -l < tempfile 137 rm -f tempfile 138 history > tempfile2 % head -3 tempfile3 > tempfile4 % cut--complement-c1-7tempfile4>tempfile5 % Let’s throw away the parts that don’t matter for this... 47

  48. Remember this slide? % history > tempfile2 % tail -4 tempfile2 > tempfile3 136 wc -l < tempfile 137 rm -f tempfile 138 history > tempfile2 % head -3 tempfile3 > tempfile4 % cut--complement-c1-7tempfile4>tempfile5 % Let’s throw away the parts that don’t matter for this... 48

  49. Remember this slide? % history > tempfile2 % tail -4 tempfile2 > tempfile3 138 history > tempfile2 % head -3 tempfile3 > tempfile4 % cut--complement-c1-7tempfile4>tempfile5 % Let’s throw away the parts that don’t matter for this... 49

  50. Remember this slide? % history > tempfile2 % tail -4 tempfile2 > tempfile3 % head -3 tempfile3 > tempfile4 % cut--complement-c1-7tempfile4>tempfile5 % Let’s throw away the parts that don’t matter for this... 50

More Related