Skip to content

University-Experience/Regex-AWK

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Regex-AWK Lab

Introduction

In this lab, You will gain practical knowledge in working with Regular Expressions and some command-based data processing tools like AWK. You will work on the given simple text file (data.txt) and use linux to process the file to answer some required queries.

Lab requirements:

  • You must have linux or WSL to run the commands on the terminal
  • Learn regular expression (Regex) and finish this tutorial here
  • Gain lots of skills working with AWK from this article (until lession number 13 at least) here
  • Try to work with AWK on this interactive tutorial here

Steps to start:

1- Go to your working directory:

cd <your working dirctory Ex. 'cd C:\docker'>

2- create a text file (data.txt for example) and insert the data we will work on:

nano data.txt

Then paste the data instide this file and press "ctrl+o" then "enter" then "ctrl+x" to exit

3- To view the data:

cat data.txt

image

Now you are ready to work on the quests ⚡✅

Quests:

a- Print only the first and last names of all the clients (there must be a space between first and last names):

It can be done in two ways:

awk '{print $1, $2}' data.txt
awk '{print $1 " " $2}' data.txt

image

b- Same as above but print the names are in reverse order (last name, first name) and make sure there is a comma between last name and first name:

It can be done like this:

awk '{print $2 "," $1}' data.txt

image

c- Print only first name and last name (without header that has the names of the columns: first name, last name, etc):

It can be done like this:

awk 'NR>1 {print $1, $2}' data.txt

image

d- Same as above but with numbers indicating the client's order:

It can be done like this:

awk 'NR>1 {print NR-1, $1, $2}' data.txt

image

e- Print first and last names of customers who are more than 50 years old:

It can be done like this:

awk 'NR>1 && $4 > 50 {print $1, $2}' data.txt

image

f- Print the first and last names of customers who own more than 10000$:

It can be done like this:

awk 'NR>1 && $5 > 10000 {print $1, $2}' data.txt

image

g- Print the total sum of all money in all the accounts:

It can be done like this:

awk '{sum += $5} END {print sum}' data.txt

image

h- Print all the information of all accounts whose owner's first name is Chad. (hint. use REGEX):

It can be done like this:

awk '$1 ~ /^Chad$/ {print}' data.txt

image

i- Print all the information of all accounts whose owner's last name ends with the letter r (hint: REGEX):

It can be done like this:

awk 'NR>3 && $2 ~ /r$/ {print}' data.txt

image

j- Print all the information of all accounts whose owner age has the number for the first and 2nd digits:

It can be done like this:

awk '$4 >= 10 && int($4/10) == $4 % 10 {print}' data.txt

image

Congratulations 🥳💥

You've completed this lab, I hope you find it helpful, follow me on my github profile to learn more exciting things ⚡💻

About

Distributed Operation Systems Lab2

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors