BASH:split a file into multi sub-files by the unique mark in one line which contain the keyword and the same mark will go into the same file.

 #!/bin/bash
 # 
 # split a file into multi sub-files by the unique mark in one line which contain the keyword,
 # the lines containing same mark will go into the same file.
 
 #the input file to statistics
 filename=$1
 
 #the keyword to search
  keyword=$2
 
  #the path to put the output files
  fullpath=/tmp/output/"$filename"
  mkdir -p "$fullpath"
 
 
  for tid in `grep "$keyword" "$filename" | awk -F'[' '{print $3}'|awk -F']' '{print $1}'|awk -F' ' '{print $1}'`
  do
      begin=`grep "$tid" "$filename" | head -n1 | awk -F' ' '{print $2}'`
      end=`grep "$tid" "$filename" | tail -n1 | awk -F' ' '{print $2}'`
      ibegin=`echo ${begin//:/}`
      iend=`echo ${end//:/}`
      echo $ibegin $iend
 
      bh=${begin:0:2}
      bm=${begin:3:2}
      bs=${begin:6:2}
      eh=${end:0:2}
      em=${end:3:2}
      es=${end:6:2}
      echo $bh $bm $bs $eh $em $es
  
      etotal=`expr $eh \* 60 \* 60 + $em \* 60 + $es`
      btotal=`expr $bh \* 60 \* 60 + $bm \* 60 + $bs`
      echo $btotal $etotal
  
      timecost=`expr $etotal - $btotal`
      echo $etotal,$btotal, $timecost
 
     grep "$tid" "$filename" > "$fullpath"/"$timecost"
  done

 

posted @ 2018-04-03 18:12  xinyueliu  阅读(46)  评论(0)    收藏  举报