3

I am trying to convert text copied to the clipboard from something like this:

+50.8863-114.0157/

to something like this:

geo:50.8927777778,-114.013055556,0

I found this code on the Web:

#!/bin/bash
x="geo:"$(xclip -o | tr -d ' ')
notify-send $x -i info
xclip -selection c

but it just removes the white space. What I need to do is: having +xx.xxxx-yy.yyyy/ in clipboard where x and y are numbers 0 - 9

  • extract the text from the clipboard as an argument
  • cut + from the beginning
  • add geo: in the beginning
  • add xx.xxxx after geo: (no spaces)
  • add , after xx.xxxx (no spaces)
  • add yy.yyyy after , (no spaces)
  • cut / from the end
  • add ,0 to the end (no spaces)
  • return the result to the clipboard

ADDED LATER I figured that out myself. Here is the code that worked:

clipboard_original="$(xclip -o)"
latitude=${clipboard_original:0:8}
longitude=${clipboard_original:8:9}
clipboard_for_digikam_geo=""geo:""${latitude//+}"00000,"${longitude//+}"00000"
echo "$clipboard_for_digikam_geo" | xclip -selection c
exit
  • What does "did not work" mean? What were you expecting, and what did you get? Did you look at the man pages for `xclip` and `tr` (`man PROGRAM_NAME`)? `xclip -o` outputs the contents of the clipboard, which is what you want here. Piping (`|`) it to another program is appropriate here. What do you think `tr -d ' '` does? A quick look at the man page shows that it deletes spaces from its input string. Not at all what you want. `sed` would do what you want, or `awk`. WARNING: those are not simple tools (but oh so powerful). `xclip -selection c` is also not doing anything useful... – Edward May 16 '17 at 20:48

1 Answers1

2

This oneliner is basically what you need:

xclip -o | sed -rne's/\+?(-?[[:digit:].]+)\+?(-?[[:digit:].]+)\//geo:\1,\2,0/p' | xclip -i

Explanation:

  • xclip -o outputs the X selection to the standard output
  • sed <regex> parses the format you gave (ignoring leading +'es) and prints the replacement text
    • -r switch instructs the sed to interpret regular expressions as Extended Regular Expressions (ERE) (quick intro here),
    • -n suppresses the output of (unmatched/unwanted) input -- so we have to explicitly print with the p command (the last letter in sed script)
    • -e script defines the sed script:
      • s/regexp/replacement/ will match regexp in each line of input (only the first occurrence) and replace it with replacement (which can include input groups, like \1). The p in the sed script actually prints the replacement text.
      • regexp (in short) is made up of two identical consecutive subpatterns: <optional +>(<optional -><one or more digits/dot>). Parentheses define a group which we use in the replacement.
  • xclip -i sets X selection from stdin (sed's output)
randomir
  • 17,989
  • 1
  • 40
  • 55
  • Thank you for your help. I am having a hard time understanding sed's syntax so I just used bash's internal tools. It is not a oneliner but it works and I can actually read it. :) I pasted the code to my initial post. Again, thank you for your help anyways! – AndriusWild May 17 '17 at 18:15
  • @AndriusWild I've added explanation on `sed` invocation/script. If you prefer pure `bash` solution, check the `=~` regular expression matching operator within double brackets test expression (available since bash v3 -- so it works even on OS X :)), combined with `BASH_REMATCH` variable. – randomir May 17 '17 at 20:11