Question
Regular Expressions with sed and awk(part II) 4. We will use sed on the ~Student/FILES/sales.txt file. a. Using sed, remove all of the commas between
Regular Expressions with sed and awk(part II)
4. We will use sed on the ~Student/FILES/sales.txt file. a. Using sed, remove all of the commas between states. We will search for , and replace it with nothing: sed s/,// sales.txt Explain the instruction. Notice in the output, only one comma was removed. How would you change your instruction so all commas are removed?
b. Using sed, insert a $ before each sales value. Since sales amounts will be consecutive digits longer than 2, we can search for [0-9][0-9][0-9]. We replace this with $& where & refers to the original matching number.
c. Using sed, lower case all state abbreviations. Here, notice that all state abbreviations are at the end of the line. We would search for [A-Z,]+ and replace this with \L&. The \L means lower case every letter in the match. For sed, we cannot use + by itself, but instead must specify the metacharacter using \+.
Our is sed s/[0-9][0-9][0-9]/\$&/ sales.txt
Explain why this instruction would not cause the $ to appear before Commission rates.
Our instruction is sed s/[A-Z, ]\+$/\L&/ sales.txt
d. Using sed, insert a comma in the appropriate place in the Sales values. For instance, 10381 should be 10,381 and 8781 should be 8,781. To do this, we need to count from the right side of each sales amount. Each sales amount has 4 or 5 digits followed by a tab. We want the comma to appear before the 3rd digit as counted from the right. We could accomplish this using the instruction
e. Using sed, change the commission entries from the format .## to ##%. You might think to do the following sed s/\.[0-9][0-9]/&%/ sales.txt Explain why this doesnt work correctly. To solve this, we need to remember a specific portion of the matched pattern using \( and \). We can then reference the matched portion using \1 instead of &. Our revised instruction is
What happens if you do not use the $ in the regular expression?
sed s/[0-9][0-9][0-9]\t/,&/ sales.txt.
What would happen if there was a 3-digit sales amount? What would happen if there was a 7-digit sales amount?
sed s/\.\([0-9][0-9]\)/\1%/ sales.txt
Explain why this solution works (that is, removes the problem that the previous solution created).
5. We will use awk on the sales.txt file. a. Write an awk command to compute the salary (Sales * Commission) for each employee for each month. We do not want to include the first row of the file which is just header information. Notice that first row has no numbers. In our awk instruction, we can then search for a pattern that includes digits, [0-9]. The instruction is
awk /[0-9]/ {print $1, $2, $3*$4} sales.txt
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started