Friday, December 27, 2019

How to find common lines in two files

While developing two Docker images, I want to find common lines for bash scripts I use to build and run the Docker images. There are fancy applications such as Beyond Compare (recommended, used by some of the best software engineers I know), or open source applications such as Meld, kdiff3 or WinMerge.

fgrep

For simple command line comparison, use fgrep:


$ fgrep -f docker_build.sh ../another-project/docker_build.sh
#!/usr/bin/env bash

user=`whoami`
uid=`id -u`
gid=`id -g`

VERSION="1.0.1"
DOCKER_REPO="registry.corp.com/"
BRAND="vitis-ai-runtime"
IMAGE_NAME="${DOCKER_REPO}${BRAND}:${VERSION}"
# Create Latest Tag - just another name for the image above
LATEST_CPU_IMAGE_TAG=${DOCKER_REPO}$BRAND:latest
TAR_PATH="${2:-/proj/rdi/staff/hanxuel/vai/docker-images}"

#cp  -f  ../../conda/*.txt .
#cp  -f ../../packages/ubuntu/xrt*18.04* .
#cp -rf /wrk/acceleration/shareData/conda/latest/conda-channel .

#sudo \
while getopts ":t:" opt; do
  case $opt in
    t)
      VERSION="TEST-${OPTARG}"
      echo "-t was triggered, TEST Version: ${VERSION}" >&2
      IMAGE_NAME="${DOCKER_REPO}${BRAND}:${VERSION}"
      unset LATEST_CPU_IMAGE_TAG
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done
docker build --no-cache\
  --network=host \
  --build-arg VERSION=$VERSION \
  -f Dockerfile \
  -t $IMAGE_NAME \
  .
#  -t $IMAGE_NAME \
  #-t vitis-ai-runtime:0.4.5 \

#rm -rf conda-channel xrt* *.txt

echo "Saving Docker Image $IMAGE_NAME"
docker tag $IMAGE_NAME $LATEST_CPU_IMAGE_TAG #registry:5000/$IMAGE_NAME
docker push $IMAGE_NAME
if [ -z ${LATEST_CPU_IMAGE_TAG} ]
then
  docker tag ${IMAGE_NAME} ${LATEST_CPU_IMAGE_TAG};
  docker push ${LATEST_CPU_IMAGE_TAG}
fi
docker save $IMAGE_NAME | gzip -c > ${BRAND}-${VERSION}.tar.gz





If you have GNU grep installed, you can use the -F flag


grep -Ff file1 file2




comm

If the lines are sorted, you can use comm


$ comm -12 docker_run.sh ../another-projecte/docker_run.sh
#!/bin/bash


comm: file 1 is not in sorted order

user=`whoami`
comm: file 2 is not in sorted order
uid=`id -u`
gid=`id -g`


VERSION=1.0.1


IMAGE_NAME="${1:-$CPU_IMAGE_TAG}"
xclmgmt_driver="$(find /dev -name xclmgmt\*)"
docker_devices=""
for i in ${xclmgmt_driver} ;
do
  docker_devices+="--device=$i "
done


render_driver="$(find /dev/dri -name renderD\*)"
for i in ${render_driver} ;
do
  docker_devices+="--device=$i "
done





It does not work with unsorted files though. For example:



$ comm -12 docker_build.sh ../vitis-ai--runtime/docker_build.sh
#!/usr/bin/env bash

comm: file 1 is not in sorted order
comm: file 2 is not in sorted order









No comments:

Post a Comment