获取列名+迭代+排序+ awk

时间:2016-05-19 03:46:52

标签: bash awk

这是我的档案

$ cat -v test6 | head
"Rec_Open_Date"|"MSISDN"|"IMEI"|"Data_Volume_Bytes"|"Device_Manufacturer"|"Device_Model"|"Product_Description"|"Data_Volume_MB"|">20MB/30"|">200MB/30"|">2048MB/30"|">5120MB/30"|">10240MB/30"
"2015-10-06"|"427"|"060"|"137765"|"Samsung Korea"|"Samsung SM-G900I"|"$39 Plan"|"0.131383"|"0"|"0"|"0"|"0"|"0"
"2015-10-06"|"592"|"620"|"0"|"Apple Inc"|"Apple iPhone 6 (A1586)"|"PREPAY  STD - TRIAL - #16"|"0"|"0"|"0"|"0"|"0"|"0"
"2015-10-06"|"007"|"290"|"0"|"Apple Inc"|"Apple iPhone 6 (A1586)"|"PREPAY PLUS - $0 -"|"0"|"0"|"0"|"0"|"0"|"0"
"2015-10-06"|"592"|"050"|"48836832"|"Apple Inc"|"Apple iPhone 5S (A1530)"|"Talk and Text Connect Flexi Plan"|"46.5744"|"1"|"1"|"0"|"0"|"0"
"2015-10-06"|"409"|"720"|"113755347"|"Samsung Korea"|"Samsung SM-G360G"|"$29 CARRYOVER PLAN"|"108.486"|"1"|"1"|"1"|"0"|"0"
"2015-10-06"|"742"|"620"|"19840943"|"Apple Inc"|"Apple iPhone S (A1530)"|"PREPAY STD - $0 - #2"|"18.9218"|"1"|"1"|"0"|"0"|"0"
"2015-10-06"|"387"|"180"|"0"|"HUAWEI Technologies Co Ltd"|"HUAWEI HUAWEI G526-L11"|"PREPAY STD - $1 - #4"|"0"|"0"|"0"|"0"|"0"|"0"
"2015-10-06"|"731"|"570"|"2258243"|"Samsung Korea"|"Samsung SM-N910U"|"Business Freedom"|"2.15363"|"1"|"0"|"0"|"0"|"0"
"2015-10-06"|"556"|"910"|"13332272"|"Samsung Korea"|"Samsung GT-I9505"|"$49 Plan"|"12.7146"|"1"|"1"|"0"|"0"|"0"

我对列名9-13感兴趣

$ head -n1 test6 | tr '|' '\n' | cat -n
     1  "Rec_Open_Date"
     2  "MSISDN"
     3  "IMEI"
     4  "Data_Volume_Bytes"
     5  "Device_Manufacturer"
     6  "Device_Model"
     7  "Product_Description"
     8  "Data_Volume_MB"
     9  ">20MB/30"
    10  ">200MB/30"
    11  ">2048MB/30"
    12  ">5120MB/30"
    13  ">10240MB/30"

我想用awk取出名字,但我想控制订单。这有效,但排序并不像我一样:

$ awk -F'|' -v q='"' 'NR==1{for (i=9;i<=NF;i++) col[i]=$i}; END { for (i in col) print col[i]}' test6;
">200MB/30"
">2048MB/30"
">5120MB/30"
">10240MB/30"
">20MB/30"
$

我希望排序从上到下,和/或颠倒,即9-13或13-9。为了清楚起见,我在输出中留下了数字(9-13),我不希望它们出现在输出中。我希望我的输出就像我上面的awk输出。 可以在awk中完成,还是必须使用其他内容?

9  ">20MB/30"
10  ">200MB/30"
11  ">2048MB/30"
12  ">5120MB/30"
13  ">10240MB/30"

这就是我想要的和/或反过来的,我希望它可以从awk完成。

">20MB/30"
">200MB/30"
">2048MB/30"
">5120MB/30"
">10240MB/30"

EDIT1我的答案来自以下答案

关键是for (i = 9; i <= NF; i++) print col[i]对此for (i in col) print col[i]

$ awk -F'|' -v q='"' 'NR==1{for (i=9;i<=NF;i++) col[i]=$i}; END { for (i = NF; i >= 9; i--) print col[i]}' test6
">10240MB/30"
">5120MB/30"
">2048MB/30"
">200MB/30"
">20MB/30"

$ awk -F'|' -v q='"' 'NR==1{for (i=9;i<=NF;i++) col[i]=$i}; END { for (i = 9; i <= NF; i++) print col[i]}' test6
">20MB/30"
">200MB/30"
">2048MB/30"
">5120MB/30"
">10240MB/30"

3 个答案:

答案 0 :(得分:1)

您的代码无法可靠地运行,因为GNU awk数组是关联的,并且扫描数组的顺序取决于实现。 awk -F'|' -v q='"' 'NR==1{for (i=9;i<=NF;i++) col[i]=$i}; END { for (i = 9; i <= 13; i++) print col[i]}' test6 可以在一定程度上控制扫描顺序。但是,由于您知道索引,因此您可以直接使用它们,如迈克尔所建议的那样:

$(document).ready(function() {
   var projects = [{
       value: "jquery",
       label: "jQuery",
       desc: "the write less, do more, JavaScript library"
   }, {
       value: "jquery-ui",
       label: "jQuery UI",
       desc: "the official user interface library for jQuery"
   }, {
       value: "sizzlejs",
       label: "Sizzle JS",
       desc: "a pure-JavaScript CSS selector engine"
   }];

   $("#search").autocomplete({
           minLength: 0,
           source: projects,
           focus: function(event, ui) {
               console.log(ui.item.label);
               $("#project").val(ui.item.label);
               return false;
           },
           select: function(event, ui) {
            console.log(ui);
               $("#search").val(ui.item.label);
               $("#project-id").val(ui.item.value);
               $("#project-description").html(ui.item.desc);
               return false;
           }
       })
       .autocomplete( "instance" )._renderItem = function( ul, item ) {
      return $( "<li>" )
        .append( "<a>" + item.label + "<br>" + item.desc + "</a>" )
        .appendTo( ul );
    };
   });

答案 1 :(得分:0)

请查看以下内容是否符合您的要求:

awk -F'|' -v q='"' '{if(FNR==1){for (i=0;i<=NF;i++) print $i;}}' test6

答案 2 :(得分:0)

短流水线可以做到这一点:

head -1 input | cut -f9-13 -d'|' | tr '|' '\n'

提供

">20MB/30"
">200MB/30"
">2048MB/30"
">5120MB/30"
">10240MB/30"

但这假设引号中没有管道符号

和使用awk的版本具有相同的假设:

awk -vFPAT='[^"|]+' 'NR==1{for(i=9;i<=13;i++){print $i}}' input

这给出了

>20MB/30
>200MB/30
>2048MB/30
>5120MB/30
>10240MB/30