0

I am writing a shell script which is as below:

#!/bin/bash
try="7200005781
8110892533
9585055818
9942609990
9943985228"
filename="TAMILNADU,917373342777,POSTPAID
TAMILNADU,917639403547,POSTPAID
TAMILNADU,917639989185,POSTPAID
TAMILNADU,917667882223,POSTPAID
TAMILNADU,918012888899,POSTPAID
TAMILNADU,918098909992,POSTPAID
TAMILNADU,917200005781,POSTPAID
TAMILNADU,918110892533,POSTPAID
TAMILNADU,919585055818,POSTPAID
TAMILNADU,919942609990,POSTPAID
TAMILNADU,919944040565,POSTPAID
TAMILNADU,919943985228,POSTPAID"

#echo -e "\n${try[@]}" -> works
#echo -e "\n"           -> works
#echo -e "\n${filename[@]}" -> works

for j in "${try[@]}"
do
    for k in "${filename[@]}"
    do     
        sed -i 's/$j//g' $k > master.txt
    done
done

I am trying to match 'try' and 'filename'

If any number of matches from 'try' in 'filename'

I need to remove that entry from 'filename'

Kindly help me with this.

Florin Ghita
  • 17,525
  • 6
  • 57
  • 76
  • No closing quotes on `filename`, also single quotes on sed. Probably other problems as well. – 123 Dec 17 '15 at 11:57
  • Yes, i know there are some syntax errors in sed. And yea, forgot to close filename. – sumit gupta Dec 17 '15 at 11:59
  • @tripleee strictly speaking this is not a duplicate as op is asking for rows _not_ matching rather than a join. – hack_on Dec 18 '15 at 05:58
  • @hack_on Thanks for the comment. This is a common enough task that it's probably possible to find a different duplicate which more precisely answers this specific case, but my search fu didn't bring it up yesterday. If you find a better duplicate, do please link it here. – tripleee Dec 18 '15 at 06:00

2 Answers2

0

That's simple if you have the records in files:

egrep -vf try filename
Florin Ghita
  • 17,525
  • 6
  • 57
  • 76
0

This is one way to do it within the script you have, but requires writing one file.

#!/bin/bash
try="7200005781
8110892533
9585055818
9942609990
9943985228"
filename="TAMILNADU,917373342777,POSTPAID
TAMILNADU,917639403547,POSTPAID
TAMILNADU,917639989185,POSTPAID
TAMILNADU,917667882223,POSTPAID
TAMILNADU,918012888899,POSTPAID
TAMILNADU,918098909992,POSTPAID
TAMILNADU,917200005781,POSTPAID
TAMILNADU,918110892533,POSTPAID
TAMILNADU,919585055818,POSTPAID
TAMILNADU,919942609990,POSTPAID
TAMILNADU,919944040565,POSTPAID
TAMILNADU,919943985228,POSTPAID"

echo "$filename" > /tmp/filename_zz.txt
echo "$try" | xargs -L 1 -I{} grep {} /tmp/filename_zz.txt 
rm /tmp/filename_zz.txt

EDIT: didn't realise we want to remove the items, not match them ... replace the last 3 lines of above script with these lines ... credit to @Florin

echo "$filename" > /tmp/filename_zz.txt
echo "$try" > /tmp/try_zz.txt
grep -F -vf /tmp/try_zz.txt /tmp/filename_zz.txt
rm /tmp/try_zz.txt
rm /tmp/filename_zz.txt
hack_on
  • 2,532
  • 4
  • 26
  • 30
  • do i have to explicitly create filename_zz.txt ? or just running your code will work out? – sumit gupta Dec 17 '15 at 13:04
  • You just run the script and it will create the file, then delete it when its done. – hack_on Dec 17 '15 at 21:06
  • what if, I interchange the variables in echo? echo "$try" > /tmp/filename_zz.txt echo "$filename" | xargs -L 1 -I{} grep {} /tmp/filename_zz.txt Actually, your code fetches all the entries related to try.. i need to print other numbers. Pls help. – sumit gupta Dec 18 '15 at 05:08
  • Updated answer as I had not understood the question correctly. – hack_on Dec 18 '15 at 05:56
  • your edit serves my purpose. Thanks a tonn for it. And not forgetting Florin too... thanks guys.. you ppl r amazing. :) – sumit gupta Dec 18 '15 at 06:10