saurabh agarwal
saurabh agarwal

Reputation: 2184

Matching Id from a file

I have a inputId file as

input_abc,input_xyz,input_def
input_def,input_qwe
input_ghf,input_tgf,input_xyz

I have other matcherFile as

input_xyz
input_def

I want to filter all ids in matcherFile from inputId file, So output is

input_abc
input_qwe
input_ghf,input_tgf

Wat I have written as

inputFileName="$1"
matchFile="$2"
outputFile="$3"

matchFileIds=$(cat $matchFile); 
echo "$matchFileIds"

for IDS in $(cat $inputFileName);
do
    for i in $(echo $IDS | sed "s/,/ /g")
    do
    # here we get each id in inputfile separately, 
    # I want to check if $i belongs id in  matcherFile
    echo "$i"

    done
done

Upvotes: 1

Views: 30

Answers (2)

Red Cricket
Red Cricket

Reputation: 10470

Here's another possible solution.

$ cat input.txt
input_abc,input_xyz,input_def
input_def,input_qwe
input_ghf,input_tgf,input_xyz

$ cat filter.txt
input_xyz
input_def

$ cat filter.sh
#!/bin/sh
input_file=input.txt
filter_file=filter.txt

for filter in $(cat $filter_file)
do
        cmd="$cmd|sed -e's/$filter//g'"
done
cmd="cat ${input_file} ${cmd} | sed -e's/,,//g'| sed -e's/^,//g'| sed -e's/,$//g'"
echo $cmd > ./tmp_script.sh
bash ./tmp_script.sh

$ ./filter.sh
input_abc
input_qwe
input_ghf,input_tgf

Upvotes: 1

Faizan Younus
Faizan Younus

Reputation: 803

What I understand from your post is, you want to delete entries from inputId file those are matching in matcherFile file.

for that I hope this code will help you.

inputFileName="$1"
matchFile="$2"
outputFile="$3"

cp $inputFileName ${inputFileName}_bkp

matchFileIds=$(cat $matchFile); 
echo "$matchFileIds"

for IDS in $(cat $matchFile);
do
 echo $IDS
 sed -i "s/$IDS,//g" $inputFileName
 sed -i "s/,$IDS//g" $inputFileName

done

mv $inputFileName $outputFile
mv ${inputFileName}_bkp $inputFileName 

Upvotes: 1

Related Questions