Advertisement
DavidA122

"Text Processing" - r/scripting/drfu1w

Nov 5th, 2019
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.52 KB | None | 0 0
  1. #!/bin/bash
  2. # Takes data to be processed as an input file, outputting data to stdout (the terminal)
  3.  
  4. input_data="$1"
  5. lookup_file="materials.txt"
  6.  
  7. while IFS= read -r input_line; do
  8.   materialdimension1="$(echo ${input_line} | awk '{print $1}')"
  9.   material="${materialdimension1%%[0-9]*}"        # Strips all characters from the line starting from the first digit
  10.   #material="${input_line:0:3}"                     We can use this line instead if every material has a length of 3 characters, and is always the first entry in the line
  11.   dimension1="${materialdimension1##*[a-zA-Z]}"   # Strips all characters ahead of the last letter
  12.   # dimension1="${materialdimension1:3}"            We can use this line instead if, as above, every material has length 3
  13.  
  14.   dimensions2x3="$(echo ${input_line} | awk '{print $2}')"
  15.   dimension2="${dimensions2x3%%x*}"              # Strips all characters trailing an 'x'
  16.   dimension3="${dimensions2x3##*x}"              # Strips all characters preceeding an 'x'
  17.  
  18.   amount="$(echo ${input_line} | awk '{print $3}' | sed 's/x//')"   # Strips trailing 'x' from amount value
  19.  
  20.   volumecost="$(grep "${material}" "${lookup_file}" | awk '{print $2}')"   # Pulls cost/volume value from the lookup file for the current material
  21.  
  22.   total=$(( (dimension1 * dimension2 * dimension3 * amount * volumecost ) / 1000000 ))   # Multiply everything together, divide by 1,000,000
  23.  
  24.   echo "${total} - ${input_line}"   # Output the value, along with the input line (for demonstration purposes)
  25. done < "${input_data}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement