我有以下代码用于将行转置为列
awk '/VE464 V4.1.06/{if(s){print s;s=$0}else{s=$0}}
/Normal Acq/{s=s";"$0}
/SL/{s=s";"$0}
/SN/{s=s";"$0}
/Gps Status/{s=s";"$0}
/GPGGA/{s=s";"$0}
/GNGSA/{s=s";"$0}
/GPZDA/{s=s";"$0}
/PTNL/{s=s";"$0}
/TB/{s=s";"$0}
END{print s}' input.txt
我的输入数据是这个(input.txt):
% VE464 V4.1.06
% Normal Acq
% SL : 30391.00
% SN : 48097.00
% Gps Status : $GPGGA,034403.00,2059.56495319,N,05034.47277356,E,5,15,0.7,207.283,M,-32.341,M,1.0,0000*6
% GNGSA : $GNGSA,A,3,87,72,65,66,88,81,78,,,,,,1.6,0.7,1.5*27
% GNGST : $GNGST,034403.00,0.001,0.053,0.046,127.2,0.049,0.050,0.017*44
% GPZDA : $GPZDA,034403.02,25,11,2017,00,00*67
% PTNL : $PTNL,GGK,034403.00,112517,2059.56495319,N,05034.47277356,E,12,15,1.6,EHT174.943,M*4C
% TB : 17/11/25 03:43:51:732000 [13431732000 microsec]
% VE464 V4.1.06
% Normal Acq
% SL : 30385.00
% SN : 48161.00
% Gps Status : $GPGGA,035229.00,2059.52294297,N,05034.93435481,E,5,16,0.7,201.012,M,-32.341,M,1.0,0000*6
% GNGSA : $GNGSA,A,3,20,25,18,29,15,10,26,21,,,,,1.5,0.7,1.4*2F
% GNGST : $GNGST,035229.00,0.002,0.053,0.046,136.0,0.050,0.049,0.017*4A
% GPZDA : $GPZDA,035229.01,25,11,2017,00,00*6B
% PTNL : $PTNL,GGK,035229.00,112517,2059.52294297,N,05034.93435481,E,12,16,1.5,EHT168.672,M*43
% TB : 17/11/25 03:52:17:180000 [13937180000 microsec]
% VE464 V4.1.06
% Normal Acq
% SL : 30387.00
% SN : 48161.00
% Gps Status : $GPGGA,035313.00,2059.54233727,N,05034.93011171,E,5,14,0.7,199.952,M,-32.341,M,1.0,0000*
% GNGSA : $GNGSA,A,3,20,25,18,29,15,10,26,21,,,,,1.7,0.7,1.6*2F
% GNGST : $GNGST,035313.00,0.001,0.053,0.046,138.5,0.050,0.049,0.017*4A
% GPZDA : $GPZDA,035313.01,25,11,2017,00,00*63
% PTNL : $PTNL,GGK,035313.00,112517,2059.54233727,N,05034.93011171,E,12,14,1.7,EHT167.612,M*4F
% TB : 17/11/25 03:53:01:180000 [13981180000 microsec]
% VE464 V4.1.06
% Normal Acq
% SL : 30389.00
% SN : 48161.00
% Gps Status : $GPGGA,035348.00,2059.55364804,N,05034.93016495,E,5,15,0.7,199.466,M,-32.341,M,1.0,0000*
% GNGSA : $GNGSA,A,3,87,79,65,66,88,78,,,,,,,1.6,0.7,1.4*24
% GNGST : $GNGST,035348.00,0.001,0.053,0.046,138.4,0.050,0.049,0.016*44
% GPZDA : $GPZDA,035348.01,25,11,2017,00,00*6D
% PTNL : $PTNL,GGK,035348.00,112517,2059.55364804,N,05034.93016495,E,12,15,1.6,EHT167.125,M*46
% TB : 17/11/25 03:53:36:380000 [14016380000 microsec]
然后输出我得到的代码是(行转换为以;分隔的列)
% VE464 V4.1.06;% Normal Acq ;% SL : 30391.00;% SN : 48097.00;% Gps Status : $GPGGA,034403.00,2059.56495319,N,05034.47277356,E,5,15,0.7,207.283,M,-32.341,M,1.0,0000*6;% Gps Status : $GPGGA,034403.00,2059.56495319,N,05034.47277356,E,5,15,0.7,207.283,M,-32.341,M,1.0,0000*6;% GNGSA : $GNGSA,A,3,87,72,65,66,88,81,78,,,,,,1.6,0.7,1.5*27;% GPZDA : $GPZDA,034403.02,25,11,2017,00,00*67;% PTNL : $PTNL,GGK,034403.00,112517,2059.56495319,N,05034.47277356,E,12,15,1.6,EHT174.943,M*4C;% TB : 17/11/25 03:43:51:732000 [13431732000 microsec]
% VE464 V4.1.06;% Normal Acq ;% SL : 30385.00;% SN : 48161.00;% Gps Status : $GPGGA,035229.00,2059.52294297,N,05034.93435481,E,5,16,0.7,201.012,M,-32.341,M,1.0,0000*6;% Gps Status : $GPGGA,035229.00,2059.52294297,N,05034.93435481,E,5,16,0.7,201.012,M,-32.341,M,1.0,0000*6;% GNGSA : $GNGSA,A,3,20,25,18,29,15,10,26,21,,,,,1.5,0.7,1.4*2F;% GPZDA : $GPZDA,035229.01,25,11,2017,00,00*6B;% PTNL : $PTNL,GGK,035229.00,112517,2059.52294297,N,05034.93435481,E,12,16,1.5,EHT168.672,M*43;% TB : 17/11/25 03:52:17:180000 [13937180000 microsec]
% VE464 V4.1.06;% Normal Acq ;% SL : 30387.00;% SN : 48161.00;% Gps Status : $GPGGA,035313.00,2059.54233727,N,05034.93011171,E,5,14,0.7,199.952,M,-32.341,M,1.0,0000*;% Gps Status : $GPGGA,035313.00,2059.54233727,N,05034.93011171,E,5,14,0.7,199.952,M,-32.341,M,1.0,0000*;% GNGSA : $GNGSA,A,3,20,25,18,29,15,10,26,21,,,,,1.7,0.7,1.6*2F;% GPZDA : $GPZDA,035313.01,25,11,2017,00,00*63;% PTNL : $PTNL,GGK,035313.00,112517,2059.54233727,N,05034.93011171,E,12,14,1.7,EHT167.612,M*4F;% TB : 17/11/25 03:53:01:180000 [13981180000 microsec]
% VE464 V4.1.06;% Normal Acq ;% SL : 30389.00;% SN : 48161.00;% Gps Status : $GPGGA,035348.00,2059.55364804,N,05034.93016495,E,5,15,0.7,199.466,M,-32.341,M,1.0,0000*;% Gps Status : $GPGGA,035348.00,2059.55364804,N,05034.93016495,E,5,15,0.7,199.466,M,-32.341,M,1.0,0000*;% GNGSA : $GNGSA,A,3,87,79,65,66,88,78,,,,,,,1.6,0.7,1.4*24;% GPZDA : $GPZDA,035348.01,25,11,2017,00,00*6D;% PTNL : $PTNL,GGK,035348.00,112517,2059.55364804,N,05034.93016495,E,12,15,1.6,EHT167.125,M*46;% TB : 17/11/25 03:53:36:380000 [14016380000 microsec]
在我使用前一个输出进行一些处理后,我将获得(清洁输出):
% VE464 V4.1.06;% Normal Acq ;% SL : 30391.00;% SN : 48097.00;% Gps Status : $GPGGA,034403.00,2059.56495319,N,05034.47277356,E,5,15,0.7,207.283,M,-32.341,M,1.0,0000*6;% Gps Status : $GPGGA,034403.00,2059.56495319,N,05034.47277356,E,5,15,0.7,207.283,M,-32.341,M,1.0,0000*6;% GNGSA : $GNGSA,A,3,87,72,65,66,88,81,78,,,,,,1.6,0.7,1.5*27;% GPZDA : $GPZDA,034403.02,25,11,2017,00,00*67;% PTNL : $PTNL,GGK,034403.00,112517,2059.56495319,N,05034.47277356,E,12,15,1.6,EHT174.943,M*4C;% TB : 17/11/25 03:43:51:732000 [13431732000 microsec]
% VE464 V4.1.06;% Normal Acq ;% SL : 30387.00;% SN : 48161.00;% Gps Status : $GPGGA,035313.00,2059.54233727,N,05034.93011171,E,5,14,0.7,199.952,M,-32.341,M,1.0,0000*;% Gps Status : $GPGGA,035313.00,2059.54233727,N,05034.93011171,E,5,14,0.7,199.952,M,-32.341,M,1.0,0000*;% GNGSA : $GNGSA,A,3,20,25,18,29,15,10,26,21,,,,,1.7,0.7,1.6*2F;% GPZDA : $GPZDA,035313.01,25,11,2017,00,00*63;% PTNL : $PTNL,GGK,035313.00,112517,2059.54233727,N,05034.93011171,E,12,14,1.7,EHT167.612,M*4F;% TB : 17/11/25 03:53:01:180000 [13981180000 microsec]
然后,使用前一个文件将希望从一列到另一行转置,保持与原始文件相同的格式(input.txt)
最终输出应该是这个:
% VE464 V4.1.06
% Normal Acq
% SL : 30391.00
% SN : 48097.00
% Gps Status : $GPGGA,034403.00,2059.56495319,N,05034.47277356,E,5,15,0.7,207.283,M,-32.341,M,1.0,0000*6
% GNGSA : $GNGSA,A,3,87,72,65,66,88,81,78,,,,,,1.6,0.7,1.5*27
% GNGST : $GNGST,034403.00,0.001,0.053,0.046,127.2,0.049,0.050,0.017*44
% GPZDA : $GPZDA,034403.02,25,11,2017,00,00*67
% PTNL : $PTNL,GGK,034403.00,112517,2059.56495319,N,05034.47277356,E,12,15,1.6,EHT174.943,M*4C
% TB : 17/11/25 03:43:51:732000 [13431732000 microsec]
% VE464 V4.1.06
% Normal Acq
% SL : 30387.00
% SN : 48161.00
% Gps Status : $GPGGA,035313.00,2059.54233727,N,05034.93011171,E,5,14,0.7,199.952,M,-32.341,M,1.0,0000*
% GNGSA : $GNGSA,A,3,20,25,18,29,15,10,26,21,,,,,1.7,0.7,1.6*2F
% GNGST : $GNGST,035313.00,0.001,0.053,0.046,138.5,0.050,0.049,0.017*4A
% GPZDA : $GPZDA,035313.01,25,11,2017,00,00*63
% PTNL : $PTNL,GGK,035313.00,112517,2059.54233727,N,05034.93011171,E,12,14,1.7,EHT167.612,M*4F
% TB : 17/11/25 03:53:01:180000 [13981180000 microsec]
如果可以改进并且更容易,并且使用从列到行的最后一个(干净输出)转置,您是否会检查我的代码是否在行之间进行转置?
答案 0 :(得分:1)
你不需要太多编程!
$ tr ';' '\n' <file
除非我错过了一些细节,否则会做这个工作吗?
% VE464 V4.1.06
% Normal Acq
% SL : 30391.00
% SN : 48097.00
% Gps Status : $GPGGA,034403.00,2059.56495319,N,05034.47277356,E,5,15,0.7,207.283,M,-32.341,M,1.0,0000*6
% Gps Status : $GPGGA,034403.00,2059.56495319,N,05034.47277356,E,5,15,0.7,207.283,M,-32.341,M,1.0,0000*6
% GNGSA : $GNGSA,A,3,87,72,65,66,88,81,78,,,,,,1.6,0.7,1.5*27
% GPZDA : $GPZDA,034403.02,25,11,2017,00,00*67
% PTNL : $PTNL,GGK,034403.00,112517,2059.56495319,N,05034.47277356,E,12,15,1.6,EHT174.943,M*4C
% TB : 17/11/25 03:43:51:732000 [13431732000 microsec]
% VE464 V4.1.06
% Normal Acq
% SL : 30387.00
% SN : 48161.00
% Gps Status : $GPGGA,035313.00,2059.54233727,N,05034.93011171,E,5,14,0.7,199.952,M,-32.341,M,1.0,0000*
% Gps Status : $GPGGA,035313.00,2059.54233727,N,05034.93011171,E,5,14,0.7,199.952,M,-32.341,M,1.0,0000*
% GNGSA : $GNGSA,A,3,20,25,18,29,15,10,26,21,,,,,1.7,0.7,1.6*2F
% GPZDA : $GPZDA,035313.01,25,11,2017,00,00*63
% PTNL : $PTNL,GGK,035313.00,112517,2059.54233727,N,05034.93011171,E,12,14,1.7,EHT167.612,M*4F
% TB : 17/11/25 03:53:01:180000 [13981180000 microsec]
在记录之间添加空行
$ sed '$!G' file | tr ';' '\n'
假设记录之间有空行,转换为列可以简化为
$ awk -F'\n' -v RS= -v OFS=';' -v ORS='\n' '{$1=$1}1' file