使用 bash 将行转换为列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25317736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:08:22  来源:igfitidea点击:

Convert rows to columns with bash

bash

提问by Robert

I have a large text file that looks like the example below. It has many more groups of rows seperated by an empty row.

我有一个大文本文件,看起来像下面的例子。它有更多组由空行分隔的行。

Aggr2_N1_SATA
Normal
192.168.1.2:/floluesxprd5_ds_vol1
Unknown
522.50 GB
478.69 GB
NFS
10/14/2020 3:21:52 PM
Enabled
Disabled
Not supported

boot_lun_svr1
Normal
NETAPP Fibre Channel Disk (naa.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx):3
Non-SSD
5.00 GB
4.29 GB
VMFS5
2/10/2020 4:26:37 PM
Enabled
Disabled
Supported

I would like to convert it to look like this:

我想把它转换成这样:

Aggr2_N1_SATA,Normal,192.168.1.2:/floluesxprd5_ds_vol1,Unknown,522.50 GB,478.69 GB,NFS,10/14/2020,3:21:52 PM,Enabled,Disabled,Not supported

boot_lun_svr1,Normal,NETAPP Fibre Channel Disk (naa.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx):3,Non-SSD,5.00 GB,4.29 GB,VMFS5,2/10/2020 4:26:37 PM,Enabled,Disabled,Supported

Any ideas are appreciated!

任何想法表示赞赏!

回答by glenn Hymanman

perl:

珀尔:

perl -00 -lpe 's/\n/,/g' file

回答by konsolebox

awk -v RS= -v OFS=, '{ = } 1' file

Output:

输出:

Aggr2_N1_SATA,Normal,192.168.1.2:/floluesxprd5_ds_vol1,Unknown,522.50 GB,478.69 GB,NFS,10/14/2020 3:21:52 PM,Enabled,Disabled,Not supported    
boot_lun_svr1,Normal,NETAPP Fibre Channel Disk (naa.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx):3,Non-SSD,5.00 GB,4.29 GB,VMFS5,2/10/2020 4:26:37 PM,Enabled,Disabled,Supported

With spaces between:

之间有空格:

awk -v RS= -v OFS=, '{ = } NR > 1 { print "" } 1' file

Output:

输出:

Aggr2_N1_SATA,Normal,192.168.1.2:/floluesxprd5_ds_vol1,Unknown,522.50 GB,478.69 GB,NFS,10/14/2020 3:21:52 PM,Enabled,Disabled,Not supported

boot_lun_svr1,Normal,NETAPP Fibre Channel Disk (naa.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx):3,Non-SSD,5.00 GB,4.29 GB,VMFS5,2/10/2020 4:26:37 PM,Enabled,Disabled,Supported

With spaces after every line:

每行后有空格:

awk -v RS= -v OFS=, -v ORS='\n\n' '{ = } 1' file

回答by jaypal singh

Here is a way using GNU sedto complete the rout. Please note that this is only for reference and you are better off using glenn Hymanman'sperlsolution as that is portable and easy to understand.

这是使用GNUsed完成溃败的一种方法。请注意,这仅供参考,您最好使用glenn Hymanman 的perl解决方案,因为它便于携带且易于理解。

Easy way is to read the entire file in one long string separated by ,and then putting newlines when two consecutive ,are seen.

简单的方法是在一个长字符串中读取整个文件,,然后在,看到两个连续的时放置换行符。

sed '
    :a;            # Create a label a for a loop
    $!N;           # Append the next line to pattern space if it is not the last line
    s/\n/,/;       # Substitute the newline with ,
    ta;            # If the substitution modified pattern space repeat the loop
    s/,,/\n\n/g    # At the end, substitute two , with two newlines
' file

One-liner form:

单线形式:

sed ':a;$!N;s/\n/,/;ta;s/,,/\n\n/g' file

However, reading entire file in one long string is not an efficient solution. So there is a paragraph mode for sedwhich allows you to work with one paragraph at a time.

但是,在一个长字符串中读取整个文件并不是一种有效的解决方案。因此,有一种段落模式sed允许您一次处理一个段落。

sed '
    /./ {          # If it is not a blank line
        H;         # Append the pattern space to hold space
        $!d        # Delete it if it is not the last line
    }
    x              # When we encounter a blank line, we swap the pattern and hold space
    s/\n//         # We remove the first empty newline
    s/\n/,/g       # Replace all newlines with ,
    $!G            # If it not the last line swap hold and pattern space for blank lines
' file

One-liner form:

单线形式:

sed '/./{H;$!d};x;s/\n//;s/\n/,/g;$!G' file

Output in both cases:

两种情况下的输出:

Aggr2_N1_SATA,Normal,192.168.1.2:/floluesxprd5_ds_vol1,Unknown,522.50 GB,478.69 GB,NFS,10/14/2020 3:21:52 PM,Enabled,Disabled,Not supported

boot_lun_svr1,Normal,NETAPP Fibre Channel Disk (naa.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx):3,Non-SSD,5.00 GB,4.29 GB,VMFS5,2/10/2020 4:26:37 PM,Enabled,Disabled,Supported

回答by choroba

Perl solution:

Perl解决方案:

perl -pe '$comma and !/^$/ and  print "," ;$comma = !/^$/ and chomp or print "\n"' input

回答by jm666

Maybe this could works

也许这可以奏效

perl -00 -F"\n" -lanE 'say join(",", @F)'

prints

印刷

Aggr2_N1_SATA,Normal,192.168.1.2:/floluesxprd5_ds_vol1,Unknown,522.50 GB,478.69 GB,NFS,10/14/2020 3:21:52 PM,Enabled,Disabled,Not supported
boot_lun_svr1,Normal,NETAPP Fibre Channel Disk (naa.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx):3,Non-SSD,5.00 GB,4.29 GB,VMFS5,2/10/2020 4:26:37 PM,Enabled,Disabled,Supported

and the

 perl -00 -F"\n" -lanE 'say join(",", @F),"\n"'

prints

印刷

Aggr2_N1_SATA,Normal,192.168.1.2:/floluesxprd5_ds_vol1,Unknown,522.50 GB,478.69 GB,NFS,10/14/2020 3:21:52 PM,Enabled,Disabled,Not supported

boot_lun_svr1,Normal,NETAPP Fibre Channel Disk (naa.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx):3,Non-SSD,5.00 GB,4.29 GB,VMFS5,2/10/2020 4:26:37 PM,Enabled,Disabled,Supported

回答by jmunsch

Based on this answer (Format output in columns [bash, grep, sed, awk]) if every eleven rows are being transposed:

基于这个答案(格式输出列 [bash, grep, sed, awk])如果每 11 行被转置:

printf "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n" $(sed 's/ /_/g' file_name) | sed 's/_/ /g'