简体   繁体   English

通过换行以awk反转html的顺序

[英]Reverse order of html with awk via line swapping

Basically every week I have to reverse the following snippet 基本上每周我都必须反转以下代码段

<!-- Homepage Slider Begin -->
<div class="container-fluid">
  <div class="single-item-home hidden-xs">
    <div class="slide slide--has-caption">
      <a href="/1">
        <img src="/sliders/1_example.jpg">
      </a>
    </div>

    <div class="slide slide--has-caption">
      <a href="/2">
        <img src="/sliders/2_example.jpg">
      </a>
    </div>

    <div class="slide slide--has-caption">
      <a href="/3">
        <img src="/sliders/3_example.jpg">
      </a>
    </div>

    <div class="slide slide--has-caption">
      <a href="/4">
        <img src="/sliders/4_example.jpg">
      </a>
    </div>
  </div>
</div>
<!-- Homepage Slider End -->

Basically I'm wanting to make awk script and have a cron job to essentially take lines 4-8 to swap with lines 22-26 and lines 10-14 swap with lines 16-20 however I can only seem to find a way to swap one line and not line blocks. 基本上我想制作awk脚本并进行cron工作,以便基本上将4-8行与22-26行交换,将10-14行与16-20行交换,但是我似乎只能找到一种交换方法一线而不是线块。

Is this even possible with awk or just silly? awk甚至是愚蠢的方法是否有可能?

You may use awk . 您可以使用awk Below script 下面的脚本

 awk 'NR==FNR{line[i++]=$0} 
     END{
         for(j=0;j<i;j++){
         if(j>=3 && j<=7){
             print line[j+18];
             continue;
         }
         else if(j>=21 && j<=25){
             print line[j-18];
             continue;
         }
         else if(j>=9 && j<=13){
             print line[j+6];
             continue;
         }
         else if(j>=15 && j<=19){
             print line[j-6];
             continue;
         }
         print line[j];
         }
     }' file

will do what you want. 会做你想要的。

Sample Output 样本输出

<!-- Homepage Slider Begin -->
<div class="container-fluid">
  <div class="single-item-home hidden-xs">
    <div class="slide slide--has-caption">
      <a href="/4">
        <img src="/sliders/4_example.jpg">
      </a>
    </div>

    <div class="slide slide--has-caption">
      <a href="/3">
        <img src="/sliders/3_example.jpg">
      </a>
    </div>

    <div class="slide slide--has-caption">
      <a href="/2">
        <img src="/sliders/2_example.jpg">
      </a>
    </div>

    <div class="slide slide--has-caption">
      <a href="/1">
        <img src="/sliders/1_example.jpg">
      </a>
    </div>
  </div>
</div>
<!-- Homepage Slider End -->

Note: I leave the array-bounds check up to you. 注意:我让数组边界检查由您决定。 If the content of the file is static, you may not need this 如果文件内容是静态的,则可能不需要

perl -e '@f=<>; print @f[0..2,21..25,8,15..19,14,9..13,20,3..7,26..$#f]' ip.html
  • -e option to pass Perl code from command line itself -e选项从命令行本身传递Perl代码
  • @f=<> Reads the contents of file (passed as command line argument) into an array @f=<>将文件的内容(作为命令行参数传递)读入数组
  • and then print as per order required (index starts from 0, $#f gives last index of array @f ) 然后按要求打印(索引从0开始,$#f给出数组@f最后一个索引)

This doesn't care how many lines are in each block or where they start/end in the file and it doesn't require you to store the whole file in memory (though most of the file is the "slides" which DO need to be stored so that's probably a non-issue): 这并不关心每个块中有多少行,或者它们在文件中的开始/结束位置,也不需要您将整个文件存储在内存中(尽管大多数文件是“幻灯片”,但确实需要被存储,因此可能不是问题):

$ cat tst.awk
/<div class="slide/ { inSlide=1; slide="" }
inSlide {
    slide = slide $0 ORS
    if ( /<\/div>/ ) {
        slides[++numSlides] = slide
        inSlide = 0
    }
    next
}
/<\/div>/ {
    for (slideNr=numSlides; slideNr>=1; slideNr--) {
        printf "%s", slides[slideNr]
    }
    numSlides = 0
}
NF

.

$ awk -f tst.awk file
<!-- Homepage Slider Begin -->
<div class="container-fluid">
  <div class="single-item-home hidden-xs">
    <div class="slide slide--has-caption">
      <a href="/4">
        <img src="/sliders/4_example.jpg">
      </a>
    </div>
    <div class="slide slide--has-caption">
      <a href="/3">
        <img src="/sliders/3_example.jpg">
      </a>
    </div>
    <div class="slide slide--has-caption">
      <a href="/2">
        <img src="/sliders/2_example.jpg">
      </a>
    </div>
    <div class="slide slide--has-caption">
      <a href="/1">
        <img src="/sliders/1_example.jpg">
      </a>
    </div>
  </div>
</div>
<!-- Homepage Slider End -->

This is a solution, where you define an order where to print in the BEGIN section and in that order it will print: 这是一个解决方案,您可以在BEGIN部分中定义一个打印位置的顺序,并按照该顺序打印:

$ cat > preordered.awk
BEGIN {
    split("1,2,3,22,23,24,25,26,9,16,17,18,19,20,15,10,11,12,13,14,21,4,5,6,7,8",a,",")
} 
{
    b[(NR in a?a[NR]:NR)]=$0
} 
END {
    PROCINFO["sorted_in"]="@ind_num_asc"
    for(i in b)
        print b[i]
}

Give it a go: 搏一搏:

$ awk -f preordered.awk' file
<!-- Homepage Slider Begin -->
<div class="container-fluid">
  <div class="single-item-home hidden-xs">
    <div class="slide slide--has-caption">
      <a href="/4">
        <img src="/sliders/4_example.jpg">
      </a>
    </div>
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM