AWK脚本自动从String中删除前导0

时间:2016-03-07 19:46:22

标签: linux bash awk

我有一个文件BLACK.FUL.eg2:

<script type="text/javascript">
$(function () {
    var view = $(document).findByClass("product-view");
    var companysource = view.data("datasource-url");
    $("#txtcompany").autocomplete({
        minLength: 0,
        source: function (request, resonse) {
            $.ajax({
                url: companysource,
                data: { term: $('#txtcompany').val() },
                dataType: "json",
                type: "GET",
                success: function (data) {
                    resonse(data);
                }
            });
        },
        focus: function (event, ui) {
            $("#txtcompany").val(ui.item.Name);
            return false;
        },
        select: function (event, ui) {
            $("#txtcompany").val(ui.item.Name);
            return false;
        },
        change: function (event, ui) {
            if (ui.item == null) {

            } else {

            }
        }
    })
    .data("ui-autocomplete")._renderItem = function (ul, item) {
        return $("<li>")
            .data("ui-autocomplete-item", item)
            .append("<div style='margin-bottom:2px; padding:1px 1px; font-size:14px;'><a>" + "<b>Company Name: </b>" + item.Name + "</a></div>")
            .appendTo(ul);
    };
});

我写过这个AWK脚本:

10>BLACK.FUL>272/GSMA/000000>151006>01
15>004401074905590>004401074905590>B>I>0011>Insert>240/PLMN/000100>>5000-K525122-15
15>004402145955010>004402145955010>B>I>0011>Insert>240/PLMN/000100>>1200-K108534-14
15>004402146016260>004402146016360>B>I>0011>Insert>240/PLMN/000100>>1200-K-94878-14
15>004402452698630>004402452698630>B>I>0011>Insert>240/PLMN/000100>>5000-K538947-14
90>BLACK.FUL>272/GSMA/000000>151006>01>4

给出了输出:

awk 'NR > 2 { print p } { p = $0 }' BLACK.FUL.eg2 | awk -F">" \
'{if (length($2) == 15) print substr($2,1,length($2)-1)","substr($3,1,length($3)-1)","$6","$8; \
else print $2","$3","$6","$8;}' | awk -F"," '{if ($2 == $1) print $1","$3","$4; \
else {if (length($1) > 14) {v = substr($1,9,6); t = substr($2,9,6); \
while(v <= t) print substr($2,1,8)v++substr($2,15,2)","$3","$4;} \
else {d = $1;while(d <= $2) print d++","$3","$4;}}}'

有一个问题:field1中字符串的前导0会因为对它们进行数字运算而自动删除。所以我的实际预期输出是:

00440107490559,0011,240/PLMN/000100
00440214595501,0011,240/PLMN/000100
440214601626,0011,240/PLMN/000100
440214601627,0011,240/PLMN/000100
440214601628,0011,240/PLMN/000100
440214601629,0011,240/PLMN/000100
440214601630,0011,240/PLMN/000100
440214601631,0011,240/PLMN/000100
440214601632,0011,240/PLMN/000100
440214601633,0011,240/PLMN/000100
440214601634,0011,240/PLMN/000100
440214601635,0011,240/PLMN/000100
440214601636,0011,240/PLMN/000100
00440245269863,0011,240/PLMN/000100

为此,我尝试了以下更新的AWK脚本:

00440107490559,0011,240/PLMN/000100
00440214595501,0011,240/PLMN/000100
00440214601626,0011,240/PLMN/000100
00440214601627,0011,240/PLMN/000100
00440214601628,0011,240/PLMN/000100
00440214601629,0011,240/PLMN/000100
00440214601630,0011,240/PLMN/000100
00440214601631,0011,240/PLMN/000100
00440214601632,0011,240/PLMN/000100
00440214601633,0011,240/PLMN/000100
00440214601634,0011,240/PLMN/000100
00440214601635,0011,240/PLMN/000100
00440214601636,0011,240/PLMN/000100
00440245269863,0011,240/PLMN/000100

但是收到错误:

awk 'NR > 2 { print p } { p = $0 }' BLACK.FUL.eg2 | awk -F">" \
'{if (length($2) == 15) print substr($2,1,length($2)-1)","substr($3,1,length($3)-1)","$6","$8; \
else print $2","$3","$6","$8;}' | awk -F"," '{if ($2 == $1) print $1","$3","$4; \
else {if (length($1) > 14) {v = substr($1,9,6); t = substr($2,9,6); \
while(v <= t) print substr($2,1,8)v++substr($2,15,2)","$3","$4;} \
else {d = $1; for ( i=1;i<length($1);i++ ) if (substr($1,i++,1) == "0") \
{m=m"0"; else exit 1;}; while(d <= $2) print md++","$3","$4;}}}'

请你强调一下我做错了什么来达到预期的输出。仅对我现有的AWK脚本进行修改会有很大帮助。感谢

注意:前导0可以是任意数量的occcurence,不仅在每种情况下都是2 0,如上例输出中所示。

1 个答案:

答案 0 :(得分:4)

由于您的字段大小是固定的,因此对于给定的示例,只需将最后一个打印语句更改为

$ awk ... printf "%014d,%s,%s\n",d++,$3,$4}}}'

00440107490559,0011,240/PLMN/000100
00440214595501,0011,240/PLMN/000100
00440214601626,0011,240/PLMN/000100
00440214601627,0011,240/PLMN/000100
00440214601628,0011,240/PLMN/000100
00440214601629,0011,240/PLMN/000100
00440214601630,0011,240/PLMN/000100
00440214601631,0011,240/PLMN/000100
00440214601632,0011,240/PLMN/000100
00440214601633,0011,240/PLMN/000100
00440214601634,0011,240/PLMN/000100
00440214601635,0011,240/PLMN/000100
00440214601636,0011,240/PLMN/000100
00440245269863,0011,240/PLMN/000100

<强>更新

如果您的字段大小未固定,您可以捕获长度(或所需长度)并使用相同的模式。由于您的代码太复杂,我将编写一个可以嵌入到脚本中的概念证明。

这基本上是你的问题,增加一个零填充数字并且前导零被删除。

$ echo 0001 | awk '{$1++; print $1}'
2

这是建议的解决方案,其参数长度为零填充。

$ echo 0001 | awk '{n=length($1); $1++; printf "%0"n"s\n", $1}'
0002