cs2042_Lecture6 - CS2042 - Unix Tools Fall 2009 Lecture 6...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS2042 - Unix Tools Fall 2009 Lecture 6 David Slater dms236 at cornell.edu October 13, 2009 David Slater dms236 at cornell.edu CS2042 - Unix Tools Organization Homework 2 Due Wednesday! No Class on Monday! David Slater dms236 at cornell.edu CS2042 - Unix Tools When to Script What if we wanted to... Change a Notepad-style text file to a Unix-style? Strip directory prefixes from a path? Print certain columns from a text file? Remove all the comment lines from some code? Count all words in a text file and sort them by number of occurrences? How much time/effort/code would it take to do this stuff in C/C++/Java? Stuff like this is the reason scripting was created - you can get each task done in a line or two. David Slater dms236 at cornell.edu CS2042 - Unix Tools Search and Replace The Tr anslate Command tr [options] <set1> [set2] Translate or delete characters Sets are strings of characters Only works on STDIN/STDOUT- use redirection to translate files By default, searches for strings matching set1 and replaces them with set2 Example: tr AEIOU aeiou < somefile- changes all capital vowels to lower case vowels David Slater dms236 at cornell.edu CS2042 - Unix Tools Some Simple Examples Example: echo * prints everything in the directory, separated by spaces. Lets seperate them by newlines instead: echo * | tr \n replaces all spaces with newlines Example: Lets print a file in all uppercase: tr a-z A-Z < test.txt- prints the contents of text.txt in all caps David Slater dms236 at cornell.edu CS2042 - Unix Tools tr and sets If the two sets passed to tr are the same length then the first character in the first set is replaced by the first in the second and so on. If the second is shorter than the first, then they are matched and all extra are changed to the last character If the first is shorter, then only those cooresponding characters in the second matter Example: echo "abcdefghijklmnopqrstuvwxyz" | tr a-z a aaaaaaaaaaaaaaaaaaaaaaaaaa echo "abcdefghjiklmnopqrstuvwxyz" | tr a-z wxyz wxyzzzzzzzzzzzzzzzzzzzzzzz David Slater dms236 at cornell.edu CS2042 - Unix Tools tr options tr has some very useful options: tr options tr -d <set>- delete all characters that are in the set tr -c <set1> [set2]- complements set1 before replacing it with set2 Example: Lets print a file with all nonletters removed: tr -cd a-zA-Z < somefile- removes nonletters from somefile David Slater dms236 at cornell.edu CS2042 - Unix Tools Redirection Revisited Bash processes I/O redirection from left to right, allowing us to do fun things like this: Example: Lets delete everything but the numbers from test1.txt, then store them in test2.txt tr -cd 0-9 < test1.txt > test2.txt Note: Redirecting from and to a file at the same time (e.g....
View Full Document

Page1 / 31

cs2042_Lecture6 - CS2042 - Unix Tools Fall 2009 Lecture 6...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online