AWK/GREP sorting help

hi everyone, I am kind of new to this forum. I need help in sorting this data out accordingly, I am actually doing a traceroute application and wants my AS path displayed in front of my address like this;
192.168.1.1 AS28513 AS65534 AS5089 AS5089 .... till the last AS number and if possible sort for uniq AS number so that there wont repetion as it occurs below.

From data like below;

 1  192.168.1.1 (192.168.1.1) [AS28513]  4.305 ms  4.150 ms  5.069 ms
 2  10.193.108.1 (10.193.108.1) [AS65534]  17.308 ms  17.716 ms  18.170 ms
 3  leic-cam-1a-v134.network.virginmedia.net (82.3.36.29) [AS5089]  19.280 ms  19.715 ms  20.731 ms
 4  leic-core-1a-ae1-0.network.virginmedia.net (195.182.174.129) [AS5089]  20.226 ms  23.771 ms  24.368 ms
 5  leed-bb-1a-as8-0.network.virginmedia.net (213.105.172.17) [AS5089]  27.425 ms  26.590 ms  31.437 ms
 6  leed-bb-1b-ae0-0.network.virginmedia.net (62.253.187.186) [AS5089]  32.144 ms  42.170 ms  42.660 ms
 7  nrth-bb-1a-as0-0.network.virginmedia.net (213.105.175.133) [AS5089]  17.566 ms  17.743 ms  21.289 ms
 8  fran-ic-1-as0-0.network.virginmedia.net (62.253.185.81) [AS5089]  37.432 ms  36.812 ms  37.800 ms
 9  te0-7-0-7.mpd22.fra03.atlas.cogentco.com (130.117.14.133) [AS174]  41.965 ms  42.538 ms  42.975 ms

Thanks for you anticipated help.

traceroute X.X.X.X | awk 'BEGIN{ORS=" "}{if(NR==1)print $2,$4;else print $4;}'

You may pipe a sed command to remove the [ and ] brackets...

1 Like

You can remove the square brackets in the same awk; added a RS at the end (not sure if needed):

awk 'BEGIN{ORS=" "}{gsub(/[][]/,"",$4); if(NR==1)print $2,$4;else print $4}END{print RS}' infile
192.168.1.1 AS28513 AS65534 AS5089 AS5089 AS5089 AS5089 AS5089 AS5089 AS174

Edit:
Sorry, just noticed you wanted also to remove duplicates and a sort too.

1 Like

Please post the script which does the "traceroute" making it clear what Operating System you have and what Shell you are using.

The syntax for "traceroute" and the processing a "traceroute" output varies considerably. Many versions of the program output part of the output to STDOUT and part to STDERR.

The syntax for "awk", "sort" etc. also varies.

1 Like

Hi, thanks for your urgent help, the traceroute is same as usual one.....traceroute6 -A ::1
But i discovered some repetition of some ASes along each line;

to (2001:1ba0:2a0:5965:0:30:14:1), AS786 AS786 AS786 AS786 AS786 AS786 AS786 * AS29208 AS29208 AS29208 AS29208 AS29208
to (2001:da8:208:100::121), AS786 AS786 AS786 AS786 AS786 AS786 AS20965 AS20965 AS20965 * * * * * * * * * * * * * * * * * * * * *

how can I sort it out to avoid repetitions of AS number along each line?
thanks

(I guess this is some Linux variant with some version of traceroute6).

yes it is, thanks a lot

I didn't make it in one awk instruction but nevertheless:

awk 'NR==1{h=$2} {gsub(/[]AS[]/,"",$4); _[$1]=$4} END{print h; for(a in _){if(f){f=f RS _[a]}else{f=_[a]}}; print f | "sort -n| uniq"}' infile| awk 'NR==1 {printf("%s", $1)} NR>1 {printf(" AS%s"), $1} END{printf RS}'
192.168.1.1 AS174 AS5089 AS28513 AS65534

Done with GNU awk; maybe someone got a more compact version.

---------- Post updated at 04:04 PM ---------- Previous update was at 03:47 PM ----------

Removed the substitution and pasting of A and S (AS) so that sort can take S as delimeter for fields.

awk 'NR==1{h=$2} {gsub(/[][]/,"",$4); _[$1]=$4} END{print h; for(a in _){if(f){f=f RS _[a]}else{f=_[a]}}; print f | "sort -tS -n -k2| uniq"}' infile| awk 'NR==1 {printf("%s", $1)} NR>1 {printf(" %s"), $1} END{printf RS}'
192.168.1.1 AS174 AS5089 AS28513 AS65534
1 Like

Thanks a million