Blute
Blute
Develop an awk script that accepts date arguments in the form of dd-mm-yy and display
it in the form month, day, and year. The script should check the validity of the argument and in
the case of error, display a suitable message.
Step1:prog5a.sh Code:
rit-admin@ritadmin:~$ vi pgr5.awk
#!/usr/bin/awk -f
{
split($0, arr, "-") # Split input by "-" into the arr array
day = arr[1]
month = arr[2]
year = arr[3]
output:
rit-admin@ritadmin:~$ awk -f pgr5.awk
04-09-2024
04
Sep
2024
b.Develop an awk script to delete duplicated line from a text file. The order of the original lines
must remain unchanged.
Step1:vi prog5b.awk
Code:
#!/usr/bin/awk -f
BEGIN {
print "Removing duplicated lines..."
no = 0
}
{
line[++no] = $0 # Store the current line in the array and increment the counter
}
END {
for (i = 1; i <= no; i++) {
flag = 1 # Assume the line is unique
for (j = 1; j < i; j++) {
if (line[i] == line[j]) {
flag = 0 # Found a duplicate
break; # No need to check further
}
}
if (flag == 1) {
print line[i] >> "out13a.txt" # Print unique line to the output file
}
}
}
Output:
exam@h-primary:~$ vi prog5b.awk
exam@h-primary:~$ >out13a.txt // To remove all the content in the file “out13a.txt”
exam@h-primary:~$ cat out13a.txt
exam@h-primary:~$ cat input.txt // To add content in the input file
mat
cat
pat
cat
mat
bat
exam@h-primary:~$ awk -f prog5b.awk input.txt // To compile
Removing duplicated lines...
exam@h-primary:~$ cat out13a.txt // To display after removing duplicate contents
mat
cat
pat
bat
OR
Through AWK command
OUTPUT
exam@h-primary:~$ cat > in.txt
orange
apple
mango
apple
orange
peer
^Z
[1]+ Stopped cat > in.txt
exam@h-primary:~$ awk '!seen[$0]++' in.txt > out.txt
exam@h-primary:~$ cat out.txt
orange
apple
mango
peer
exam@h-primary:~$ cat in.txt
orange
apple
mango
apple
orange
peer
SED command
Output
Output
(H)ello (W)orld
Explanation:
2,$: This specifies the range of lines where the substitution will take place. 2 is the
starting line, and $ represents the last line.
s/unix/linux/: This part tells sed to substitute occurrences of "unix" with "linux" in
the specified line range.