使用bash将行转换为列

时间:2014-08-14 21:16:23

标签: bash

我有一个大文本文件,如下例所示。它有更多的行组由空行分隔。

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

我想将其转换为如下所示:

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

感谢任何想法!

6 个答案:

答案 0 :(得分:8)

perl的:

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

答案 1 :(得分:6)

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

输出:

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

之间有空格:

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

输出:

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

每行后面都有空格:

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

答案 2 :(得分:2)

这是使用GNU sed完成路由的方法。请注意,这仅供参考,您最好使用glenn jackman's perl解决方案,因为它易于理解且易于理解。

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

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

单线形式:

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

但是,在一个长字符串中读取整个文件并不是一种有效的解决方案。因此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

单线形式:

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

两种情况下的输出:

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

答案 3 :(得分:0)

Perl解决方案:

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

答案 4 :(得分:0)

也许这可能有效

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

打印

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

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

打印

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

答案 5 :(得分:0)

根据这个答案(Format output in columns [bash, grep, sed, awk]),如果每十一行都被转置:

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