0

我有一个文件 ( mirrorlist.pacnew) 包含这样的镜像:

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
...

我应该选择镜子并取消注释。然而,一个工具rankmirrors为我确定了最好的镜像,所以我sed习惯于取消所有它们的注释。

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
...

我保留注释行,因为 rankmirrors 打印它们并且我可以跟踪进度(它不打印它正在处理的未注释行)。

但是,我也想sedawk打印每行中的服务器计数和总计数

具体来说:

  1. 取消注释行,就像我在上面给出的示例一样。
  2. 从原始文件打印当前#Server索引的索引(不是实际的行号,因为文件包含县名、通用注释)。

最终输出看起来有点像这样:

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

是完整文件的副本。

编辑:

我自己也取得了一些进展,我将我的工作添加为答案,因为它实现了上述目标,但不是最佳的。

4

2 回答 2

2

将同一文件两次传递给 awk。第一关,数一数。第二次传球,替补。

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

给出:

## 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
于 2015-07-16T11:42:41.217 回答
1

sed并且grep只有:

prakhar@inS4n3 ~ $ COUNT=$(grep -c "Server" /etc/pacman.d/mirrorlist.pacnew); cat /etc/pacman.d/mirrorlist.pacnew | sed -r 's/^#([^#]+)/\1/;tx;d;:x'| sed = | sed 'N;s/\n/ /' | sed -r 's/([0-9]+?)\sServer\s=\s(.*)/#\1 \/ '$COUNT' Trying \2\nServer = \2/'
...
#241 / 247 Trying http://mirrors.rutgers.edu/archlinux/$repo/os/$arch
Server = http://mirrors.rutgers.edu/archlinux/$repo/os/$arch
#242 / 247 Trying http://mirror.umd.edu/archlinux/$repo/os/$arch
Server = http://mirror.umd.edu/archlinux/$repo/os/$arch
#243 / 247 Trying http://mirror.vtti.vt.edu/archlinux/$repo/os/$arch
Server = http://mirror.vtti.vt.edu/archlinux/$repo/os/$arch
#244 / 247 Trying http://mirrors.xmission.com/archlinux/$repo/os/$arch
...

待办事项

  1. 我很确定这不是最优的。
  2. 难以阅读
  3. 删除通用评论(#Worldwide

编辑:处理一般评论:

user@host $ RANDOM_CHARACTER='@'
user@host $ sed ':b;N; $!bb; s|\n|'"$RANDOM_CHARACTER"'|g;s/#Server/#\nServer/g' /etc/pacman.d/mirrorlist.pacnew | \
    sed '2,$=' | \
    sed -r '/^[0-9]*$/{s|(.*)|echo "$((\1-1))/'$COUNT' "|e; N; s|\n([^'"$RANDOM_CHARACTER"']*)|\1'"$RANDOM_CHARACTER"'\1|}' | \
    sed ':b;N; $!bb;s|\n||g;s|'"$RANDOM_CHARACTER"'|\n|g'

根据文件内容选择随机字符 - 文件中不存在且不用作 sed 命令的分隔符的任何字符。

于 2015-07-16T11:57:48.350 回答