Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 颠倒线对的线顺序_Regex_Sorting_Text_Awk_Grep - Fatal编程技术网

Regex 颠倒线对的线顺序

Regex 颠倒线对的线顺序,regex,sorting,text,awk,grep,Regex,Sorting,Text,Awk,Grep,我需要颠倒文本行对的顺序,例如: 一线1 第二行1 一线2 第二行2 一线3 第二行3 到 第二行1 一线1 第二行2 一线2 第二行3 一线3 我正在寻找一种自动化的方法(我有650行要排序),有什么神奇的解决方案吗? 谢谢 下面的两种解决方案都要求线实际上是成对的。否则,最后一行将无法打印 SED溶液 禁用自动打印 复制行以保留空间 删除模式空间 将下一行读入模式空间 附加保持空间 打印图案空间 AWK解决方案 不要将行拆分为空白字段 如果行为奇数,则存储该行 如果行为偶数,则打印当

我需要颠倒文本行对的顺序,例如:

  • 一线1
  • 第二行1
  • 一线2
  • 第二行2
  • 一线3
  • 第二行3

  • 第二行1
  • 一线1
  • 第二行2
  • 一线2
  • 第二行3
  • 一线3
我正在寻找一种自动化的方法(我有650行要排序),有什么神奇的解决方案吗? 谢谢

下面的两种解决方案都要求线实际上是成对的。否则,最后一行将无法打印

SED溶液
  • 禁用自动打印
  • 复制行以保留空间
  • 删除模式空间
  • 将下一行读入模式空间
  • 附加保持空间
  • 打印图案空间
  • AWK解决方案
  • 不要将行拆分为空白字段
  • 如果行为奇数,则存储该行
  • 如果行为偶数,则打印当前行和存储行

  • 更明显的shell版本:

    while read -r firstline ; read -r secondline
    do
        printf '%s\n%s\n' "$secondline" "$firstline"
    done
    

    请参阅。如果文件中有此项,只需:cat file.txt | sed-n'h;NPG别忘了相信这个答案,谢谢!工作起来很有魅力,将其添加为bbedit文本过滤器:创建了一个包含#的.sh文件/垃圾箱/垃圾箱类别“$1”| sed-n'h;s/*/;NGp'将其放置在library/Application support/BBEdit/Text Filter/Myscript.sh中。现在可以在“Apply Text Filter”菜单中直接访问脚本!您不需要删除模式空间,因为
    n
    会覆盖它。@DennisWilliamson-Hmmm。以前我在没有替换的情况下得到了额外的行空间,但我无法重现这种行为。替换没有伤害,但是
    sed-n'h;NGp'/tmp/foo
    也很好用。谢谢你的提醒!
    awk -F'\n' 'NR%2==1 {line=$0}; NR%2==0 {print $1 "\n" line}' /tmp/foo
    
    while read -r firstline ; read -r secondline
    do
        printf '%s\n%s\n' "$secondline" "$firstline"
    done