Search code examples
bashawkgrepxargs

xargs and grep in bash script


I have a script that takes a list of words and searches for them in a file. I use xargs to take the words in the list and pass them to grep.

awk 'BEGIN { FS = ","; } { print $9 }' $file | xargs -I {} grep -w {} $second_file

The first awk command searchs for the 9th column in $file. It produces a list of ~50 names, like YHOO, AAPL, LB, etc (stock ticker symbols). I then pass the result to grep using xargs and search for unique occurrences.

The problem is that grep will sometimes return the correct ticker symbols and others that have a .# appended.

For example:

LB
LB.1

will be returned by the above command.

The file where the search is performed ($second_file above) looks like this

2006-09-30      006733  LB      501797104       25504010        10      26.490000       2006-09-30      28.940000       2006-12-31      0.092488

In the terminal, I can type

grep  '\sLB\s'   $second_file

and it only returns LB and not LB.1, which is what I want.

How I can I replicate the '\sLB\s' in the script, using xargs? If I write

grep '\s{}\s'

in my script it does not work, all results are empty.

Thanks!


Solution

  • Why not just keep it in the one awk command? From what you've told us so far all you need is:

    awk 'NR==FNR{ticks[$9]; next} $3 in ticks' FS="," "$file" FS=" " "$second_file"
    

    If that doesn't do what you want, edit your question to clarify your requirements and provide more representative input/output.