Echo像book的索引一样排序

时间:2023-01-20 19:38:46

I have a script that modifies all files in a directory and outputs in terminal the status of the modifications in real time, like this example:

我有一个脚本,可以实时修改目录中的所有文件,并在终端中输出修改的状态,例如下面这个示例:

Modifying json.php...   MODIFIED
Modifying layout.php...     ERROR
Modifying a_very_long_named_file.php...     NOT MODIFIED

I'm wondering if there is some command I can use to echo like in an ordered book's index, like this:

我想知道是否有一些命令我可以像在有序的书索引中那样使用,比如:

Modifying json.php..........................MODIFIED
Modifying layout.php........................ERROR
Modifying a_very_long_named_file.php........NOT MODIFIED

3 个解决方案

#1


0  

You can probably do it with echo, put I find printf more satisfying:

你可以用echo,让我觉得printf更令人满意:

printf "Modifying ...................................\033[11G%s\033[35G%s\n" \
    $filename ERROR

Will print the filename starting at column 11, and print ERROR at column 35, with .... in between. If the filename is too large to fit, it will be partially overwritten.

将打印文件名开始列11,和打印错误列35,....在两者之间。如果文件名太大,无法容纳,它将被部分覆盖。

Or:

或者:

If you always have one space after 'Modifying', you could try:

如果你在“修改”后总是有一个空格,你可以试试:

< input column -t | tr ' ' . | sed 's/\./ /'

This requires all of the output to available, so that the maximum width can be determined before any data is produced. Since you say "real time", that is probably not desirable, in which case you could use something like:

这需要所有可用的输出,以便在生成任何数据之前确定最大宽度。既然你说“实时”,那可能不可取,在这种情况下,你可以使用以下内容:

echo Modifying json.php MODIFIED | perl -lane 'print "$F[0] $F[1]", "...",
    "." x ( 30 - length( $F[1] )), $F[2]'

To print "...." that is 33 columns wide (adjust as desired). If any file name is too long, that row will be too wide, but there's really no way to determine the maximum width required unless you wait for all of the data to be available. (Unless you have access to all the file names before you start. Pick a suitable width.) The extra "..." is there to have at least some dots printed on wide rows, rather than concatenating the file name with the message.

打印“....”33列宽(根据需要调整)。如果任何文件名太长,那么该行就太宽了,但是除非您等待所有数据都可用,否则实际上没有办法确定所需的最大宽度。(除非您在开始之前可以访问所有的文件名。选择一个合适的宽度)。额外的“…”至少有一些点打印在宽行上,而不是将文件名与消息连接在一起。

#2


1  

based on your input data, the line below works: (gawk needed)

根据您的输入数据,下面的行可以工作:(gawk需要)

 awk -F '\\.\\.\\. +' -vd=50 '{x=sprintf("%-"d"s   %s",$1,$2);gsub(/  /,"..",x);print x}' file

test

测试

kent$  cat s
Modifying json.php...   MODIFIED
Modifying layout.php...     ERROR
Modifying a_very_long_named_file.php...     NOT MODIFIED

kent$  awk -F '\\.\\.\\. +' -vd=50 '{x=sprintf ("%-"d"s   %s",$1,$2);gsub(/  /,"..",x);print x}' s
Modifying json.php..................................MODIFIED
Modifying layout.php................................ERROR
Modifying a_very_long_named_file.php................NOT MODIFIED

note

请注意

  • you change the number (50 in my example) to what you want

    您将数字(在我的示例中是50)更改为您想要的

  • the gsub(..) part is a little bit risky. it changes two connected spaces to two dots. but I think it wouldn't be big problem at the end there is a space left. (before Error/Modified...).

    gsub(.. .)部分有点危险。它把两个连通的空间变成了两个点。但我认为这最终不会是个大问题,还有空间。(之前的错误/修改…)。

EDIT

编辑

add a vim solution, if you feel playing text in vim is comfortable

添加一个vim解决方案,如果您觉得在vim中播放文本很舒服的话

  • type set ve=all
  • 类型设置ve =
  • remove those spaces after three dots (...) :%s/\. \+/\./g
  • 在三个点后删除这些空格(…):%s/\。\ + / \ / g
  • cursor at first line, type qa$vT.x55|pF.v55|r.jq
  • 在第一行输入qa$vT.x55|pF.v55|r.jq
  • then 20@a
  • 然后20 @a

all key-typing operations are in code block, not so much actually. this will do this text transformation in 20 lines, you could change 20 to 200 or 999 if you have many lines to do. Also this will move your "MODIFIED" etc to 55th column. you could change the number too.

所有的键类型操作都是在代码块中进行的,实际上没有那么多。这将在20行中完成这个文本转换,如果你有很多行要做,你可以将20改为200或999。这也将把你的“修改”等移到第55列。你也可以改变数字。

if you do it in vim, it will look like:

