AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 705890
Accepted
Dingo
Dingo
Asked: 2022-06-13 02:34:00 +0800 CST2022-06-13 02:34:00 +0800 CST 2022-06-13 02:34:00 +0800 CST

按照给定规则缩进文本行

  • 772

我想知道如何按照自定义规则递归地缩进越来越多的诗行。例如

假设我们有:

OF Mans First Disobedience, and the Fruit
Of that Forbidden Tree, whose mortal tast
Brought Death into the World, and all our woe,
With loss of Eden, till one greater Man
Restore us, and regain the blissful Seat,
Sing Heav'nly Muse, that on the secret top
Of Oreb, or of Sinai, didst inspire
That Shepherd, who first taught the chosen Seed,
In the Beginning how the Heav'ns and Earth
Rose out of Chaos: Or if Sion Hill
Delight thee more, and Siloa's Brook that flow'd
Fast by the Oracle of God; I thence
Invoke thy aid to my adventrous Song,
That with no middle flight intends to soar
Above th' Aonian Mount, while it pursues
Things unattempted yet in Prose or Rhime.
And chiefly Thou O Spirit, that dost prefer
Before all Temples th' upright heart and pure,
Instruct me, for Thou know'st; Thou from the first
Wast present, and with mighty wings outspread.

我们想递归缩进它,在第一行之后的任何三行添加3个空格,以这种方式

OF Mans First Disobedience, and the Fruit
   Of that Forbidden Tree, whose mortal tast
   Brought Death into the World, and all our woe,
   With loss of Eden, till one greater Man
Restore us, and regain the blissful Seat,
   Sing Heav'nly Muse, that on the secret top
   Of Oreb, or of Sinai, didst inspire
   That Shepherd, who first taught the chosen Seed,
In the Beginning how the Heav'ns and Earth
   Rose out of Chaos: Or if Sion Hill 
   Delight thee more, and Siloa's Brook that flow'd
   Fast by the Oracle of God; I thence
Invoke thy aid to my adventrous Song,
   That with no middle flight intends to soar
   Above th' Aonian Mount, while it pursues
   Things unattempted yet in Prose or Rhime.
And chiefly Thou O Spirit, that dost prefer
   Before all Temples th' upright heart and pure,
   Instruct me, for Thou know'st; Thou from the first
   Wast present, and with mighty wings outspread

实现这一目标的最简单方法是什么?

text-processing text-formatting
  • 2 2 个回答
  • 175 Views

