0

I am creating a shell script to take input from CSV files, having two rows ( One Column mentioning time and another file string ).. My script works when there is one row... But it isnot working in multiple rows and also how can i know which row it is searching didnt got the files .

Sample file:

1300,N213
1245,N218
1400,N222
1600,N225

Code, I am trying to make it work.

#!/bin/bash
tr_filepath=/var/opt/data/nms_umts_pms_seg/segment1/
echo "Folder to search for traces ${tr_filepath}"
tr_newpath=/var/opt/ericsson/nms_umts_pms_seg/segment1/edos/4G/
CNTRL_FILE=/home/vx622325/filematch.csv
echo "File Contents of ${CNTRL_FILE} to match with pattern"

for i in $CNTRL_FILE;
do

  t=$(cat $i | awk -F"," '{ print $1}')
  n=$(cat $i | awk -F"," '{ print $2}')
  X=`find "$tr_filepath" -type f -iname "A*."$t"*,*="$n"*.bin.gz"`
  echo -e "Traces To Copy \n $X\n" >> /home/vx622325/result_`date +"%d_%m_%Y"`.csv


if [ -d "$tr_newpath" ]; then
        y= cp -rp $X $tr_newpath
else
        echo "Output folder $tr_newpath not found" > /home/vx622325/result_`date +"%d_%m_%Y"`.csv

fi
done

Files to search for

A20190118.2200+0300-2201+0300_SubNetwork=ONRM_RootMo,SubNetwork=N213,MeContext=N213,ManagedElement=1_rnc_gpehfile_Mp0.bin.gz
A20190118.2200+0300-2201+0300_SubNetwork=ONRM_RootMo,SubNetwork=N213,MeContext=N213,ManagedElement=1_rnc_gpehfile_Mp10.bin.gz
A20190118.2200+0300-2201+0300_SubNetwork=ONRM_RootMo,SubNetwork=N213,MeContext=N213,ManagedElement=1_rnc_gpehfile_Mp11.bin.gz

2 Answers 2

0

Simplified script, which assumes that the file ~vx622325/filematch.csv is a simple CSV file (without embedded newlines or commas):

#!/bin/sh

tr_filepath=/var/opt/data/nms_umts_pms_seg/segment1
tr_newpath=/var/opt/ericsson/nms_umts_pms_seg/segment1/edos/4G

result_out=~vx622325/$( date +'result_%d_%m_%Y.log' )

if [ -d "$tr_newpath" ]; then
    printf 'Destination does not exist: %s\n' "$tr_newpath" >"$result_out"
    exit 1
fi

set --
while IFS=, read -r a b; do
   set -- "$@" -o -name "A*.$a*,*=$b*.bin.gz"
done <~vx622325/filematch.csv
shift   # removes the initial "-o"

find "$tr_filepath" -type f \( "$@" \) -exec sh -c '
    dest=$1; shift
    cp "$@" "$dest"/
    printf "Copied: %s\n" "$@"' sh "$tr_newpath" {} + >"$result_out"

This reads each record from the input CSV file using read, with a comma as the field delimiter, into the two variables a and b.

The variables are used to create OR-ed -name patterns for find.

find is then used to look for files whose names match the patterns. The files are copied to the destination directory in batches, and their names are recorded in the output file.

Using GNU tools, you could do the following:

#!/bin/bash

tr_filepath=/var/opt/data/nms_umts_pms_seg/segment1
tr_newpath=/var/opt/ericsson/nms_umts_pms_seg/segment1/edos/4G

printf -v result_out '%s/result_%(%d_%m_%Y)T.log' ~vx622325 -1

if [ -d "$tr_newpath" ]; then
    printf 'Destination does not exist: %s\n' "$tr_newpath" >"$result_out"
    exit 1
fi

namepats=()
while IFS=, read -r a b; do
   namepats+=( -o -name "A*.$a*,*=$b*.bin.gz" )
done <~vx622325/filematch.csv

find "$tr_filepath" -type f \( "${namepats[@]:1}" \) \
    -exec cp -t "$tr_newpath"/ {} + \
    -printf 'Copied: %p\n' >"$result_out"
0

As mentioned by msp9011, problem is with

for i in $CNTRL_FILE 

where file name is given where as you should be giving content of the file. Easy way is to ready file setting the internal field separator (IFS) as following

while IFS=, read -r column1 column2
do
    echo "column1 : $column1, column2 : $column1"
    #Take action using input
done < $CNTRL_FILE

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.