In this assignment, you are asked to do reformat the all-caps text messages as t
ID: 3827856 • Letter: I
Question
In this assignment, you are asked to do reformat the all-caps text messages as they were distributed by the National Weather Service until May 10, 2016, when this format was finally retired.
These messages usually appeared something like
Your task is to write a shell script that accepts one of these messages and reformats it.
The Unix tools that your shell script may use are
tr
fmt
sed
mv and cp
grep, fgrep, and egrep
cat
sort
You can use any built-ins that you like from bash, including such items as echo, iteration constructions, and variable assignment.
You may not use any other general programming language, such as C, Perl, Python, or even awk.
Specifications
Your program should look to see if there is an argument; if so, then it should assume that the argument is a file name and try to work on that file. If there is no file, it should assume that input is coming from standard input.
All output should go to standard out.
Please name your shell script NWS.sh.
The new file should be reformatted in the following manner:
The resulting output to stdout should be formatted to 100 columns.
Word spacing inside of paragraphs should be made uniformly one space between words. After commas, periods, or ellipses, you can choose what kind of spacing rules you like as long as the results resemble ordinary typed English.
Spacing outside of paragraphs should stay the same.
Case should be changed to normal English rather than all-caps.
[However, with respect to proper names, I recognize that this problem is non-deterministic, and you may spend as much (or as little) time as you like on the proper names problem. (Hint: to do this, you can create static (or even dynamic) data files that provide spelling hints.)]
For instance, the previous message could be reformatted to:
Explanation / Answer
Hi there,
I wrote detailed comments along with the code. Put the code in a code editor save as a bash file for proper readablility.. For better understanding use man <command_name> in your linux / unix system. Read about tr, fmt, sed, grep, cat. I used just these. All of these need a good understanding and also a good experience with them, so don't straight out feel like 'I can't understand!'.
#!/bin/bash
# If number of command line arguments > 0
if [ $# -gt 0 ]
then
# cat the data in file and store in a
# variable.
dat="$(cat $1)"
else
# Else read from stdin and store in
# 'dat' variable.
read dat
fi
# Echoes the data inside the variable.
echo "$dat" |
# Translate all upper case to lower case
tr '[:upper:]' '[:lower:]' |
# Formats to a with of 100 columns
fmt --width=100 |
# Removes all spaces that are 1 space or
# greater and replaces with single space
# Does not replace new lines.
sed -e "s/s{1,}/ /g" |
# Change words with a leading fullstop and
# space to uppercase.
sed -e "s/.s(.)/. u/g" |
# First letter of every line to capital
# '^' used to select the first letter here.
sed -e "s/^(.)/u/g"