0

Trying to do the following, I have txt file which as the data of the interfaces.

Eth1/15       desc01   1
Eth101/1/11   desc01   1
Eth101/1/16   desc01   1
Eth103/1/21   desc01   1
Eth1/2        desc01   1
Eth1/24       desc01   1
Eth103/1/5    desc01   1
Gi0/1         desc01   1
Gi0/0/1       desc01   1
Gi0/1/1       desc01   1

Wanted to find out the common Interface type Expected results:

If / contains twice then count the matching, else just Gi0 or Eth1

Eth1, Eth101/1, Eth103/1, Gi0, Gi0/0, Gi0/1

I tried using the AWK, what but not getting the results. cat findint.txt | egrep 'Eth|Gi' | awk -F"/" '{print $1}'

I tried following

cat findint.txt | egrep 'Eth|Gi' | \ awk '{print substr($1, 0, length($1)-2)}' | sort -u

Results, some values are repeated which I don't.

Expected Results: Eth1, Eth101/1, Eth103/1, Gi0, Gi0/0, Gi0/1

1 Answer 1

4
$ awk -F"/" '!seen[$1]++ {print $1}' findint.txt 
Eth1
Eth101
Eth103
Gi0

To get the output all in one comma-separated line, you could set the output separator ORS to , however you'd need to make a special case for the the last value; a simpler way is to pipe the output of awk to paste:

awk -F"/" '!seen[$1]++ {print $1}' findint.txt | paste -sd,
9
  • Thanks, never use seen interesting Commented Jun 13, 2019 at 18:40
  • 1
    @netkool seen is just an array - its name is arbitrary (you could use a[$1]++ or foo[$1]++ etc.) Commented Jun 13, 2019 at 18:41
  • 1
    @netkool, this is a awk idiom to do something only the very first time some field is encountered. Commented Jun 13, 2019 at 18:43
  • Thanks @steeldriver for the explanation, make sense now, is it possible to print in one line with comma Eth1,Eth101,Eth103,Gi0 removing the \n thanks Commented Jun 14, 2019 at 19:16
  • @netkool please see updated answer Commented Jun 14, 2019 at 20:40

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.