Unix P Journal
Unix P Journal
Sign :- _________________
P a g e 1 | 16
TY BCA Unix Journal 3023
Output..
P a g e 2 | 16
TY BCA Unix Journal 3023
Ans..
# Get the current hour in 12-hour format (1-12) with AM/PM
hour=$(date +"%I") minute=$(date +"%M") ampm=$(date
+"%p")
P a g e 3 | 16
TY BCA Unix Journal 3023
Output..
Existing_Attemp=0
P a g e 4 | 16
TY BCA Unix Journal 3023
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
}
P a g e 7 | 16
TY BCA Unix Journal 3023
Output..
Output..
P a g e 8 | 16
TY BCA Unix Journal 3023
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
P a g e 9 | 16
TY BCA Unix Journal 3023
P a g e 10 | 16
TY BCA Unix Journal 3023
Que.15 Display the lines which are not starting with 2 at the
beginning.
Ans..
grep -v "^2" filename Output..
P a g e 11 | 16
TY BCA Unix Journal 3023
Que.18 Display two lines starting from 4th line of file test.txt.
Ans..
sed -n '4,5p' test.txt Output..
P a g e 12 | 16
TY BCA Unix Journal 3023
Ans..
grep "^[a-zA-Z]" test.txt 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..
P a g e 13 | 16
TY BCA Unix Journal 3023
{
num = 10
while (num >= 1) { print
num num--
}
}
Output..
wc -l f1.txt Output..
P a g e 15 | 16
TY BCA Unix Journal 3023
Ans..
find . -maxdepth 1 -type f -user $(whoami) Output..
P a g e 16 | 16