使用bash将行转换为列
我有一个大的文本文件,看起来像下面的例子。它有更多的行组,由一个空行分隔使用bash将行转换为列,bash,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.xxxxxxxxxxxxxxxxxxxx
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
任何想法都很感激 Perl解决方案:
perl -pe '$comma and !/^$/ and print "," ;$comma = !/^$/ and chomp or print "\n"' input
Perl解决方案:
perl -pe '$comma and !/^$/ and print "," ;$comma = !/^$/ and chomp or print "\n"' input
输出:
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
在以下各项之间留有空格:
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
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
输出:
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
在以下各项之间留有空格:
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
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
也许这可以奏效
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
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
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)'
印刷品
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
和
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
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
基于此答案()如果每十一行被转置:
printf "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n" $(sed 's/ /_/g' file_name) | sed 's/_/ /g'
基于此答案()如果每十一行被转置:
printf "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n" $(sed 's/ /_/g' file_name) | sed 's/_/ /g'
perl:
perl:
下面是一种使用GNU
sed
完成路由的方法。请注意,这仅供参考,您最好使用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
有一个段落模式,允许您一次处理一个段落
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
一个班轮形式:
两种情况下的输出:
下面是一种使用GNU
sed
完成路由的方法。请注意,这仅供参考,您最好使用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
有一个段落模式,允许您一次处理一个段落
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
一个班轮形式:
两种情况下的输出:
这是行不通的,因为有些行有空格。更新:但是perl解决方案看起来更优雅,这不是正确的技术。原始数据已经包含下划线,因此将用空格替换它们。一般来说,很难假设一个您希望不会出现在输入中的字符。这是可行的,需要bash 4:
sed'/^$/d'文件|{mapfile-t行;printf“%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,\s\n\n”“${lines[@]}”}
这是行不通的,因为有些行有空格。更新:但是perl解决方案看起来更加优雅,这不是正确的技术。原始数据已经包含下划线,因此将用空格替换它们。一般来说,很难假设一个您希望不会出现在输入中的字符。这是可行的,需要bash 4:sed'/^$/d'文件{mapfile-t行;printf“%s,%s,%s,%s,%s,%s,%s,%s,%s\n'${lines[@]}”}
可以缩短为perl-00lpe'y/\n/,
@AdamSpiers,高尔夫冠军!是的,几年前我参加过Perl高尔夫比赛;-)少一个字符:perl-00lpe y/\\n/,/
goddmanit我生锈了:-(虽然我很喜欢原始的perl高尔夫规则,但是引号不算在内。可以缩短为perl-00lpe'y/\n/,/”
@AdamSpiers,高尔夫冠军!是的,我几年前曾在perl高尔夫比赛;-)少一个字符:perl-00lpe'y/\\n,/
goddanit I'm rusty:-(虽然我对原始的Perl高尔夫规则很满意,但引号并不算在内。+1表示所有变体,其中1个匹配OP的精确期望输出。+1表示所有变体,其中1个匹配OP的精确期望输出。