0

I am running geoiplookup command on linux, which gives the following:

[root@localhost temp]$ geoiplookup 8.8.8.8

GeoIP Country Edition: US, United States
GeoIP City Edition, Rev 1: US, N/A, N/A, N/A, 38.000000, -97.000000, 0, 0
GeoIP City Edition, Rev 0: US, N/A, N/A, N/A, 38.000000, -97.000000
GeoIP ASNum Edition: AS15169 Google Inc.

I want the output to be on one line and have tried this:

[root@localhost temp]$ geoiplookup 8.8.8.8 | sed '2,3d' | sed 's/^.*: /,/g' | sed '$!{:a;N;s/\n/\t/;ta}' | awk -F ',' '{print $4","$2}'
AS15169 Google Inc.,US

Sorry to make you misunderstand. I want to use easier way to keep the country code and ASNum as the output line.

Like thie "AS15169 Google Inc.,US"

Tazugan
  • 129
  • 3
  • 3
  • 9

4 Answers4

1

The paste command will do what you want.

geoiplookup 8.8.8.8 | paste -sd " "

See also How do I remove newlines from a text file?

Community
  • 1
  • 1
bmb
  • 6,058
  • 2
  • 37
  • 58
1
geoiplookup 8.8.8.8 | awk '/^GeoIP Country/{ sub(/,*$/, "", $4); c = $4; } sub(/^GeoIP ASNum Edition: /, ""){ print $0 "," c }'

Expected output:

AS15169 Google Inc.,US

Edit: sub is enough. No need to go gsub.

konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • that works great. Thanks for you answer, learn more thing about awk as well – Tazugan Jul 16 '14 at 06:29
  • @Tazugan Welcome :) You can also consider reading the user's guide if you're using GNU awk: http://www.gnu.org/software/gawk/manual/gawk.html. – konsolebox Jul 16 '14 at 06:35
0

Does this work for you:

geoiplookup 8.8.8.8 | sed '2,3d' | sed 's/^.*: /,/g' | sed '$!{:a;N;s/\n/\t/;ta}' | awk -F ',' '{print $8","$2}'
nbkr
  • 121
  • 5
0

A very simple technique is:

echo $(geoiplookup 8.8.8.8)

Note the absence of double quotes around the $(…) command substitution — contrast with Capturing multiple line output to a bash variable.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278