【问题标题】:Print string of words formatting similar to text output from `ls` command打印格式类似于 `ls` 命令的文本输出的字串
【发布时间】:2018-07-25 19:46:35
【问题描述】:

是否有任何命令可以打印任何字符串,例如

“Lorem ipsum dolor sit amet consectetur adipiscing elit Aenean arcu dui commodo nec tortor ut posuere malesuada magna"

动态格式化为类似于ls 命令的文本输出的列,例如

~$ls
Applications   Dropbox        Pictures       test2          playground
Desktop        Library        Public         test3          programs
Documents      Movies         VirtualBoxVMs  macports       projects
Downloads      Music          test1          test4

列数应与终端大小相对应。

【问题讨论】:

    标签: bash shell sh


    【解决方案1】:

    使用tr 将空格替换为换行符。然后使用column -cXXX 格式化终端XXX 字符宽的输出,并使用tput cols 找出终端的实际宽度:

    echo "..." | tr ' ' '\n' | column -c$(tput cols)
    

    【讨论】:

      【解决方案2】:

      column -t 需要用换行符分隔行,所以我们可以在指定的单词之间插入换行符来创建一行。
      所以:

      1. 我使用sed 's/\(\([^ ]* \)\{5\}\)/\1\n/g' 每 5 个单词插入一个换行符
      2. 我用column -t创建了一个表

      $ echo "Lorem ipsum dolor sit amet consectetur adipiscing elit Aenean arcu dui commodo nec tortor ut posuere malesuada magna" | sed 's/\(\([^ ]* \)\{5\}\)/\1\n/g' | column -t -o$'\t'
      Lorem       ipsum       dolor   sit     amet
      consectetur adipiscing  elit    Aenean  arcu
      dui         commodo     nec     tortor  ut
      posuere     malesuada   magna   
      

      然而这可能是一个更好的解决方案,它考虑了终端宽度。

      【讨论】:

      • “5 words”对我来说听起来太“临时”了。我希望格式是与终端大小相对应的动态格式,例如ls 命令的输出。
      【解决方案3】:

      您可以使用 printf 和列选项轻松完成。

      这样的简单方法:

      printf "%-8s\n" a.txt b.txt c.txt d.txt e.txt f.txt g.txt h.txt i.txt j.txt k.txt l.txt m.txt n.txt o.txt p.txt q.txt r.txt s.txt t.txt u.txt v.txt w.txt x.txt y.txt z.txt | column
      

      或者在循环中:

      #!/bin/bash
      
      for aFileNames in a.txt b.txt c.txt d.txt e.txt f.txt g.txt h.txt i.txt j.txt k.txt l.txt m.txt n.txt o.txt p.txt q.txt r.txt s.txt t.txt u.txt v.txt w.txt x.txt y.txt z.txt
      do 
        printf "%-8s\n" $aFileNames
      done | column
      

      输出将如下所示:

      a.txt        j.txt        s.txt
      b.txt        k.txt        t.txt
      c.txt        l.txt        u.txt
      d.txt        m.txt        v.txt
      e.txt        n.txt        w.txt
      f.txt        o.txt        x.txt
      g.txt        p.txt        y.txt
      h.txt        q.txt        z.txt
      i.txt        r.txt
      

      如果您愿意,可以更改 "%-8s" 值以更改显示的列数。 有效值为:"-4", "-8", "-16", "-32", etc...

      如果您在终端和此处输入column -help,您可以找到有关它的更多信息:

      【讨论】:

        【解决方案4】:

        您可以使用fmt 来实现:,示例:

        echo "Lorem ipsum .. .. .. . . magna" |fmt 
        Lorem ipsum dolor sit amet consectetur adipiscing elit
        Aenean arcu dui commodo nec tortor ut posuere malesuada
        magna
        

        -w, --width=WIDTH 最大行宽(默认为 75 列)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多