如果你在vim中做,它会看起来像:

Echo像book的索引一样排序

#3


1  

You can also do:

你也可以做的事:

   dots=.................................................
   echo Modifying $file ${dots:${#file}} $msg

This send a substring of dots to echo as the 3rd argument, but is not standard sh. (Works in bash.)

这将发送一个点的子字符串作为第三个参数echo,但不是标准的sh。

#1


0  

You can probably do it with echo, put I find printf more satisfying:

你可以用echo,让我觉得printf更令人满意:

printf "Modifying ...................................\033[11G%s\033[35G%s\n" \
    $filename ERROR

Will print the filename starting at column 11, and print ERROR at column 35, with .... in between. If the filename is too large to fit, it will be partially overwritten.

将打印文件名开始列11,和打印错误列35,....在两者之间。如果文件名太大,无法容纳,它将被部分覆盖。

Or:

或者:

If you always have one space after 'Modifying', you could try:

如果你在“修改”后总是有一个空格,你可以试试:

< input column -t | tr ' ' . | sed 's/\./ /'

This requires all of the output to available, so that the maximum width can be determined before any data is produced. Since you say "real time", that is probably not desirable, in which case you could use something like:

这需要所有可用的输出,以便在生成任何数据之前确定最大宽度。既然你说“实时”,那可能不可取,在这种情况下,你可以使用以下内容:

echo Modifying json.php MODIFIED | perl -lane 'print "$F[0] $F[1]", "...",
    "." x ( 30 - length( $F[1] )), $F[2]'

To print "...." that is 33 columns wide (adjust as desired). If any file name is too long, that row will be too wide, but there's really no way to determine the maximum width required unless you wait for all of the data to be available. (Unless you have access to all the file names before you start. Pick a suitable width.) The extra "..." is there to have at least some dots printed on wide rows, rather than concatenating the file name with the message.

打印“....”33列宽(根据需要调整)。如果任何文件名太长,那么该行就太宽了,但是除非您等待所有数据都可用,否则实际上没有办法确定所需的最大宽度。(除非您在开始之前可以访问所有的文件名。选择一个合适的宽度)。额外的“…”至少有一些点打印在宽行上,而不是将文件名与消息连接在一起。

#2


1  

based on your input data, the line below works: (gawk needed)

根据您的输入数据,下面的行可以工作:(gawk需要)

 awk -F '\\.\\.\\. +' -vd=50 '{x=sprintf("%-"d"s   %s",$1,$2);gsub(/  /,"..",x);print x}' file

test

测试

kent$  cat s
Modifying json.php...   MODIFIED
Modifying layout.php...     ERROR
Modifying a_very_long_named_file.php...     NOT MODIFIED

kent$  awk -F '\\.\\.\\. +' -vd=50 '{x=sprintf ("%-"d"s   %s",$1,$2);gsub(/  /,"..",x);print x}' s
Modifying json.php..................................MODIFIED
Modifying layout.php................................ERROR
Modifying a_very_long_named_file.php................NOT MODIFIED

note

请注意

  • you change the number (50 in my example) to what you want

    您将数字(在我的示例中是50)更改为您想要的

  • the gsub(..) part is a little bit risky. it changes two connected spaces to two dots. but I think it wouldn't be big problem at the end there is a space left. (before Error/Modified...).

    gsub(.. .)部分有点危险。它把两个连通的空间变成了两个点。但我认为这最终不会是个大问题,还有空间。(之前的错误/修改…)。

EDIT

编辑

add a vim solution, if you feel playing text in vim is comfortable

添加一个vim解决方案,如果您觉得在vim中播放文本很舒服的话

  • type set ve=all
  • 类型设置ve =
  • remove those spaces after three dots (...) :%s/\. \+/\./g
  • 在三个点后删除这些空格(…):%s/\。\ + / \ / g
  • cursor at first line, type qa$vT.x55|pF.v55|r.jq
  • 在第一行输入qa$vT.x55|pF.v55|r.jq
  • then 20@a
  • 然后20 @a

all key-typing operations are in code block, not so much actually. this will do this text transformation in 20 lines, you could change 20 to 200 or 999 if you have many lines to do. Also this will move your "MODIFIED" etc to 55th column. you could change the number too.

所有的键类型操作都是在代码块中进行的,实际上没有那么多。这将在20行中完成这个文本转换,如果你有很多行要做,你可以将20改为200或999。这也将把你的“修改”等移到第55列。你也可以改变数字。

if you do it in vim, it will look like:

如果你在vim中做,它会看起来像:

Echo像book的索引一样排序

#3


1  

You can also do:

你也可以做的事:

   dots=.................................................
   echo Modifying $file ${dots:${#file}} $msg

This send a substring of dots to echo as the 3rd argument, but is not standard sh. (Works in bash.)

这将发送一个点的子字符串作为第三个参数echo,但不是标准的sh。