有没有办法创建一个sed或awk语句来替换代码中的字符与另一个范围中的字符

时间:2012-05-25 23:49:54

标签: sed awk

背景:MS向OEM公司发布了COA标签,这些标签在磨损方面存在严重问题。使用的墨粉未正确粘合到COA标签纸上。随着时间的推移,产品密钥会降级,某些字母/数字不再可读,禁止输入授权密钥进行重新安装。

问题:当只有几个字母/数字被损坏时,尝试确定以前尝试过哪个号码是非常令人沮丧的。

请求:使用sed或awk(最好)或其他方法输入STRING,例如

A{B,E,R,8}CDE-FGH{I,1}J-KLMN{O,Q,0}-PQRST-UVWXY

并收到

的输出
ABCDE-FGHIJ-KLMNO-PQRST-UVWXY
AECDE-FGHIJ-KLMNO-PQRST-UVWXY
ARCDE-FGHIJ-KLMNO-PQRST-UVWXY
A8CDE-FGHIJ-KLMNO-PQRST-UVWXY
ABCDE-FGH1J-KLMNO-PQRST-UVWXY
AECDE-FGH1J-KLMNO-PQRST-UVWXY
ARCDE-FGH1J-KLMNO-PQRST-UVWXY
A8CDE-FGH1J-KLMNO-PQRST-UVWXY
ABCDE-FGHIJ-KLMNQ-PQRST-UVWXY
AECDE-FGHIJ-KLMNQ-PQRST-UVWXY
ARCDE-FGHIJ-KLMNQ-PQRST-UVWXY
A8CDE-FGHIJ-KLMNQ-PQRST-UVWXY
ABCDE-FGH1J-KLMNQ-PQRST-UVWXY
AECDE-FGH1J-KLMNQ-PQRST-UVWXY
ARCDE-FGH1J-KLMNQ-PQRST-UVWXY
A8CDE-FGH1J-KLMNQ-PQRST-UVWXY
ABCDE-FGHIJ-KLMN0-PQRST-UVWXY
AECDE-FGHIJ-KLMN0-PQRST-UVWXY
ARCDE-FGHIJ-KLMN0-PQRST-UVWXY
A8CDE-FGHIJ-KLMN0-PQRST-UVWXY
ABCDE-FGH1J-KLMN0-PQRST-UVWXY
AECDE-FGH1J-KLMN0-PQRST-UVWXY
ARCDE-FGH1J-KLMN0-PQRST-UVWXY
A8CDE-FGH1J-KLMN0-PQRST-UVWXY

2 个答案:

答案 0 :(得分:5)

你在命令行上试试了吗?至少在bash中,它“只是有效”:

> echo A{B,E,R,8}CDE-FGH{I,1}J-KLMN{O,Q,0}-PQRST-UVWXY
ABCDE-FGHIJ-KLMNO-PQRST-UVWXY ABCDE-FGHIJ-KLMNQ-PQRST-UVWXY ABCDE-FGHIJ-KLMN0-PQRST-UVWXY ABCDE-FGH1J-KLMNO-PQRST-UVWXY ABCDE-FGH1J-KLMNQ-PQRST-UVWXY ABCDE-FGH1J-KLMN0-PQRST-UVWXY AECDE-FGHIJ-KLMNO-PQRST-UVWXY AECDE-FGHIJ-KLMNQ-PQRST-UVWXY AECDE-FGHIJ-KLMN0-PQRST-UVWXY AECDE-FGH1J-KLMNO-PQRST-UVWXY AECDE-FGH1J-KLMNQ-PQRST-UVWXY AECDE-FGH1J-KLMN0-PQRST-UVWXY ARCDE-FGHIJ-KLMNO-PQRST-UVWXY ARCDE-FGHIJ-KLMNQ-PQRST-UVWXY ARCDE-FGHIJ-KLMN0-PQRST-UVWXY ARCDE-FGH1J-KLMNO-PQRST-UVWXY ARCDE-FGH1J-KLMNQ-PQRST-UVWXY ARCDE-FGH1J-KLMN0-PQRST-UVWXY A8CDE-FGHIJ-KLMNO-PQRST-UVWXY A8CDE-FGHIJ-KLMNQ-PQRST-UVWXY A8CDE-FGHIJ-KLMN0-PQRST-UVWXY A8CDE-FGH1J-KLMNO-PQRST-UVWXY A8CDE-FGH1J-KLMNQ-PQRST-UVWXY A8CDE-FGH1J-KLMN0-PQRST-UVWXY

或在单独的行上/在其他命令中使用:

> for a in A{B,E,R,8}CDE-FGH{I,1}J-KLMN{O,Q,0}-PQRST-UVWXY; do echo $a; done
ABCDE-FGHIJ-KLMNO-PQRST-UVWXY
ABCDE-FGHIJ-KLMNQ-PQRST-UVWXY
ABCDE-FGHIJ-KLMN0-PQRST-UVWXY
ABCDE-FGH1J-KLMNO-PQRST-UVWXY
ABCDE-FGH1J-KLMNQ-PQRST-UVWXY
ABCDE-FGH1J-KLMN0-PQRST-UVWXY
AECDE-FGHIJ-KLMNO-PQRST-UVWXY
AECDE-FGHIJ-KLMNQ-PQRST-UVWXY
AECDE-FGHIJ-KLMN0-PQRST-UVWXY
AECDE-FGH1J-KLMNO-PQRST-UVWXY
AECDE-FGH1J-KLMNQ-PQRST-UVWXY
AECDE-FGH1J-KLMN0-PQRST-UVWXY
ARCDE-FGHIJ-KLMNO-PQRST-UVWXY
ARCDE-FGHIJ-KLMNQ-PQRST-UVWXY
ARCDE-FGHIJ-KLMN0-PQRST-UVWXY
ARCDE-FGH1J-KLMNO-PQRST-UVWXY
ARCDE-FGH1J-KLMNQ-PQRST-UVWXY
ARCDE-FGH1J-KLMN0-PQRST-UVWXY
A8CDE-FGHIJ-KLMNO-PQRST-UVWXY
A8CDE-FGHIJ-KLMNQ-PQRST-UVWXY
A8CDE-FGHIJ-KLMN0-PQRST-UVWXY
A8CDE-FGH1J-KLMNO-PQRST-UVWXY
A8CDE-FGH1J-KLMNQ-PQRST-UVWXY
A8CDE-FGH1J-KLMN0-PQRST-UVWXY

或者你可能已经知道这个并且因为其他原因需要sed / awk?

答案 1 :(得分:0)

我可能会为此选择Ruby,Python或Perl。如果它只是为了我,我会做一些超级便宜的东西并使用一系列嵌套的for循环,如下面的伪代码:

string = "A%sCDE-FGH%sJ-KLMN%s-PQRST-UVWXY"

for a in ["B", "E", "R", "8"]
  for b in ["I", "1"]
    for c in ["O" "Q", "0"]
        printf string, a, b, c
    end
  end
end

缺点是你需要替换字符串,每个for循环中的字符列表,并可能根据未知字符的数量添加或删除for循环。如你所描述的那样,使它成为一个更强大的工具肯定会付出更多的努力,但如果你想为其他人设计一个工具,可能会有所不同。