5.a.
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]
# Validate day and month
if (day < 1 || day > 31 || month < 1 || month > 12) {
print "Invalid date"
exit 0
}
# Print the day
print day
# Print month name based on the value of month
if (month == 1) {
print "Jan"
} else if (month == 2) {
print "Feb"
} else if (month == 3) {
print "March"
} else if (month == 4) {
print "April"
} else if (month == 5) {
print "May"
} else if (month == 6) {
print "Jun"
} else if (month == 7) {
print "Jul"
} else if (month == 8) {
print "Aug"
} else if (month == 9) {
print "Sep"
} else if (month == 10) {
print "Oct"
} else if (month == 11) {
print "Nov"
} else if (month == 12) {
print "Dec"
}
# Print the year
print year
}
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
awk '!seen[$0]++' in.txt > out.txt
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
The SED (Stream Editor) command in Unix/Linux is a powerful utility
used to process and manipulate text in files.
SED is a powerful text stream editor. Can do insertion, deletion,
search, and replace(substitution).
SED command in UNIX supports regular expression which allows it to
perform complex pattern matching.
Syntax:
sed [OPTIONS] 'SCRIPT' [INPUTFILE...]
where,
‘OPTIONS’: These are optional flags that modify the behavior of the
sed command.
‘SCRIPT’: This defines the command or sequence of commands to
execute on the input file.
‘INPUTFILE’: One or more input files to be processed.
Consider the below text file as an input.
$cat > geekfile.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn
unix .unix is a powerful.
1. Sample Commands
Replacing or substituting string: Sed command is mostly used to
replace the text in a file. The below simple sed command replaces the
word “unix” with “linux” in the file.
$sed 's/unix/linux/' geekfile.txt
Output
linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn
unix .unix is a powerful.
2. Replacing the nth occurrence of a pattern in a line
Use the ‘/1’, ‘/2’ etc. flags to replace the first, second occurrence of a
pattern in a line. The below command replaces the second occurrence of
the word “unix” with “linux” in a line.
$sed 's/unix/linux/2' geekfile.txt
Output:
unix is great os. linux is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.linux is a multiuser os.Learn
unix .unix is a powerful.
3. Replacing all the occurrence of the pattern in a line
Use the ‘/1’, ‘/2’ etc. flags to replace the first, second occurrence of a
pattern in a line. The below command replaces the second occurrence of
the word “unix” with “linux” in a line.
$sed 's/unix/linux/2' geekfile.txt
Output:
unix is great os. linux is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.linux is a multiuser os.Learn
unix .unix is a powerful.
5. Parenthesize first character of each word
echo "Hello World" | sed 's/\([A-Z]\)/(\1)/g'
Explanation:
s/: This specifies the substitution command in sed.
\([A-Z]\): This captures any uppercase letter (A-Z) in a capturing group.
(\1): This replaces the uppercase letter with itself surrounded by parentheses. \1
refers to the captured uppercase letter.
g: This applies the substitution globally on each line.
Output
(H)ello (W)orld
6. Replacing string on a range of lines
sed '2,$ s/unix/linux/'
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.
Eg: $sed '2,$ s/unix/linux/' geekfile.txt
echo -e "I love unix\nunix is powerful\nunix is everywhere" | sed '2,$ s/unix/linux/'
7 Deleting lines from a particular file
SED command can also be used for deleting lines from a particular file.
SED command is used for performing deletion operation without even
opening the file
Examples:
1. To Delete a particular line say n in this example
Syntax:
$ sed 'nd' filename.txt
Example:
$ sed '5d' filename.txt
2. To Delete a last line
Syntax:
$ sed '$d' filename.txt
3. To Delete line from range x to y
Syntax:
$ sed 'x,yd' filename.txt
Example:
$ sed '3,6d' filename.txt
4. To Delete from nth to last line
Syntax:
$ sed 'nth,$d' filename.txt
Example:
$ sed '12,$d' filename.txt
5. To Delete pattern matching line
Syntax:
$ sed '/pattern/d' filename.txt
Example:
$ sed '/abc/d' filename.txt