Please help, I am stuck on Linux operating system bash shell scripting: Write th
ID: 3727071 • Letter: P
Question
Please help, I am stuck on Linux operating system bash shell scripting:
Write the bash shell script that uses a combination of the Linux commands to help summarize the data contained in an external file (cat, grep, cut, numaverage, uniq, numbound, sort, wc )
- use the man ommand to help learn more about Linux commands.
-The script has to accept the name of the external file as a command line argument
- The script has to display the total number of particles recorded in the file
-The script has to summarize the data of each unique particle species:
-- dont create temporary files to store data (chain commands together with piping)
-- All decimal values should be displayed to six decimal places
Name of particle species
Total number of occurrences of the species
Minimum momentum of the species
Maximum momentum of the species
Average momentum of the species
2 data files are provided to use with the BASH script small.dat and large.dat
Use the small.dat file to test and perfect the BASH script
Use the large.dat file to produce the output
ex: (using the small.dat file)
Contents sub set of the small.dat file
Summary of the complete small.dat file
Thank you!
Explanation / Answer
Hi,
Below is the code to get the count, sum and average for a given column in the file. I have written the code for the sample data as given. Please use the column headings instead for the purpose and replace the files name "a.txt" with "small.dat" file name.
# Your code here!
# Hello World Program in Bash Shell
echo "Hello World!"
echo "NEUTRON 20.900103" > a.txt
echo "PION- 0.215176" >> a.txt
echo "PION- 22.716532" >> a.txt
echo "NEUTRON 8.043279" >> a.txt
echo "PION+ 1.374297" >> a.txt
echo "PION- 0.313350" >> a.txt
echo "PION+ 0.167848" >> a.txt
cat a.txt
awk '$1 != v {
if (NR>1)
printf("%s max=%d,min=%d,count=%d,avg=%.2f ", v, max, min, k, (sum/k));
v=$1;
min=$2;
k=sum=max=0
}
{
k++;
sum+=$2;
if (min > $2)
min=$2;
if (max < $2)
max=$2
}
END {
printf("%s max=%d,min=%d,count=%d,avg=%.2f ", v, max, min, k, (sum/k))
}' a.txt