シェル スクリプトは csv 列から読み取り、ファイルを検索します。

シェル スクリプトは csv 列から読み取り、ファイルを検索します。

私は、2 つの行 (時刻を示す 1 つの列と、別のファイル文字列) を持つ CSV ファイルから入力を取得するシェル スクリプトを作成しています。スクリプトは、行が 1 つの場合は機能しますが、複数の行では機能しません。また、どの行を検索してファイルを取得できなかったかを知るにはどうすればよいですか。

サンプルファイル:

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

コード、動作するようにしようとしています。

#!/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

検索するファイル

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

答え1

簡略化されたスクリプト。ファイル~vx622325/filematch.csv単純CSV ファイル (埋め込まれた改行やカンマなし):

#!/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"

readこれは、フィールド区切り文字としてコンマを使用して、入力 CSV ファイルから各レコードを 2 つの変数と に読み取りaますb

変数は、 の OR-nameパターンを作成するために使用されますfind

find次に、パターンに一致する名前を持つファイルを検索するために使用されます。ファイルはバッチで宛先ディレクトリにコピーされ、その名前が出力ファイルに記録されます。

GNU ツールを使用すると、次のことができます。

#!/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"

答え2

msp9011が述べたように、問題は

for i in $CNTRL_FILE 

ファイル名はファイルの内容を指定する場所です。簡単な方法は、内部フィールドセパレータ(IFS)を次のように設定してファイルを準備することです。

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

関連情報