I have 2 arrays abc and xyz
abc = ( a b c d e f g h i j k l m n o p q r s t u v w x y z )
and
xyz = ( b c d e f )
lets assume a .... z are the file name.
I have to perform a pattern replacement on each file present in abc array accept the files i have in xyz array. and i am doing the update one each file one at a time.
how do i achieve this in shell script?
i might use a for loop for updating each file
for i in ${abc[@]}
do
.....
but if $i also exist in xyz array don't perform the task or update
done
Hi kukretiabhi13
Like this so you mean?
#!/bin/bash
abc=( "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" )
xyz=( "b" "c" "d" "e" "f" )
for i in ${abc[@]}
do
for ix in ${xyz[@]}
do
if [ "$i" == "$ix" ] ; then
echo $i
fi
done
done
[root@rhnserver ~]# ./del
b
c
d
e
f
The following script proceeds the files from array abc but excludes the files from array xyz.
#!/usr/bin/bash
abc=( a b c d e f g h i j k l m n o p q r s t u v w x y z )
xyz=( b c d e f )
for file in "${xyz[@]}"
do
exclude="${exclude}�${file}�"
done
for file in "${abc[@]}"
do
if [[ "${exclude}" == *�${file}�* ]]
then
echo "Ignore file ${file}"
continue
fi
echo "Proceed file ${file}"
done
Output:
Proceed file a
Ignore file b
Ignore file c
Ignore file d
Ignore file e
Ignore file f
Proceed file g
Proceed file h
Proceed file i
Proceed file j
Proceed file k
Proceed file l
Proceed file m
Proceed file n
Proceed file o
Proceed file p
Proceed file q
Proceed file r
Proceed file s
Proceed file t
Proceed file u
Proceed file v
Proceed file w
Proceed file x
Proceed file y
Proceed file z
Jean-Pierre.
Thanks your solution is working but i cannot understand this part.
exclude="${exclude}�${file}�"
and
if [[ "${exclude}" == *�${file}�* ]]
The following code builds the list of files to be excluded, the character � surrounds each file inside the list:
for file in "${xyz[@]}"
do
exclude="${exclude}�${file}�"
done
The resulting value of the variable exclude is �a��b��c��d�
This following statement tets if the file is present inside this list:
if [[ "${exclude}" == *�${file}�* ]]
Hope this will help you.
Jean-Pierre.