linux中通过awk进行文本的对齐格式化处理?awk printf左对齐?

需求描述:

  今天在对一个从excel文件中粘出来的内容进行整理的时候,发现格式很乱,就想用awk工具格式化一下,在此记录一下.

操作过程:

1.从excel中复制出来的内容

job_name    job_group
syncCommStockJob    dataSync
syncStoreChnlJob    dataSync
syncOrderJob    dataSync
syncReportStoreJob    dataSync
hdfsScanJob    report
orderSaleDailyJob    report
jdzmdOrdersJob    report
jdCanJob    report
tbTermSalesJob    report
ess3GTermSalesJob    report
npBusiDayReportJob    report
npBusiWeekMonthReportJob    report
snOrdersScreenJob    report
jdzmdOrderScreenJob    report
jjTermSalesJob    report
termSalesJob    report
jfScanJob    report
operatorSalesJob    report
operatorSpecialSalesJob    report
syncTopSaleByDayJob    report
operatorDataJob    report
npBusiSpecWeekMonthReportJob    report
analyseTopSaleDayJob    report
analyseTopSaleWeekAndMonthJob    report
clearScreenCacheJob    report
commStockReportJob    report
costDetailJob    report
incomeDetailJob    report
bigScreenIncomeAndCostJob    report
termSaleGatherDayJob    report
termSalesGatherJob    report
mobileSaleGatherDayJob    report
mobileTermSalesGatherJob    report
incomeAndCostReportJob    report
businessInfoMonitorJob    report
ticketBigDataJob    report
activityStatisticsByDayJob    report
detectFtpFileJob    report
electronicKafkaInfoJob    report
storePerformmanceJob    report
bdElectronicKafkaDataJob    report
testJob    service
reportJob    service
topSessionJob    service
topGetStoresJob    service
topItemsGetJob    service
topGoodsDetailJob    service
topTradeDownloadJob    service
topUserPermitJob    service
topMsgConsumerJob    service
bdStoreChlJob    service
bdSmsPushJob    service
smsFeedbackJob    service
targetUserJob    service
propertyInfoJob    service
storeAndAdminAreaUploadJob    service
newTargetUserJob    service
blackAndWhiteListSynJob    service
areaCacheJob    sysman
departCacheJob    sysman
departJianPinJob    sysman
allAreaCacheJob    sysman
staffCacheJob    sysman
areaSyncJob    sysman
allFuncrightCacheJob    sysman
synDepartJob    sysman
originDepartCacheJob    sysman
allAdminAreaCacheJob    sysman
adminAreaCacheJob    sysman
testAJob    sysman
testBJob    sysman
storeCacheJob    sysman

备注:就想要第一列是对齐的,同时第二列也是对齐的.

2.使用awk进行格式化(首先将上面的内容粘贴到linux一个文件中)

[root@testvm ~]# cat abc.txt | awk '{printf "%-30s%-15s\n",$1,$2}'               #%-30s表示输出字符串,宽度30位,左对齐.%-15s用来指定第二列的,左对齐,宽度15.两个百分号之间可以没有空格.使用\n对每一行的输出加上换行符
job_name                      job_group      
syncCommStockJob              dataSync       
syncStoreChnlJob              dataSync       
syncOrderJob                  dataSync       
syncReportStoreJob            dataSync       
hdfsScanJob                   report         
orderSaleDailyJob             report         
jdzmdOrdersJob                report         
jdCanJob                      report         
tbTermSalesJob                report         
ess3GTermSalesJob             report         
npBusiDayReportJob            report         
npBusiWeekMonthReportJob      report         
snOrdersScreenJob             report         
jdzmdOrderScreenJob           report         
jjTermSalesJob                report         
termSalesJob                  report         
jfScanJob                     report         
operatorSalesJob              report         
operatorSpecialSalesJob       report         
syncTopSaleByDayJob           report         
operatorDataJob               report         
npBusiSpecWeekMonthReportJob  report         
analyseTopSaleDayJob          report         
analyseTopSaleWeekAndMonthJob report         
clearScreenCacheJob           report         
commStockReportJob            report         
costDetailJob                 report         
incomeDetailJob               report         
bigScreenIncomeAndCostJob     report         
termSaleGatherDayJob          report         
termSalesGatherJob            report         
mobileSaleGatherDayJob        report         
mobileTermSalesGatherJob      report         
incomeAndCostReportJob        report         
businessInfoMonitorJob        report         
ticketBigDataJob              report         
activityStatisticsByDayJob    report         
detectFtpFileJob              report         
electronicKafkaInfoJob        report         
storePerformmanceJob          report         
bdElectronicKafkaDataJob      report         
testJob                       service        
reportJob                     service        
topSessionJob                 service        
topGetStoresJob               service        
topItemsGetJob                service        
topGoodsDetailJob             service        
topTradeDownloadJob           service        
topUserPermitJob              service        
topMsgConsumerJob             service        
bdStoreChlJob                 service        
bdSmsPushJob                  service        
smsFeedbackJob                service        
targetUserJob                 service        
propertyInfoJob               service        
storeAndAdminAreaUploadJob    service        
newTargetUserJob              service        
blackAndWhiteListSynJob       service        
areaCacheJob                  sysman         
departCacheJob                sysman         
departJianPinJob              sysman         
allAreaCacheJob               sysman         
staffCacheJob                 sysman         
areaSyncJob                   sysman         
allFuncrightCacheJob          sysman         
synDepartJob                  sysman         
originDepartCacheJob          sysman         
allAdminAreaCacheJob          sysman         
adminAreaCacheJob             sysman         
testAJob                      sysman         
testBJob                      sysman         
storeCacheJob                 sysman

备注:使用printf进行输出第一列和第二列.然后使用%s表示输出字符串,并且通过负号(-),数字表示的是左对齐输出.这样就达到了对格式进行整理.

 

文档创建时间:2018年7月12日10:59:29

posted @ 2018-07-12 11:01  Zhai_David  阅读(12336)  评论(0编辑  收藏  举报