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.xxxxaftergeo:(no spaces) - add
,afterxx.xxxx(no spaces) - add
yy.yyyyafter,(no spaces) - cut
/from the end - add
,0to 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
This oneliner is basically what you need:
Explanation:
xclip -ooutputs the X selection to the standard outputsed <regex>parses the format you gave (ignoring leading+'es) and prints the replacement text-rswitch instructs thesedto interpret regular expressions as Extended Regular Expressions (ERE) (quick intro here),-nsuppresses the output of (unmatched/unwanted) input -- so we have to explicitly print with thepcommand (the last letter in sed script)-e scriptdefines the sed script:s/regexp/replacement/will matchregexpin each line of input (only the first occurrence) and replace it withreplacement(which can include input groups, like\1). Thepin the sed script actually prints the replacement text.<optional +>(<optional -><one or more digits/dot>). Parentheses define a group which we use in the replacement.xclip -isets X selection from stdin (sed's output)