壳层,列-t,把第n个空间后的所有东西都当作最后一列

时间:2023-01-14 15:19:28

I want to print a two column table. In the first column are paths (without spaces), in the second column are names (with spaces). I'd like to make the column treat everything after Nth space (N = 2) as belonging to a single column.

我想打印一个两列表。第一列是路径(没有空格),第二列是名称(有空格)。我想让列把第N个空间(N = 2)之后的所有东西都当作属于一个列。

I don't need to use the column, but I'd like something short. Note: I'm not looking for other solution that expects that there are only two columns.

我不需要用这一列,但我想要短一点的。注意:我并不是在寻找期望只有两列的其他解决方案。

mark@localhost ~/.config/chromium/Default/Extensions
% for f in */*/manifest.json; do
    echo -n "$f ";
    cat $f | jq -r '.name + " " + .version';
done | column -t
cjpalhdlnbpafiamejdnhcphjbkeiagm/1.14.22_0/manifest.json     uBlock                  Origin      1.14.22          
dbepggeogbaibhgnhhndojpepiihcmeb/1.62.4_0/manifest.json      Vimium                  1.62.4                       
dffhipnliikkblkhpjapbecpmoilcama/0.3_0/manifest.json         Swap                    My          Cookies  0.3     
gcbommkclmclpchllfjekcdonpmejbdp/2017.12.6_0/manifest.json   __MSG_about_ext_name__  2017.12.6                    
gdbofhhdmcladcmmfjolgndfkpobecpg/4.19_0/manifest.json        Don't                   track       me       Google  4.19
pkehgijcmpdhfbdbbnkijodmdjhbjlgp/2017.11.20_0/manifest.json  __MSG_name__            2017.11.20                   

2 个解决方案

#1


2  

To take the output of some command (which could be your loop) and format it with everything after the first two spaces as being part of one column:

获取某个命令的输出(可以是您的循环),并将前两个空格后的所有内容格式化为一列的一部分:

command | sed 's/ /\t/g; s/\t/ /2; s/\t/ /1' input | column -ts' ' | sed 's/\t/ /g'

As an example, consider this file as input:

例如,将此文件视为输入:

$ cat input
c1 c2 Eric Clapton
column1 column2 David Crosby
very-long-first-column 2nd Jimi Hendrix Jr

We can generate your desired output via:

我们可以通过以下方式生成您想要的输出:

$ sed 's/ /\t/g; s/\t/ /2; s/\t/ /1' input | column -ts' ' | sed 's/\t/ /g'
c1                      c2       Eric Clapton
column1                 column2  David Crosby
very-long-first-column  2nd      Jimi Hendrix Jr

How it works

We work on the text in three steps:

我们分三个步骤来研究文本:

  1. sed 's/ /\t/g; s/\t/ /2; s/\t/ /1'

    sed的s / / \ t / g;s / \ t / / 2;年代/ \ t / / 1

    This replaces all blanks except the first two with tabs.

    这将用制表符替换除前两个空格之外的所有空格。

  2. column -ts' '

    列ts ' '

    This does column -t formatting but applies it only to blank-separated fields.

    它执行列-t格式,但只应用于空格分隔的字段。

  3. sed 's/\t/ /g'

    sed ' s / \ t / / g’

    This converts the tabs in the third column back to blanks.

    这会将第三列中的制表符转换为空白。

Shell function for ease-of-use

First, define:

首先,定义:

format() { sed 's/ /\t/g; s/\t/ /2; s/\t/ /1' "$@" | column -ts' ' | sed 's/\t/ /g'; }

Now, we can apply the formatting in a quick-and-simple way, either as part of a pipeline:

现在,我们可以快速而简单地应用格式,或者作为管道的一部分:

$ cat input | format
c1                      c2       Eric Clapton
column1                 column2  David Crosby
very-long-first-column  2nd      Jimi Hendrix Jr

Or, operating on a file:

或在文件上操作:

$ format input
c1                      c2       Eric Clapton
column1                 column2  David Crosby
very-long-first-column  2nd      Jimi Hendrix Jr

To make the definition of format permanent, place it in your ~/.bashrc file.

要使格式的定义永久,请将它放在您的~/中。bashrc文件。(

Formatting for N number of columns

Let's define a function that will allow us to specify a number of columns:

让我们定义一个函数,使我们可以指定若干列:

frmt() { [[ $1 =~ ^[0-9]+$ ]] && sed "s/ /\t/$1g" "${@:2}" | column -ts' ' | sed 's/\t/ /g'; }

We can format a file for two columns:

我们可以为两个列格式化文件:

$ frmt 2 input
c1                      c2 Eric Clapton
column1                 column2 David Crosby
very-long-first-column  2nd Jimi Hendrix Jr

Or for 3 columns:

或三列:

$ frmt 3 input
c1                      c2       Eric Clapton
column1                 column2  David Crosby
very-long-first-column  2nd      Jimi Hendrix Jr

We can use the same function as part of a pipeline:

我们可以使用与管道部分相同的功能:

$ cat input | frmt 3
c1                      c2       Eric Clapton
column1                 column2  David Crosby
very-long-first-column  2nd      Jimi Hendrix Jr

This approach uses GNU sed and, consequently, will work on Linux, including ArchLinux, but not on OSX/BSD systems.

这种方法使用GNU sed,因此,它将在Linux上运行,包括ArchLinux,但不是在OSX/BSD系统上。

#2


1  

You can try

你可以试着

command | sed 's/[^ ]*/&\t/' | column -ts $'\t'

