用空列格式化输出

时间:2018-07-18 08:09:38

标签: bash awk

我正在尝试将qstat(SGE)提供的输出格式化为CSV。提供的输出有8列。在最后两行中可以看到,列号7(队列)可以为空:

job-ID  prior      name user            state submit/start at         queue                                                           slots 
-------------------------------------------------------------------------------------------------------------------------------------------------
 12345 0.25000 jobA   michael      r        07/17/2018 17:54:49  something@somehostname.com    72        
 12346 0.25000 jobB   michael      r        07/17/2018 17:54:49  something@somehostname.com    72        
 12347 0.25000 jobC   michael      qw    07/17/2018 13:52:12                                                                        72        
 12348 0.25000 jobD   michael      qw    07/17/2018 13:52:20                                                                        72

我目前通过awk采取的方法行得通,但在第7栏为空。

qstat | awk '{print $1","$2","$3","$4","$5","$6" "$7","$8","$9}' | sed '2d'

这将产生以下输出:

job-ID,prior,name,user,state,submit/start at,queue,slots
12345,0.25000,jobA,michael,r,07/17/2018 17:54:49,something@somehostname.com,72
12346,0.25000,jobB,michael,r,07/17/2018 17:54:49,something@somehostname.com,72
12347,0.25000,jobC,michael,qw,07/17/2018 13:52:12,72,
12348,0.25000,jobD,michael,qw,07/17/2018 13:52:20,72,

我想要的输出是这样(请注意最后两行中的第7列为空,而这些行的末尾没有,

job-ID,prior,name,user,state,submit/start at,queue,slots
12345,0.25000,jobA,michael,r,07/17/2018 17:54:49,something@somehostname.com,72
12346,0.25000,jobB,michael,r,07/17/2018 17:54:49,something@somehostname.com,72
12347,0.25000,jobC,michael,qw,07/17/2018 13:52:12,,72
12348,0.25000,jobD,michael,qw,07/17/2018 13:52:20,,72

2 个答案:

答案 0 :(得分:1)

如果您的Input_file与所示示例相同,则以下内容可能会帮助您。

awk '
FNR==1 && match($0,/.*\/start/){
  sub(/ +$/,"")
  val1=substr($0,RSTART,RLENGTH)
  gsub(/ +/,",",val1)
  val2=substr($0,RSTART+RLENGTH+1)
  gsub(/ +/,",",val2)
  sub(/[a-zA-Z]+$/,",&",val2)
  print val1,val2
  next
}
match($0,/[0-9]+\/[0-9]+\/[0-9]+/){
  gsub(/^ +| +$/,"")
  val1=substr($0,1,RSTART-1)
  gsub(/ +/,",",val1)
  val2=substr($0,RSTART+RLENGTH+1)
  gsub(/ +/,",",val2)
  if($0 !~ /[a-zA-Z]+@[a-zA-Z]+\.com/){ sub(/[0-9]+$/,",&",val2)}
  print val1 substr($0,RSTART,RLENGTH) val2
}'   Input_file

答案 1 :(得分:1)

$ awk -v OFS=',' 'NF<9{$9=$8; $8=""} {$1=$1} NR!=2' file
job-ID,prior,name,user,state,submit/start,at,queue,slots
12345,0.25000,jobA,michael,r,07/17/2018,17:54:49,something@somehostname.com,72
12346,0.25000,jobB,michael,r,07/17/2018,17:54:49,something@somehostname.com,72
12347,0.25000,jobC,michael,qw,07/17/2018,13:52:12,,72
12348,0.25000,jobD,michael,qw,07/17/2018,13:52:20,,72

或者您是否真的关心日期和时间是一个字段:

$ awk -v OFS=',' 'NF<9{$9=$8; $8=""} NR!=2{print $1, $2, $3, $4, $5, $6 " " $7, $8, $9}' file
job-ID,prior,name,user,state,submit/start at,queue,slots
12345,0.25000,jobA,michael,r,07/17/2018 17:54:49,something@somehostname.com,72
12346,0.25000,jobB,michael,r,07/17/2018 17:54:49,something@somehostname.com,72
12347,0.25000,jobC,michael,qw,07/17/2018 13:52:12,,72
12348,0.25000,jobD,michael,qw,07/17/2018 13:52:20,,72