Linux Quiz 8 Regular Expressions vs Wildcards "Need help to see if I got them correct or not" Write the command line to

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Linux Quiz 8 Regular Expressions vs Wildcards "Need help to see if I got them correct or not" Write the command line to

Post by answerhappygod »

Linux Quiz 8 Regular Expressions vs Wildcards "Need help
to see if I got them correct or not"
Write the command line to do each of the following. Use only the
commands "ls" or "grep" or "sed"! This is not a test of commands,
but of regular expressions and wildcards. For some questions there
is no command line that will work (using just these commands and no
pipe). For those questions, just write "impossible" or "no". DO NOT
LEAVE SUCH QUESTIONS BLANK!
NOTE1: For the "sed" command, DO NOT USE
"sed -i"!
NOTE2: For the impossible commands, using
a pipe is possible. I will accept those answers if they are done
correctly.
NOTE3: Do not try to use the "find"
command, unless you are an expert! One problem with it is that it
searches subdirectories, so can get many more answers than
desired.
NOTE4: You should quote the patterns in
"grep", else some characters may be treated as wildcards and have
file names substituted.
xgh
6. Find all the lines in file “testing”
that contain a word that starts with “g” and ends with “h”, but can
only have “x”s between the “g” and “h”.
Answer Attempt1: ls
7. Find all the files in your current
directory whose name has the second letter “g” and next-to-last
letter “h” and only a “1” or a “2” or a “3” in between them.
Answer Attempt1: sed
8. Find all the lines in file “testing”
that have second letter “g” and next-to-last letter “h” and nothing
except possibly “1”, “2” or “3” in between them, possibly more than
once.
Answer Attempt1: grep
9. Find all the lines that contain nothing
but the letters a, b, c, d, w, x, y or z (any number of times, and
no other letters), in all the files whose name starts with one of
those letters. That is to say, you are looking in several files at
once, and for each file, you are finding a particular set of
lines.
Answer Attempt1: sed
10. In file “testing”, change all
instances of the name “Stan” or “Stanley” (capitalized or not) to
“Fred”. Make sure you don’t change words like Stanford or
understand (stan or stanley must be whole words). This is a hard
question, but give it a try, you'll get most of the credit if you
make an effort. You'll be using the "sed" command, and should know
that in "sed", to group characters together in a subexpression (for
instance, before a repetition symbol), the parentheses need to be
quoted with a backslash. Also some extended regex characters need
backslashes, as "\?" and "\b". An alternative is to use "-r" (or
even -E with this Ubuntu Linux) to allow the regular expression
characters without backslashing them. Try to test your answer as
completely as you can.
My Answer Attempt1: sed testing [s-S]
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply