Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Linux- 1. You were a given a very large log file that has the colon “:” delimite

ID: 3706164 • Letter: L

Question

Linux-

1. You were a given a very large log file that has the colon “:” delimiter present between each of the line values and you want to pull out the 2, 4 and 7 column to a file called “May-2014-parsed”. What are the steps you would take to accomplish this task. Hint: use the awk command.

2. You would like to write an alias that counts the number of directory entries in your current directory. What commands would you “pipe” together and how do you set up the alias if you want to call it “numofdirs”?

3. Describe the steps and command that are required to change all instances of the work “May” to the word “MAY” in the file called May-2014

Explanation / Answer

Hi,

Here the commands for your question. The description follows each of the commands.
I have verified these commands on Ubuntu 16.04 with Bash shell.


Hope this answer helps you.


#1
cat large-logfile.log | awk -F: '{ print $2, $4, $7 }' > May-2014-parsed


Description:
We use the "cat" command to print the contents of the file and then using "awk" command extract the values in the column #2, #4 and #7. The extracted values are written to a new file - May-2014-parsed using the redirection operator (>)

The -F switch passed to the awk command implies the delimiter is colon (:)


#2
alias numofdirs='ls -l | grep "^d" | wc -l'

We use the "ls" command with long listing option (-l) and then using "grep" command, we extract only the entries that begin with "d" - implies that the entry is a directory. Finally we use the "wc" command with "-l" option to retrieve the count of lines.

The result of these commands piped to one another is the count of directories in the current folder.

To set an alias, we use the "alias" command. The new alias will be known as "numofdirs" and the value will be set as the above sequence of commands.

You can now execute the alias command at the prompt and verify the count.

$ numofdirs


#3
sed -i -e 's/May/MAY/g' May-2014

To replace the instances of a string "May" to a new value (say MAY) in a file, we can use "sed" command. The -e option is pass the script content. The script contains the code to substitute (s), the first string (May) and replace with (MAY). The /g in the script code implies replace all matching instances in the current line.

The -i option for the "sed" command is to imply in-place editing of the current file.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote