0

I am using the following simple shell script to read transaction file against the contents of a master file , and just output the matching lines from the transaction file.

Transaction file contains:

this is a sample line - first
this is a sample line - second
this is a sample line - nth line

Master File contains:

first

Output:

this is a sample line - first
for tranfile in transactionfile.txt
do
  grep -f  MasterFile.txt $tranfile >> out.txt
done

PS: When i execute this line outside of the above shel script it works like a charm ; Just that it wont return within this shell script.

What am i missing???

oguz ismail
  • 1
  • 16
  • 47
  • 69
veekay
  • 1

2 Answers2

0

Without the script output text, knowing what shell, i'm just guessing, but i suspect you either fail to find grep in the $PATH ( or using a different version of grep) or fail to find one of the files or you are executing a shell in the command line and another different shell in the script.

Try adding shebang in the script with the correct shell and try to put the grep path (usually /bin/grep or /usr/bin/grep) and also add the full path to the files you are requiring.

To help debug, i suggest you to add a set -x to the top of the script, so the shell will output what is doing and you can notice what is missing. This set -x may be replaced with a -x option in the shebang (example #!/bin/bash -x )

set -x also work in the command line, use set +x to disable it

higuita
  • 2,127
  • 20
  • 25
0

I would have done this using awk

awk 'NR==FNR {a=$0;next} $0~a' master transaction
  • FNR==NR {a=$0;next} stores the data from master file in variable a
  • $0~a test every line of transaction if it contains variable a, if so do the default action print the line.

If master file contains more than one word. Test the last field against all words like this:

awk 'FNR==NR {a[$0];next} $NF in a' master transaction

If word can be anywhere on the line:

awk 'FNR==NR {a[$0];next} {for (i in a) if ($0~i) print}' master transaction
Jotne
  • 40,548
  • 12
  • 51
  • 55
  • I tried the last "awk" statement but it wont write anything to the o/p file.. This is how i tried.. *************************************************************************************************** for paramfile in $BASEDIR/*.prm do ##grep -f parameterMaster.txt $paramfile >> out.txt awk 'FNR==NR {a[$0];next} {for (i in a) if ($0~i) print}' $BASEDIR/$masterfile $paramfile >> out.txt ## done *************************************************************************************************** – veekay Oct 21 '19 at 15:07
  • @veekay Post real data and what you like the output to be. You do not need to mix `grep` in to this, `awk` can do it all. – Jotne Oct 21 '19 at 15:10
  • The grep statement is just commented out from my earlier code.. .. Master File contains: First=one Transaction File this is a sample line - First this is a sample line - second this is a sample line - nth line Pls note that the "First" keyword can be anywhere in the Line/record Output this is a sample line - first Note-1: IF i add more Lines in the master File, i should get matching lines , as output, from the Transaction FIle. – veekay Oct 21 '19 at 16:36