find
is used to search for files in the Unix file system that match certain criteriagrep
is used to search text files with regular expressionsfind
searches recursively through directories testing each item (directories, files, devices, sockets, symlinks, etc.) it encounters against a set of operatorsbash
-rwx------ 1 kena staff 0 Sep 16 22:15 program1*
-rwx------ 1 kena admin 0 Sep 16 22:15 program2*
$ find . -name "program*" -group staff -print
This prints the file name "./program1"
How would the output change if the command is modified as follows?
$ find . -name "program*" -print -group staff -print
As a reminder, -print is an operator that prints the name of the current item to stdout and then returns true
prog1
)find
is that its output changes slightly based on the directory given to it at the command line./home/faculty/kena/csci3308/src/lab05/object2.c
/home/faculty/kena/csci3308/src/lab05/subject2.c
/home/faculty/kena/csci3308/src/lab05/subject1.c
/home/faculty/kena/csci3308/src/lab05/verb.c
/home/faculty/kena/csci3308/src/lab05/object1.c
/home/faculty/kena/csci3308/src/lab05/facts.c
./csci3308/src/lab05/object2.c
./csci3308/src/lab05/subject2.c
./csci3308/src/lab05/subject1.c
./csci3308/src/lab05/verb.c
./csci3308/src/lab05/object1.c
./csci3308/src/lab05/facts.c
../kena/csci3308/src/lab05/object2.c
../kena/csci3308/src/lab05/subject2.c
../kena/csci3308/src/lab05/subject1.c
../kena/csci3308/src/lab05/verb.c
../kena/csci3308/src/lab05/object1.c
../kena/csci3308/src/lab05/facts.c
grep architecture /usr/dict/words
#!/usr/bin/ruby
, place Ruby commands after it, and make the file executable. It then becomes possible to invoke the command like any other shell command
#!/usr/bin/ruby
puts "!ybuR oT emocleW".reverse
def
keyword; method definitions are closed with the end
keyword
def sort(a, b)
# both a and b are required parameters
…
end
def sort(a, b=2)
# a is required, b is optional with default value of 2
…
end
def sort(a, b=2, *c)
# a is required, b is optional with default value of 2
# c is an array containing any other values passed to the method
# this is how Ruby supports methods with a variable number of arguments
…
end
return
keyword is optional)
def foo(a, b)
a + b
end
a = 20
puts "The value of a is #{a}"
# produces "The value of a is 20"
match
method that applies a regular expression looking for a match
Here is a Ruby program that demonstrates using Ruby's regular expression operators, the match method, MatchData, and the regular expression related global variables.
#!/usr/bin/ruby firstandlast = /Mr\. ([A-Za-z]+) ([A-Za-z]+)/ target = "My name is Mr. Ken Anderson, so there." m = target.match(firstandlast) puts "======== String and Regular Expression ===========" puts puts "Target String : '#{target}'" puts "Regular Expression : Mr\. ([A-Za-z]+) ([A-Za-z]+)" puts puts "============== Global Variables ==================" puts puts "Entire Match ($&): '#{$&}'" puts "Before the Match ($`): '#{$`}'" puts "After the Match ($'): '#{$'}'" puts "First Group ($1): '#{$1}'" puts "Second Group ($2): '#{$2}'" puts puts "================= Match Data ====================" puts puts "Entire Match (m[0]): '#{m[0]}'" puts "m.pre_match : '#{m.pre_match}'" puts "m.post_match : '#{m.post_match}'" puts "First Group (m[1]): '#{$1}'" puts "Second Group (m[2]): '#{$2}'" puts "Start of $1 : '#{m.begin(1)}'" puts "End of $1 : '#{m.end(1)}'" puts "Start of $2 : '#{m.begin(2)}'" puts "End of $2 : '#{m.end(2)}'" puts puts "================= Match Data ====================" puts puts "Result of target =~ firstandlast : #{target =~ firstandlast}" puts "Result of target !~ firstandlast : #{target !~ firstandlast}" puts puts "=================================================="
sub
method searches a string with its first argument and returns a string that replaces the first match with sub's
second argument.gsub
method searches a string with its first argument and returns a string that replaces each match with gsub's
second argument.sub
and gsub
return new strings, if you want to modify the target string directly use sub!
and gsub!
ken ="Ken"
ken.sub(/e/, "en Anderso")
# returns the string "Ken Anderson"
# ken still equals "Ken Anderson"
ken.sub!(/e/, "en Anderso")
# returns the string "Ken Anderson"
# ken now equals "Ken Anderson" (!!!!)
# in both cases, we were matching the "e" in "Ken" and replacing it with the string "en Anderso"
name = "Ken Anderson Ken Anderson"
name.gsub(/Ken/, "Max")
# returns "Max Anderson Max Anderson"
name.gsub(/([A-Za-z]+) ([A-Za-z]+)/, '\2 \1')
# returns "Anderson Ken Anderson Ken"
sub
and gsub
become extremely useful in the context of searching and modifying files using regular expressions when you realize that you can read an entire file into a single string! You will get a chance to try this out in Lab 2!