Shell脚本 - 按列排序文本文件&将不存在

时间:2015-10-16 03:11:24

标签: shell sorting unix

我想根据文件中的第4列(例如,16或18或5或7)对文本文件进行排序,但正如您所看到的,有些行没有该字段(例如:)在排序之后,它们应该低于它们最初的线以下。

示例数据

<?php
   while(($count<$rpp) && ($i<$tcount)) {
   mysqli_data_seek($result,$i);
   $r = mysqli_fetch_array($result); ?>
<div class="col-md-6" style="border:0px solid #bbb;border-radius:30px;padding:0px 20px 10px 20px;">
   <div class="thumbnail">
      <?php     $s= " 
         SELECT * FROM tbbarang where idbarang='$r[idbarang]' ";


                    $resu = mysqli_query($koneksi,$s);
                while  ($d= mysqli_fetch_array($resu)){

                    ?>        
      <img id="myImage" onclick="changeImage()"   src="<?php echo $d['gmbr1'] ?>">
      <script>
         function changeImage() {
             var image = document.getElementById('myImage');
             if (image.src.match("<?php echo $d['gmbr4'] ?>"))
             {
                 image.src = "<?php echo $d['gmbr1'] ?>"; 


             } 
            else if (image.src.match("<?php echo $d['gmbr1'] ?>"))
                {
                  image.src = "<?php echo $d['gmbr2'] ?>"; 

                }
            else if (image.src.match("<?php echo $d['gmbr2'] ?>"))
                {
                  image.src = "<?php echo $d['gmbr3'] ?>"; 

                }

            else if (image.src.match("<?php echo $d['gmbr3'] ?>"))
            {
                  image.src = "<?php echo $d['gmbr4'] ?>";


                }   
         }
         <?php } ?>
      </script>                                                    
      <div class="caption">
         <h3><b><?php echo  $r['namabarang']; ?></b></h3>
         <tr style="font-size:14px;"><strong>Harga : <?php echo  $r['harga']; ?> </strong>&nbsp;&nbsp;  Satuan : <?php echo  $r['satuan']; ?>
            &nbsp;&nbsp;                                                          
         </tr>
         <tr style="font-size:14px;">Ukuran : <?php echo  $r['ukuran']; ?> &nbsp;&nbsp; Berat : <?php echo  $r['berat']; ?></tr>
         <p>
            <a  data-toggle="modal" href="#responsive<?php echo  $i; ?>" class="btn btn-primary">Beli </a>
            <!-- Modal -->
            <!-- mulai modal -->
         </p>
         <div id="responsive<?php echo  $i; ?>" class="modal fade" tabindex="-1" aria-hidden="true" align="center" >
            <form method="POST" action="inserttransaksi.php">
               <div class="modal-dialog" align="center">
                  <div class="modal-content" align="center">
                     <div class="modal-header" align="center">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                        <h4 class="modal-title"><strong>Detail Pembelian</strong></h4>
                     </div>
                     <div class="modal-body" align="center">
                        <div class="scroller" style="height:200px" data-always-visible="1" data-rail-visible1="1" align="center">
                           <div class="row" align="center">
                              <div class="table col-md-6" align="center">
                                 <!--h4 align="center"><strong>Keterangan Cancel Barang <?php echo $r['namabarang'];?></strong></h4-->
                                 <table width="612" height="156">
                                    <input type="text" value="<?php echo $r['idbarang'];?>" name="iddetail2" class="input-xsmall" ></td> 
                                    <?php $sq= "SELECT * FROM tbbarang where idbarang='$r[idbarang]'";
                                       $resul = mysqli_query($koneksi,$sq);
                                       $s= mysqli_fetch_array($resul);

                                       ?>
                                    <tr>
                                       <td width="91" height="36">Nama Barang</td>
                                       <td width="6">: &nbsp;</td>
                                       <td width="168"><strong><?php echo $r['namabarang'];?></strong></td>
                                    </tr>
                                    <tr>
                                       <td height="23">Jumlah</td>
                                       <td>:&nbsp;</td>
                                       <input  value="<?php echo $s['harga'];?>" name="txt" id="txt" class="form-control" type="text"></td> 
                                       <td><input type="text" value="" name="txtjumlah" id="txtjumlah" class="input-xsmall" onKeyUp="myFunction(this.value)"></td>
                                    </tr>
                                    <tr>
                                    <tr>
                                       <td height="41">Harga</td>
                                       <td>:&nbsp;</td>
                                       <td><?php echo $s['harga'];?></td>
                                       <td width="18">&nbsp;</td>
                                       <td width="123" height="41">Total Real</td>
                                       <td width="6">:&nbsp;</td>
                                       <td width="168"><input type="text" value="" name="txtreal" id="txtreal" class="form-control" readonly></td>
                                    </tr>
                                    <tr>&nbsp;</tr>
                                    <tr>
                                       <td height="31">Penawaran</td>
                                       <td>:&nbsp;</td>
                                       <td><input type="text" value="" name="txtpe" id="txtpe" class="form-control" onKeyUp="myFunctio(this.value)"></td>
                                       <td>&nbsp;</td>
                                       <td height="32">Total Penawaran</td>
                                       <td>:</td>
                                       <td><input type="text" value="" name="txtp" id="txtp" class="form-control" readonly></td>
                                    </tr>
                                 </table>
                              </div>
                           </div>
                        </div>
                        <div class="modal-footer">
                           <button type="submit" name="save1" class="btn default">Save</button>
                           <button type="button" data-dismiss="modal" class="btn default">Close</button>
                        </div>
                     </div>
                  </div>
               </div>
            </form>
         </div>
      </div>
   </div>
</div>
<?php
   $i++; 
   $count++;
   }
   ?>