#1


2  

To take the output of some command (which could be your loop) and format it with everything after the first two spaces as being part of one column:

获取某个命令的输出(可以是您的循环),并将前两个空格后的所有内容格式化为一列的一部分:

command | sed 's/ /\t/g; s/\t/ /2; s/\t/ /1' input | column -ts' ' | sed 's/\t/ /g'

As an example, consider this file as input:

例如,将此文件视为输入:

$ cat input
c1 c2 Eric Clapton
column1 column2 David Crosby
very-long-first-column 2nd Jimi Hendrix Jr

We can generate your desired output via:

我们可以通过以下方式生成您想要的输出:

$ sed 's/ /\t/g; s/\t/ /2; s/\t/ /1' input | column -ts' ' | sed 's/\t/ /g'
c1                      c2       Eric Clapton
column1                 column2  David Crosby
very-long-first-column  2nd      Jimi Hendrix Jr

How it works

We work on the text in three steps:

我们分三个步骤来研究文本:

  1. sed 's/ /\t/g; s/\t/ /2; s/\t/ /1'

    sed的s / / \ t / g;s / \ t / / 2;年代/ \ t / / 1

    This replaces all blanks except the first two with tabs.

    这将用制表符替换除前两个空格之外的所有空格。

  2. column -ts' '

    列ts ' '

    This does column -t formatting but applies it only to blank-separated fields.

    它执行列-t格式,但只应用于空格分隔的字段。

  3. sed 's/\t/ /g'

    sed ' s / \ t / / g’

    This converts the tabs in the third column back to blanks.

    这会将第三列中的制表符转换为空白。

Shell function for ease-of-use

First, define:

首先,定义:

format() { sed 's/ /\t/g; s/\t/ /2; s/\t/ /1' "$@" | column -ts' ' | sed 's/\t/ /g'; }

Now, we can apply the formatting in a quick-and-simple way, either as part of a pipeline:

现在,我们可以快速而简单地应用格式,或者作为管道的一部分:

$ cat input | format
c1                      c2       Eric Clapton
column1                 column2  David Crosby
very-long-first-column  2nd      Jimi Hendrix Jr

Or, operating on a file:

或在文件上操作:

$ format input
c1                      c2       Eric Clapton
column1                 column2  David Crosby
very-long-first-column  2nd      Jimi Hendrix Jr

To make the definition of format permanent, place it in your ~/.bashrc file.

要使格式的定义永久,请将它放在您的~/中。bashrc文件。(

Formatting for N number of columns

Let's define a function that will allow us to specify a number of columns:

让我们定义一个函数,使我们可以指定若干列:

frmt() { [[ $1 =~ ^[0-9]+$ ]] && sed "s/ /\t/$1g" "${@:2}" | column -ts' ' | sed 's/\t/ /g'; }

We can format a file for two columns:

我们可以为两个列格式化文件:

$ frmt 2 input
c1                      c2 Eric Clapton
column1                 column2 David Crosby
very-long-first-column  2nd Jimi Hendrix Jr

Or for 3 columns:

或三列:

$ frmt 3 input
c1                      c2       Eric Clapton
column1                 column2  David Crosby
very-long-first-column  2nd      Jimi Hendrix Jr

We can use the same function as part of a pipeline:

我们可以使用与管道部分相同的功能:

$ cat input | frmt 3
c1                      c2       Eric Clapton
column1                 column2  David Crosby
very-long-first-column  2nd      Jimi Hendrix Jr

This approach uses GNU sed and, consequently, will work on Linux, including ArchLinux, but not on OSX/BSD systems.

这种方法使用GNU sed,因此,它将在Linux上运行,包括ArchLinux,但不是在OSX/BSD系统上。

#2


1  

You can try

你可以试着

command | sed 's/[^ ]*/&\t/' | column -ts $'\t'