1

Running

ip -o -f inet addr show | grep $INTERNAL |awk '/scope global/ {print $4}' 

Want to replace the / in my output to _ so rather than reading

10.168.122.59/16 

it reads as

10.168.122.59_16 

.

|sed s///_/  

didnt help

Any suggestions?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Shadd
  • 23
  • 3

3 Answers3

2

All the postprocessing requested can be done internal to awk. Expanding a one-liner provided in a comment by @123 for better readability, this can look like the following:

ip -o -f inet addr show | \
  awk -v i="$INTERNAL" '
    $0 ~ i && /scope global/ { 
      sub(/\//, "_", $4);
      print $4;
    }'

Breaking down how this works:

  • awk -v i="$INTERNAL" defines an awk variable based on a shell variable. (As an aside, all-caps shell variable names are bad form; per POSIX convention, lowercase names are reserved for application use, whereas all-caps names can have meaning to the OS and surrounding tools).
  • $0 ~ i filters for the entire line ($0) matching the awk variable i.
  • /scope global/ by default is applied as a regex against $0 as well (it's equivalent to $0 ~ /scope global/).
  • sub(/\//, "_", $4) substitutes /s with _s in the fourth field.
  • print $4 prints that field.
Community
  • 1
  • 1
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
1

You need to scape the / or use a different separator as below:

echo 10.168.122.59/16 | sed s:/:_:

Carles Fenoy
  • 4,740
  • 1
  • 26
  • 27
0
echo 10.168.122.59/16| awk '{sub(/\//,"_")}1'
10.168.122.59_16
Claes Wikner
  • 1,457
  • 1
  • 9
  • 8