regex - Insert counts at specific positions using bash (sed or awk?) -


i have file (mirrorlist.pacnew) containing mirrors this:

prakhar@ins4n3 ~ $ cat /etc/pacman.d/mirrorlist.pacnew  ... ## worldwide #server = https://dgix.ru/mirrors/archlinux/$repo/os/$arch #server = http://mirror.rackspace.com/archlinux/$repo/os/$arch  ## australia #server = http://mirror.aarnet.edu.au/pub/archlinux/$repo/os/$arch ... 

i supposed choose mirrors , uncomment those. however, tool rankmirrors determines best mirrors me, use sed uncomment of them.

prakhar@ins4n3 ~ $ cat /etc/pacman.d/mirrorlist.pacnew | sed -r 's/^#([^#]+)/#\1\n\1/' ... ## worldwide #server = https://dgix.ru/mirrors/archlinux/$repo/os/$arch server = https://dgix.ru/mirrors/archlinux/$repo/os/$arch #server = http://mirror.rackspace.com/archlinux/$repo/os/$arch server = http://mirror.rackspace.com/archlinux/$repo/os/$arch  ## australia #server = http://mirror.aarnet.edu.au/pub/archlinux/$repo/os/$arch server = http://mirror.aarnet.edu.au/pub/archlinux/$repo/os/$arch ... 

i keeping commented lines because rankmirrors prints them , can keep track of progress (it doesn't print un-commented lines processing).

however, sed or awk print server count , total count in each line well.

specifically:

  1. uncomment lines gave example above.
  2. print index of current #server index (not actual line number, file contains county names, generic comments) original file.

the final output this:

#22/247 server = http://mirror.aarnet.edu.au/pub/archlinux/$repo/os/$arch server = http://mirror.aarnet.edu.au/pub/archlinux/$repo/os/$arch 

here's copy of full file.

edit:

i have made progress myself, , added work an answer, achieves above, not optimally.

passing same file twice awk. first pass, count. second pass, substitute.

awk 'nr==fnr {          if( /^#server *=/)count++;          next;      }      /#server *=/{          sub(/^#*/,"");          print "#" ++i "/" count " " $0;     }     1' serverlist serverlist 

gives:

## worldwide #1/3 server = https://dgix.ru/mirrors/archlinux/$repo/os/$arch server = https://dgix.ru/mirrors/archlinux/$repo/os/$arch #2/3 server = http://mirror.rackspace.com/archlinux/$repo/os/$arch server = http://mirror.rackspace.com/archlinux/$repo/os/$arch  ## australia #3/3 server = http://mirror.aarnet.edu.au/pub/archlinux/$repo/os/$arch server = http://mirror.aarnet.edu.au/pub/archlinux/$repo/os/$arch 

Comments