Assignment-1 SHELL Solutions

Question-1:

cat "$1" | sed  "s/./#/5g" > output.txt 

Question-2:

#!/bin/bash

file_path=$1 # taking the path of input file
value=`cat $1` # reading the file

IFS=' ' # setting delimiter as <space>
read -ra arr<<<$value #splitting the input text by the delimiter and storing the splits in array named arr
n=${#arr[@]} # n = total no. of elements in the array
dob=${arr[$n-1]} # date of birth is the last element of the array

IFS='/'
read -ra arr2<<<$dob
dob_day=${arr2[0]}				# Getting day,month,year for dob date
dob_month=${arr2[1]}      
dob_year=${arr2[2]}

cur_date=$(date '+%Y-%m-%d')
IFS='-'
read -ra arr3<<<$cur_date
cur_day=${arr3[2]}				# Getting day,month,year for current date
cur_month=${arr3[1]}
cur_year=${arr3[0]}


age=$(( cur_year - dob_year ))

if [[ ${cur_month#0} -lt ${dob_month#0} ]]; then	
	age=$(( $age - 1 ))
elif [[ ${cur_month#0} -eq ${dob_month#0} ]]; then				# Basic logic for age calculation
	if [[ ${cur_day#0} -lt ${dob_day#0} ]]; then
		age=$(( $age - 1 ))
	fi
fi

unset arr[-1]
arr+=($age)
function join { local IFS=" "; shift; echo "$*"; }				# Output Generation
result=$(join , ${arr[@]})

echo $result
echo $result > output.txt

Question-3:

Question-4:

Question-5:

Question-6:

Last updated

Was this helpful?