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
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1398716
Accepted
CIsForCookies
CIsForCookies
Asked: 2022-03-23 06:30:49 +0800 CST2022-03-23 06:30:49 +0800 CST 2022-03-23 06:30:49 +0800 CST

如何将字符串分成 N 80 个字符行?

  • 772

我想在脚本的开头显示一条欢迎消息:

echo "Running $0 $@"

但$@可能会很长。如何将这条线分成多条最多 80 个字符的行?

command-line
  • 3 3 个回答
  • 924 Views

3 个回答

  • Voted
  1. Best Answer
    Artur Meinild
    2022-03-23T06:42:55+08:002022-03-23T06:42:55+08:00

    使用fold.

    echo "Running $0 $@" | fold -sw 80
    

    -s包裹在空格上。 -w 80将宽度设置为 80 列。

    • 10
  2. pLumo
    2022-03-23T06:46:31+08:002022-03-23T06:46:31+08:00

    当然fold是最好的选择,但您也可以使用grepor实现此目的sed:

    echo "Running $0 $@" | grep -o '.\{80\}[^ ]* '
    #or
    echo "Running $0 $@" | sed -E 's/(.{80}[^ ]*) /\1\n/g'
    

    请注意,这将在 80 个字符 + 1 个单词处中断,因此它可能不适合您。

    • 3
  3. jiwopene
    2022-03-24T09:57:40+08:002022-03-24T09:57:40+08:00

    尽管其他人提到了在达到 80 个字符限制的单词之前换行的工具,但还有另一种工具试图在句子开头之后的结尾之前不换行,并执行其他操作以提高可读性。它的用法类似:

    echo "…" | fmt -w 80
    

    该-w选项指定文本的最大宽度,但文本将仅使用行的 93%(这可以通过使用 覆盖-g)。

    这是fold格式化文本的方式:

    Although others mentioned tools that break the lines just before the word that 
    hits the 80-character limit, there is also another tool that tries to not break 
    the lines just before end of after start of a sentence and does other things 
    for better readability. Its usage is similar:
    
        echo "…" | fmt -w 80
    
    The `-w` option specifies the maximum width of the text, but the text will use 
    only 93 percent of the line (this can be overridden by using `-g`).
    
    The long sentence that hits the 80 character column count is this one and here.
    

    这是来自fmt:

    Although others mentioned tools that break the lines just before the word
    that hits the 80-character limit, there is also another tool that tries
    to not break the lines just before end of after start of a sentence and
    does other things for better readability. Its usage is similar:
    
        echo "…" | fmt -w 80
    
    The `-w` option specifies the maximum width of the text, but the text will
    use only 93 percent of the line (this can be overridden by using `-g`).
    
    The long sentence that hits the 80 character column count is this one
    and here.
    

    您会注意到最后一个(正好 80 个字符长)被包装了,但不是在第 75 列(80 的 93%),而是在它之前的一个单词,因为我们不想单独在行上有一个单词(在印刷文本等)。

    • 3

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

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

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve