Unix Assignment
Unix Assignment
else
fi
Output..
Page 1 of 17
good morning, good afternoon, good evening and good
night.
Ans..
# Get the current hour in 12-hour format (1-12) with AM/PM
hour=$(date +"%I")
minute=$(date +"%M")
ampm=$(date +"%p")
Output..
if [ -z "$file_path" $1 ]; then
echo "File path is Empty! Please Provide Existing path.."
exit $1
fi
if [ -f "$file_path" ]; then
echo "$file_path" is Regular file....
elif [ -d "$file_path" ]; then
echo "$file_path" is a Directory...
else
echo "$file_path" Does not Exist !!!
fi
Output..
Page 3 of 17
maxAttemps=3
Existing_Attemp=0
((Existing_Attemp++))
if [ $Existing_Attemp -eq $maxAttemps ]; then
echo "Maximum Attemps Reached !! Access Denied.."
exit 1
fi
done
Output..
Page 4 of 17
Que.5 Accept a string from terminal and echo suitable message if
it does not have at least 10 characters.
Ans..
#Accept String Input From User !!
read -p "Enter the String:= " str
Output..
Page 5 of 17
#Accept string from user !!
read -p "Enter the String:= " str
Output..
Page 6 of 17
#Addition Of The Entered Numbers..
sum=$((sum + n))
done
Output..
Que.8 Write an awk script to print each odd line twice and even
line thrice.
Ans..
#!/usr/bin/awk -f
# Check if the line number is odd
NR % 2 == 1 {
# Print the odd line twice
print $0
print $0
}
Page 7 of 17
NR % 2 == 0 {
# Print the even line thrice
print $0
print $0
print $0
}
Output..
Page 8 of 17
fi
Output..
Que.10 Write a shell script which takes input of file name and
prints first 10 lines of that file. file name is to be passed
as command line argument. If argument is not passed
then any ‘C’ program from the current directory is
to be selected .
Ans..
#!/bin/bash
Page 9 of 17
# Print the first 10 lines of the file
echo "Displaying the first 10 lines of $file:"
head -n 10 "$file"
Output..
Output..
Page 10 of 17
Que.14 Display the lines that do not contain “Unix”.
Ans..
grep -v "Unix" q5.sh
Output..
Que.15 Display the lines which are not starting with 2 at the
beginning.
Ans..
grep -v "^2" filename
Output..
Page 11 of 17
file test.txt.
Ans..
grep "^[a-zA-Z0-9]" test.txt
Output..
Que.18 Display two lines starting from 4th line of file test.txt.
Ans..
sed -n '4,5p' test.txt
Output..
Output..
Page 12 of 17
Que.20 To count number of words in line 10 thought 20 of file
test.txt.
Ans..
sed -n '10,20p' test.txt | wc -w
Output..
Output.
Que.22 Print the sums of the fields of every line in file f1.
Ans..
awk '{sum=0; for(i=1; i<=NF; i++) sum += $i; print sum}' f1
Output..
Page 13 of 17
Que.23 Display those words whose length greater than 10
characters and consist of digits only.
Ans..
grep -o '\b[0-9]\{11,\}\b' data.txt
Output..
{
num = 10
while (num >= 1) {
print num
num--
}
}
Output..
Page 14 of 17
Que.25 Count the total no. of lines in a file.
Ans..
wc -l f1.txt
Output..
Output..
Output..
Page 15 of 17
Que.28 Display last 2 lines of working directory.
Ans..
ls | tail -n 2
Output..
Output..
Page 16 of 17
Ans..
awk 'FNR==1{print ""} {print}' f1.txt f2.txt
Output..
Page 17 of 17