0

I'm trying to process a file which has many IP Addresses, the thing is that I want to geolocalizate those IPs with google maps, I did it with its corresponding longitude and latitude, however, I have a problem when create the Google Maps URL, the $13 variable has a comma (,) at the end. How can I delete that last character?

for i in $(awk '{ print ($NF)}' '/var/web/cgi-bin/auth.txt'); do
 htmlString+=$(geoiplookup -f /usr/share/GeoIP/GeoLiteCity.dat "$i" | awk '{ 
 print "<a target=\"_blank\" href=\"https://www.google.com/maps/search/?api=1&query=" $12 $13 "\">  '$i' </a><br>"}')
done

I want this: https://www.google.com/maps/search/?api=1&query=58.698017,-152.522067

but I got this: https://www.google.com/maps/search/?api=1&query=58.698017,-152.522067,

lak
  • 454
  • 6
  • 21
  • In general, `for x in $(...)` is bad practice. See http://mywiki.wooledge.org/BashFAQ/001 for a more robust approach to iterate over input line-by-line. – Charles Duffy Oct 25 '18 at 22:50
  • Getting towards the topic of the question, is it only commas you want to remove? `${i%,}` expands to a copy of `"$i"` with any trailing `,` removed. – Charles Duffy Oct 25 '18 at 22:51
  • ...alternately, for a pure awk approach, see the answer by Gilles at https://stackoverflow.com/a/14840991/14122; in this context, that would be `substr($13, 1, length($13)-1)` – Charles Duffy Oct 25 '18 at 22:53
  • (BTW, there's no particularly compelling reason to use `awk` here *at all*; is there a reason you aren't doing the string formatting in pure bash, if you *are* going to be using non-HTML-aware tools?) – Charles Duffy Oct 25 '18 at 22:55
  • Thank you, but I want to delete the comma of the $13 variable, I tried what you told me, but I got an error. – lak Oct 25 '18 at 22:55
  • "An error" isn't a detailed enough description to operate from. To see a working example of the suggested practice, you can try running `echo 'hi foo,bar,baz, qux' | awk '{print substr($2, 1, length($2)-1)}'` – Charles Duffy Oct 25 '18 at 22:57
  • Perfect, substr worked! Thank you! – lak Oct 25 '18 at 22:58
  • (To be clear, the `${i%,}` approach needs to be performed in a bash context, not an awk context). – Charles Duffy Oct 25 '18 at 22:59

0 Answers0