shell command/script help

September 2, 2010 at 05:55:21
Specs: Windows XP, 3Ghz/2GB

I'm attempting to run a basic shell command/script on the command line. The purpose of this command is to read a file containing a list of symbols (one per line), then grep another file for each of those symbols (sending the output to another file):

for symbol in `cat matched_options.txt`; do grep $symbol, wombat.csv | sort; done

the comma after "$symbol" is required.

The file -- matched_options.txt -- contains one symbol per line. Each symbol is technically a pair of letters/numbers seperated by a single space, like so:

2OU0 C0905
2OU0 C0910
2OU0 C0915

The problem is, it's reading each line as two different symbols:


Does anyone know how I can get it to read each line as one symbol?

Thanks in advance.

See More: shell command/script help

Report •

September 2, 2010 at 06:58:03
As the default field seperator is white space, the shell interprets each line as two columns. Use the while loop:


while read symbol
   grep "$symbol" wombat.csv
done < matched_options.txt

Also, note that $symbol needs to surrounded by double quotes or you'll have the same problem with the grep command.

Report •

September 2, 2010 at 07:41:17
That did the trick. I didn't know how to properly read a file. Thanks.

Report •

September 2, 2010 at 07:45:21
you don't have to call grep for each line of symbol. That's slow.

If you have GNU grep

 grep -f symbol wombat 

Or you can use awk

awk 'FNR==NR{a[$1];next} ($1 in a)' symbol wombat.csv

GNU win32 packages | Gawk

Report •

Related Solutions

Ask Question