zoukankan      html  css  js  c++  java
  • shell实现大批量word转码然后分析相关字段

    需求

    需要从服务器中的所有附件(2013-2019) 共60G查找相关字段

    在linux上面直接打开doc等是乱码的

    思路

    先全部附件转码为txt, 然后用grep遍历查找字段实现

    转码shell

    #!/bin/bash
    #*************************************************************************
    #         > File Name: doc.sh
    #         > Author: chenglee
    #         > Main : chengkenlee@sina.com
    #         > Blog : http://www.cnblogs.com/chenglee/
    #         > Created Time : 2019年04月10日 星期三 15时16分41秒
    #*************************************************************************
    Year="2018"
    format="txt"
    savedir=$(cd `dirname $0`; pwd)
    filetxt="filetxt"
    
    ls -l ${Year}/ |awk '/^d/ {print $NF}' > ${Year}.logs
    
    function Find(){
        for element in `ls $1`
        do
            dir_or_file=$1"/"$element
            if [ -d $dir_or_file ]
            then
                Find $dir_or_file
            else
                echo $dir_or_file
            fi
        done
    }
    function Filter(){
        cat filelogs | grep doc | grep -v 'pdf|zip|rar|pptv' > filedir
    }
    function Unoconv(){
        exec 2<"filedir"
        while read line2<&2
        do
            unoconv -f ${format} ${line2}
            echo "[${line2}] 已转码..."
            #mv *.txt ${filetxt}/${Year}
        done
    }
    function Move(){
        exec 4<"${Year}.logs"
        while read line4<&4
        do
            mv ${Year}/${line4}/*.txt ${savedir}/${filetxt}/${Year}/
        done
    }
    function Filetxt(){
        if [ -d "${filetxt}/${Year}" ];then
            root_dir="${Year}"
            Find $root_dir > filelogs
            Filter
            sum=`cat filedir | wc -l`
            echo "总数为:${sum}"
            Unoconv
        else
            mkdir -p ${filetxt}/${Year}
            root_dir="${Year}"
            Find $root_dir > filelogs
            Filter
            sum=`cat filedir | wc -l`
            echo "总数为:${sum}"
            Unoconv
        fi
    }
    function main(){
        Filetxt
        echo "全部文件已实现转码为txt类型"
        Move
        echo "已转码的文件已转移到${savedir}/${filetxt}/${Year}/下"
    }
    main
    

    注:先遍历附件中列出日期扔进filelogs这个文件和新建相对文件夹, 然后把所有能转码的doc和docx文件全部扔进filedir文件, 然后脚本直接识别这个文件中的目录文件, 转码方式是libreoffice+unoconv, 全部转码完成会自动把已转好的txt文件转移到filetxt这个文件夹中.

    注:我这是双开

    工具

    yum install libreoffice unoconv -y
    

    注:也可以自己下载包安装, 我偷个懒是直接yum拉取的

     检索shell

    #!/bin/bash
    #*************************************************************************
    #         > File Name: crawler.sh
    #         > Author: chenglee
    #         > Main : chengkenlee@sina.com
    #         > Blog : http://www.cnblogs.com/chenglee/
    #         > Created Time : 2019年04月10日 星期三 10时52分31秒
    #*************************************************************************
    
    filetxt="TXT"
    
    function If(){
        exec 6<"NameFile"
        while read line6<&6
        do
            grep -rn "${line6}" ${filetxt}/ > logs/result-${line6}.logs
            echo "检索${line6}完毕..."
        done
    }
    function main(){
        If
    }
    main
    

    注:全部转好之后,新建一个文件, 名称为NameFile, 里面换行写入需要查找的字段, 然后脚本会自动去读每行字符作为变量, 然后把所有结果扔进logs这个文件夹.

    维护shell

    #*************************************************************************
    #         > File Name: unockill.sh
    #         > Author: chenglee
    #         > Main : chengkenlee@sina.com
    #         > Blog : http://www.cnblogs.com/chenglee/
    #         > Created Time : 2019年04月10日 星期三 22时20分45秒
    #*************************************************************************
    #!/bin/bash
    
    function killAll(){
        echo "等待10秒开始判断"
        sleep 10;
        StringName=`ps aux | grep unoconv | grep -v grep | awk -F '/' '{print$NF}' | awk -F '.' '{print$1}'`
        if [ "$stringname" != "$StringName" ];then
            echo "[转码正常]"
        else
            echo "[卡住了]... 准备干掉当前进程"
            ps aux | grep unoconv | grep -v grep | awk -F ' ' '{print$2}' | xargs kill -9
        fi
    }
    function main(){
        while [ "1" = "1" ]
        do
            stringname=`ps aux | grep unoconv | grep -v grep | awk -F '/' '{print$NF}' | awk -F '.' '{print$1}'`
            killAll
        done
    }
    main

    注:这个是配合转码shell一起使用的, 每10秒检测一下进程(时间可以根据自己调, 一个一般5秒之内能转好), 如果卡住了, 干掉当前的进行下一个.

  • 相关阅读:
    最简单的基于FFMPEG+SDL的音频播放器 ver2 (採用SDL2.0)
    JBPM——工作流概念
    android项目中的拍照和本地图片截图
    hunnu--11547--你的组合数学学得怎样?
    POJ 2253
    [ACM] POJ 3349 Snowflake Snow Snowflakes(哈希查找,链式解决冲突)
    Redis简述
    C/C++,从未过时的编程语言之父
    Android4.4 Framework分析——getContentResolver启动ContentProvider的过程
    2012_p2 寻宝 (treasure.cpp/c/pas)
  • 原文地址:https://www.cnblogs.com/chenglee/p/10689725.html
Copyright © 2011-2022 走看看