0

Currently working on a command in AppleScript to trim weight data we receive from a scale. We receive the weight in the following format X.XXX. lb. The Xs representing numbers. We're trimming out 6 spaces before the digits as well as 3 spaces,lb, and 5 more spaces.

Using the following command we've managed to trim characters and get the weight to read as X.XXX.. in our files metadata:

 | sed 's/[.^0-9]//g'

We're hoping to also trim off the two decimal points following the thousandth character in the weight data. Unsure of where to go.

EDIT:

To help make it more clear what exactly is happening we are using a program to write image files on MacOS. The section of script I'm working on is to trim this weight data before exiftool writes metadata for our image files. In my examples of the different states of the text above the Xs are all numbers that can always vary however the scale doesn't read past the thousandth decimal point. So after the first Decimal we will never get more than 3 numerical digits before the first decimal we may have up to 2 numerical digits in this case we would receive XX.XXX and would need to trim excess characters while not losing the numbers. hopefully this helps better understand the problem

if we weigh a package the scale sends the data to our program in the following:

     1.392.   lbs.    

we've managed to get our script to trim this to 1.392.. before writing metadata for the files. hoping to remove those last two decimals as well, while also making sure in the case of a weight reading being double digits i.e. 11.987 we can still perform the same trim.

I'm using a program to capture image files and using AppleScript to write .jpgs from the raw camera files. This is executed when a button is clicked in our program.

im receiving weight data from a scale in the following format:

enter image description here

the following section of our script is trimming that down:

set thePath to "Macintosh HD:Users:Shared:CS:"
        if thePath is equal to thePath then
            -- Format the weight value to remove extra spaces and decimals
            set cleanWeightValue to do shell script "echo " & weightValue & " | sed 's/[^0-9.]//g'"
            
            -- Update Exif metadata with cleanWeightValue
            do shell script "/usr/local/bin/exiftool '-Description=je " & cleanWeightValue & " lbs' " & quoted form of POSIX path of thePath & "front.JPG"
            do shell script "/usr/local/bin/exiftool '-Caption-Abstract=je " & cleanWeightValue & " lbs' " & quoted form of POSIX path of thePath & "front.JPG"
            do shell script "/usr/local/bin/exiftool '-ImageDescription=je " & cleanWeightValue & " lbs' " & quoted form of POSIX path of thePath & "front.JPG"
        end if

following this our weight reads in the following format in each image's metadata:

enter image description here

our goal is to also trim out those last two decimal points. wondering if this is possible building off our existing sed command

6
  • 2
    To make more clear what you want and to allow us to test proposed solutions, please edit your question and add a few linws of example input and the corresponding expected output.
    – Bodo
    Commented Aug 29, 2023 at 18:03
  • You mention AppleScript. Should we assume you are running macOS then? But in any case, we really need to see the whole file. Is your entire file nothing but a single line with X.XXX. lb.? Will there always be exactly one number, then a . and three other numbers? What can vary? What is invariable? We need all this to know what commands would work for you, so please edit your question and include some example input data. Maybe you just want awk '{print $1}' | sed 's/\.$//'
    – terdon
    Commented Aug 29, 2023 at 18:06
  • hello - I made some edits I hope this better explains the situation
    – rpior
    Commented Aug 29, 2023 at 18:20
  • Please copy&paste several lines of real example data, formatted as a code block (see the example of the command between ~~~ lines) and show the expected output for all input lines. If there can be variations, e.g. different numbers of digits, show different possible variants of input. You did not even show the expected output for your example input ``` 1.392. lbs. ```.
    – Bodo
    Commented Aug 29, 2023 at 18:28
  • 1
    Please do not upload images of code/data/errors. Copy e.g. 10 lines of different possible input as text and show the resulting expected output. Is there always at least one digit before and after a decimal dot? Is there always a decimal dot in the number? Is there always an additional dot after the number?
    – Bodo
    Commented Aug 29, 2023 at 18:51

1 Answer 1

0

Ignoring the discussion about the whole input thing: If sed 's/[^0-9.]//g' works for you except for the trailing dots, I suggest to simply remove trailing dots:

sed 's/[^0-9.]//g;s/\.*$//'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .