bashxclip

Bash - change text copied to the clipboard using xclip


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


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

Solution

  • This oneliner is basically what you need:

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

    Explanation: