将命令输出向右移动

the*_*ard 10 bash text-processing indentation

假设我正在编写一个.bashrc文件以在我的登录终端中为我提供一些有用的信息,并且我告诉它运行 cal 命令(一个不错的命令)。我将如何将生成的日历向右移动以匹配我的.bashrc“欢迎信息”的其余部分的格式?

Spa*_*awk 14

cal | sed 's/^/     /'
Run Code Online (Sandbox Code Playgroud)

解释

  • cal |: 将 cal 的输出通过管道传输到…
  • sed 's/^/ /'sed,它将查找 lines 的开头^,替换为空格。您可以在此处更改空格数以匹配所需的格式。

编辑

要保留当天的突出显示cal,您需要告诉它向管道输出“颜色”(突出显示)。从man cal

   --color [when]
          Colorize output.  The when can be never, auto, or always.  Never will turn off coloriz?
          ing  in  all  situations.  Auto is default, and it will make colorizing to be in use if
          output is done to terminal.  Always will allow colors to be outputed when  cal  outputs
          to pipe, or is called from a script.
Run Code Online (Sandbox Code Playgroud)

注意手册中似乎有错字;我需要一个=才能工作。因此,最后的命令是

cal --color=always | sed 's/^/     /'
Run Code Online (Sandbox Code Playgroud)


mik*_*erv 7

cal | nl -bn
Run Code Online (Sandbox Code Playgroud)

...会起作用,因为nl总是缩进它没有编号的输入行以等于它会添加到它所做的那些缩进。默认情况下nl,不对输入的-header 或-footer 部分进行编号,而仅对-body 行进行编号。如果您还告诉它not 对-body 行进行编号,则它根本不会编号,并且只会在每个输入行中插入缩进。

使用-width 和-s分隔符开关来调整缩进的大小。


cal; cal | nl -bn
Run Code Online (Sandbox Code Playgroud)
    December 2015   
Su Mo Tu We Th Fr Sa
       1  2  3  4  5 
 6  7  8  9 10 11 12 
13 14 15 16 17 18 19 
20 21 22 23 24 25 26 
27 28 29 30 31       

           December 2015   
       Su Mo Tu We Th Fr Sa
              1  2  3  4  5 
        6  7  8  9 10 11 12 
       13 14 15 16 17 18 19 
       20 21 22 23 24 25 26 
       27 28 29 30 31       
Run Code Online (Sandbox Code Playgroud)

还有:

cal | pr -to[num]
Run Code Online (Sandbox Code Playgroud)

...[num]您要在每行开头插入的空格数在哪里。pr通常每页打印 66 行,但-t告诉它省略页眉/页脚插入,只打印输入到输出。所述-o然后ffset余量开关是用来插入尽可能多的空间作为被请求在[num]在每条输出线的头:

cal; cal | pr -to10
Run Code Online (Sandbox Code Playgroud)
    December 2015   
Su Mo Tu We Th Fr Sa
       1  2  3  4  5 
 6  7  8  9 10 11 12 
13 14 15 16 17 18 19 
20 21 22 23 24 25 26 
27 28 29 30 31       

              December 2015   
          Su Mo Tu We Th Fr Sa
                 1  2  3  4  5 
           6  7  8  9 10 11 12 
          13 14 15 16 17 18 19 
          20 21 22 23 24 25 26 
          27 28 29 30 31       
Run Code Online (Sandbox Code Playgroud)


Sté*_*las 6

与paste:

cal | paste /dev/null -
Run Code Online (Sandbox Code Playgroud)

paste将文件行粘贴在一起,中间有 TAB 字符。在这里,我们将空文件 ( /dev/null) 与pastestdin ( -)粘贴在一起,这是cal通过管道馈送的输出。