示例输出(所需)

INSERT,SLT_TEST_5,1218738496,16,DEBUG3,,DEBUG_LEVEL1
<v s=""MONTHLY_PEAK_DWNLOAD""/>
</a><a n=""thresholdScheme"">
<o t=""PM_UsageMonitorConfigThreshold""> 
INSERT,SLT_TEST_1,1218738496,18,DEBUG3,,DEBUG_LEVEL4
<v s=""ORANGE""/>
INSERT,SLT_TEST_3,5555738111,5,DEBUG3,,DEBUG_LEVEL1
INSERT,SLT_TEST_1,9998738342,7,DEBUG3,,DEBUG_LEVEL2

2 个答案:

答案 0 :(得分:2)

假设在排序字段之前没有嵌入逗号,您可以使用awk来帮助排序并在之后删除任何无关的信息:

awk -F',' -vi=0 '
/^INSERT/ { i=$4 }
{ printf("%08d%08d\t%s\n", i, NR, $0) }
' < data | sort | sed -e 's/^[^\t]*\t//' > newdata

只是输入一个输入文件&#34;数据&#34;到awk,它将以XXXXXXXX.NNNNNNNN形式在数据本身之前插入第4个字段的格式化副本,其中XXXXXXXX表示第4个字段中值的8位数表示找到最后一个INSERT ...行,NNNNNNNN表示格式化为8位数值的记录号(行号)。原始数据和格式化数据由制表符分隔。文件不以INSERT开头的特殊情况(例如空行,解释文件内容的注释等)被视为最后INSERT行有第四个值为0的字段。这里是结果输出的剪切样本,其中插入了一些额外的行进行测试:

0000000000000001    # This file contains data.
0000001600000002    INSERT,SLT_TEST_5,1218738496,16,DEBUG3,,DEBUG_LEVEL1
0000001600000003    <v s=""MONTHLY_PEAK_DWNLOAD""/>
0000001600000004    </a><a n=""thresholdScheme"">
0000001600000005    <o t=""PM_UsageMonitorConfigThreshold""> 
0000001800000006    INSERT,SLT_TEST_1,1218738496,18,DEBUG3,,DEBUG_LEVEL4
0000001800000007    <v s=""ORANGE""/>
0000000500000008    INSERT,SLT_TEST_3,5555738111,5,DEBUG3,,DEBUG_LEVEL1
0000000700000009    INSERT,SLT_TEST_1,9998738342,7,DEBUG3,,DEBUG_LEVEL2
0000000700000010    I'm a little teapot.

生成的行传递给sort,它将对它接收的行进行排序。然后,sed将删除sort脚本最初创建的信息awk实用程序输出的行,从而导致基于第四个字段的排序,而不会改变行和的行的顺序#39; t以INSERT开头,输出到文件&#34; newdata&#34;:

# This file contains data.
INSERT,SLT_TEST_3,5555738111,5,DEBUG3,,DEBUG_LEVEL1
INSERT,SLT_TEST_1,9998738342,7,DEBUG3,,DEBUG_LEVEL2
I'm a little teapot.
INSERT,SLT_TEST_5,1218738496,16,DEBUG3,,DEBUG_LEVEL1
<v s=""MONTHLY_PEAK_DWNLOAD""/>
</a><a n=""thresholdScheme"">
<o t=""PM_UsageMonitorConfigThreshold""> 
INSERT,SLT_TEST_1,1218738496,18,DEBUG3,,DEBUG_LEVEL4
<v s=""ORANGE""/>

答案 1 :(得分:0)

我在这一点上有点油腻,而且我很难理解我的哪一部分没有完全正确但这样的事情似乎对你的测试数据起作用(除了添加一个& #34;空白&#34;记录/进入顶部。

awk -v RS='\n?INSERT' -F , '{
    gsub(/\n$/,"");
    m[sprintf("%04d",$4)]=substr(RS,3)$0
}
END {
    l = asorti(m, mm);
    for (i = 1; i <= l; i++) {
        print m[mm[i]]
    }
}'

虽然这个版本在第一条记录上双重RS(我现在也看不清楚原因)。

awk -v RS='\nINSERT' -F , '{
    gsub(/\n$/,"");
    m[sprintf("%04d",$4)]=substr(RS,2)$0
}
END {
    l = asorti(m, mm);
    for (i = 1; i <= l; i++) {
        print m[mm[i]]
    }
}'

sprintfasorti正确排序字段(字符串排序)。这需要足够大,以便将每个字段填充到相同的宽度。