2 个回答

  • Voted
  1. Best Answer
    terdon
    2022-06-13T03:20:22+08:002022-06-13T03:20:22+08:00

    如果您只想缩进第一行,然后每第四行缩进一次,您可以使用awk:

    
    $ awk 'NR % 4 != 1{$0="    "$0};1' file 
    OF Mans First Disobedience, and the Fruit
        Of that Forbidden Tree, whose mortal tast
        Brought Death into the World, and all our woe,
        With loss of Eden, till one greater Man
    Restore us, and regain the blissful Seat,
        Sing Heav'nly Muse, that on the secret top
        Of Oreb, or of Sinai, didst inspire
        That Shepherd, who first taught the chosen Seed,
    In the Beginning how the Heav'ns and Earth
        Rose out of Chaos: Or if Sion Hill
        Delight thee more, and Siloa's Brook that flow'd
        Fast by the Oracle of God; I thence
    Invoke thy aid to my adventrous Song,
        That with no middle flight intends to soar
        Above th' Aonian Mount, while it pursues
        Things unattempted yet in Prose or Rhime.
    And chiefly Thou O Spirit, that dost prefer
        Before all Temples th' upright heart and pure,
        Instruct me, for Thou know'st; Thou from the first
        Wast present, and with mighty wings outspread.
    

    或者perl:

    $ perl -pe 's/^/    / if $. % 4 != 1' file
    OF Mans First Disobedience, and the Fruit
        Of that Forbidden Tree, whose mortal tast
        Brought Death into the World, and all our woe,
        With loss of Eden, till one greater Man
    Restore us, and regain the blissful Seat,
        Sing Heav'nly Muse, that on the secret top
        Of Oreb, or of Sinai, didst inspire
        That Shepherd, who first taught the chosen Seed,
    In the Beginning how the Heav'ns and Earth
        Rose out of Chaos: Or if Sion Hill
        Delight thee more, and Siloa's Brook that flow'd
        Fast by the Oracle of God; I thence
    Invoke thy aid to my adventrous Song,
        That with no middle flight intends to soar
        Above th' Aonian Mount, while it pursues
        Things unattempted yet in Prose or Rhime.
    And chiefly Thou O Spirit, that dost prefer
        Before all Temples th' upright heart and pure,
        Instruct me, for Thou know'st; Thou from the first
        Wast present, and with mighty wings outspread.
    

    在这两种情况下,如果当前行号模块4 不等于 1,我们将在行首添加 4 个空格,这意味着我们将对除第 1 行、第 4 行等之外的所有行执行此操作。

    在 awk 中,NR是行号,$0是行的内容,所以 NR % 4 != 1{$0=" "$0};意思是“当当前行号模 4 不等于 1 时,在行首加 4 个空格”。final1;只是“打印”的简写。

    在 Perl 中,$.是当前行号,s/old/new/是替换运算符,它将替换第一次出现的oldwith new。所以意思是“如果当前行号模 4 不等于 1,则用四个空格s/^/ / if $. % 4 != 1替换行 () 的开头”。意思是“在应用由”提供的脚本后打印输入文件的每一^行。-p-e

    这是完全相同的 perl 命令,但版本更详细且更易于理解:

    perl -e '
    open(my $fileHandle, "<", $ARGV[0]);
    my $lineCount=0;
    
    while(my $line = <$fileHandle>){
       $lineCount += 1;
       if ( $lineCount % 4 != 1 ){
           ## or $line = "    " . $line
           $line =~ s/^/    /
       }
       print "$line";
    }' file
    

    或者,几乎相同:

    perl -e '
    open(my $fileHandle, "<", $ARGV[0]);
    my $lineCount=0;
    
    while(my $line = <$fileHandle>){
       $lineCount += 1;
       unless ( $lineCount % 4 == 1 ){
           $line = "    " . $line
       }
       print "$line";
    }' file
    
    • 7
  2. jubilatious1
    2022-06-15T18:23:41+08:002022-06-15T18:23:41+08:00

    使用Raku(以前称为 Perl_6)

    自动打印(下面的前两个):

    raku -pe 's/^/    / if $++ % 4;'  
    

    或者

    raku -pe 'state $i; s/^/    / if $i++ % 4;'  
    

    或(非自动打印,以下两个)

    raku -ne '$++ % 4 ?? put "    $_" !! put "$_";' 
    

    或(非自动打印,无内部引号)

    raku -ne 'state $i; $i++ % 4 ?? qq[    $_].put !! $_.put;' 
    

    上面的 Raku 代码,前两个示例是 @terdon 的 Perl(5) 单行代码(-pe标志)的直接翻译。Raku$_用作主题变量——与 Perl 相同。注意 Raku 摒弃了许多特殊变量,取而代之的是state只初始化一次的变量。常用的state变量包括$递增/递减形式,例如$++(匿名标量,此处用于计算行号)。

    最后两个示例是非自动打印(-ne标志),并使用 Raku 的“ ?? True !! False ”三元运算符。这些形式在概念上类似于@Ed_Morton 的awk代码(评论)。尝试在 Raku 三元的每一半中保留并行文本,但实际上,在输出$_主题变量时您需要做的就是写入(如果未明确说明.put,Raku 默认为主题变量)。$_此外,put在 Raku 中\n为您添加了换行符。

    最后,Raku 中的很多引用问题都是使用“Q 语言”解决的,下面会引用它(上面的第四个单行代码中的示例使用qq[…])。

    示例输出(所有 4 个单行,以上):

    OF Mans First Disobedience, and the Fruit
        Of that Forbidden Tree, whose mortal tast
        Brought Death into the World, and all our woe,
        With loss of Eden, till one greater Man
    Restore us, and regain the blissful Seat,
        Sing Heav'nly Muse, that on the secret top
        Of Oreb, or of Sinai, didst inspire
        That Shepherd, who first taught the chosen Seed,
    In the Beginning how the Heav'ns and Earth
        Rose out of Chaos: Or if Sion Hill
        Delight thee more, and Siloa's Brook that flow'd
        Fast by the Oracle of God; I thence
    Invoke thy aid to my adventrous Song,
        That with no middle flight intends to soar
        Above th' Aonian Mount, while it pursues
        Things unattempted yet in Prose or Rhime.
    And chiefly Thou O Spirit, that dost prefer
        Before all Temples th' upright heart and pure,
        Instruct me, for Thou know'st; Thou from the first
        Wast present, and with mighty wings outspread.
    

    https://docs.raku.org/syntax/state
    https://docs.raku.org/language/quoting
    https://docs.raku.org/language/operators#index-entry-operator_ternary

    • 1

相关问题

  • grep 从 $START 到 $END 的一组行并且在 $MIDDLE 中包含匹配项

  • 重新排列字母并比较两个单词

  • 在awk中的两行之间减去相同的列

  • 多行文件洗牌

  • 如何更改字符大小写(从小到大,反之亦然)?同时[重复]

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve