Question: 1 . Create a shell script file called asgn 0 5 . sh 2 . Complete this newly created shell script asgn 0 5 .

1. Create a shell script file called asgn05.sh
2. Complete this newly created shell script asgn05.sh. The invocation of the shell script is as follows: asgn05.sh sample.log '^user|action|additional' 'location.lat,data.amount'
where
$1 is the log file;
$2 is a regular expression to filter the entries by matching specific log field keys. (e.g.,^user|action|additional: This will match any fields that start with user, or contain action, or contain additional. If an entry doesnt contain any fields matching the pattern, discard it.);
$3 is the list of JSON fields separated by comma (location.lat and data.amount will extract the lat value from the location JSON object and amount value from the data JSON object, if present).
3. The script asgn05.sh needs to do the followings: i. Parse Each Log Entry: Extract both JSON and key-value pairs from each entry. ii. Standardize Missing Fields: For any missing fields in an entry, set the value to "N/A". iii. Filter by Regular Expression: Use $2 to identify entries containing those fields that match the provided regex pattern. If an entry doesnt contain any fields matching the pattern, discard it.(Note: if an entry doesnt contain any fields specified in $2, but contains fields specified in $3, discard it.) iv. Extract JSON Field: Use $3 to extract the specified JSON fields (if present). v. Generate a CSV Report: i. Columns should include all unique keys from both JSON and key-value pairs. ii. Missing values should be filled with "N/A". iii. Ensure that columns appear in alphabetical order.
Sample Run: The expected output for executing asgn05.sh sample.log '^user|action|additional' 'location.lat|data.amount' shown below: action,additional_info,data_amount,location_lat,user_id login,N/A,N/A,40.7128,45 purchase,Promo code applied,200,N/A,58 N/A,N/A,N/A,51.5074,32 logout,N/A,N/A,N/A,100 purchase,N/A,120,N/A,21
sample.log file below a few lines example
---
timestamp=2023-10-10T10:15:30 user_id=45 location={"country":"US","city":"New York","lat":40.7128,"long":-74.0060} action=login --- timestamp=2023-10-11T14:22:17 user_id=58 data={"value":10,"type":"purchase","amount":200} action=purchase additional_info=Promo code applied
---
timestamp=2023-10-12T09:45:50 location={"country":"UK","city":"London","lat":51.5074,"long":-0.1278} user_id=32 data={"value":15,"type":"login"}
---
timestamp=2023-10-13T16:05:22 user_id=100 action=logout status=success --- timestamp=2023-10-14T20:12:00 user_id=21 data={"value":30,"type":"purchase","amount":120} action=purchase

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!