在将文本文件与diff进行比较时如何过滤/忽略特定行

时间:2018-02-19 21:06:41

标签: linux bash

为了进一步说明我想要做什么,我写了下面的脚本。我试图在我的QA和PRD环境之间审核一些文件,并希望最终的Diff输出忽略硬编码值,例如sql连接。我有大约6个不同的值到文件管理器。到目前为止,我已经尝试了几种方法,但是我无法根据需要使用它们。如果有人有任何想法,我愿意以另一种方式做这件事。我是脚本开发的新手,所以我愿意接受任何想法或信息。谢谢:)

#!/bin/bash
#*********************************************************************
# 
# Name: compareMD5.sh
# Date: 02/12/2018
# Script Location: 
# Author: Maggie o
# 
# Description: This script will pull absolute paths from a text file
#              and compare the files via ssh between QA & PRD on md5sum
#              output match or no match 
#              Then the file the non matching files will be imported to a 
#              tmp directory via scp
#              Files will be compared locally and exclude whitespace, 
#              spaces, comments, and hard coded values  
# NOTE: Script may take a several minutes to run
#       
# Usage: Auditing QA to PRD Pass 3
# nohup ./compareMD52.sh > /output/compareMD52.out 2> /error/compareMD52.err
# checking run ps -ef | grep compareMD52*
#**********************************************************************

rm /output/no_matchMD5.txt
rm /output/filesDiffer.txt
echo "Filename | Path" > /output/matchingMD5.txt
#Remove everything below tmp directory recursivly as it was created by previous script run 
rm -rf /tmp/*

for i in $(cat /input/comp_list.txt) #list of files with absolute paths output by compare script
do 
    export filename=$(basename "$i")  #Grab just the filename
    export path=$(dirname "$i") #Just the Directory
    qa_md5sum=$(md5sum "$i") #Get the md5sum
    qa_md5="${qa_md5sum%% *}" #remove the appended path
    export tmpdir=(/tmp"$path")
# if the stat is not null then run the if, if file is exisiting 
    if ssh oracle@Someconnection stat $path'$filename' \> /dev/null 2\>\&1 
    then 
    prd_md5sum=$(ssh oracle@Somelocation "cd $path; find -name '$filename' -
exec md5sum {} \;")
    prd_md5="${prd_md5sum%% *}" #remove the appended path

    if [[ $qa_md5 == $prd_md5 ]] #Match hash as integer
    then

        echo $filename $path  "  QA Matches PRD">> /output/matchingMD5.txt

    else 
        echo $i
        echo $tmpdir
        echo "Copying "$i" to "$tmpdir >> /output/no_matchMD5.txt
        #Copy the file from PRD to a tmp Dir in QA, keep dir structure to avoid issues of same filename exisiting in diffrent directorys 
        mkdir -p $tmpdir # -p creates only if not exisiting, does not produce errors if exisiting 
        scp oracle@Somelocation:$i $tmpdir # get the file from Prd, Insert into tmp Directory 
    fi

fi 

   done

for x in $(cat /output/no_matchMD5.txt) #do a local comapare using diff
do 
    comp_filename=$(basename "$x")
    #Ignore Comments, no white space, no blank lines, and only report if different but not How different  
    qa=(/tmp"$x")
    #IN TEST
    if diff  -bBq -I '^#' $x $qa >/dev/null 
    # Fails to catch files if the Comment then the start of a line
        then
            echo $comp_filename " differs more then just white space, or 
 comment"
            echo $x >> /output/filesDiffer.txt

    fi
done

2 个答案:

答案 0 :(得分:0)

您可以将输出传输到grep -v 像这样:

diff -bBq TEST.sh TEST2.sh | grep -v "^#"

答案 1 :(得分:0)

我能够使用这种方法弄明白

if diff -bBqZ -I '^#' <(grep -vE '(thing1|thing2|thing3)' $x) <(grep -vE '(thing1|thing2|thing3)' $prdfile)