syndex
July 18, 2007, 11:22am
1
This idea for a shellscript I have, is just beyond my reach of knowledge.
Basically I have a file that contains a list like so:
axis
heater
water
yast
The file is called mqm.list.
What I want to do is when the shellscript is run, it prompts the user to input data.
Say the person inputs:
axis fire water yast unibrow car truck
I want it to output, only what is on the mqm.list.
So in the example above, the output should be:
axis water yast
Because it matched the mqm.list.
I've tried a bunch of different things, but I'm not getting the concept down. Could someone help me please? Using a test like:
if [ $input = mqm.list ]
isn't working...
vino
July 18, 2007, 11:27am
2
Do you want the solution or do you want a push in that direction ?
See man grep. Look at the flags -o and -f. You can use the -o flag alone or in combination with -f flag.
syndex
July 18, 2007, 11:37am
3
Just a push for now, if I can't figure it out, I'll need a little more help. :o
grial
July 18, 2007, 11:39am
4
You may also have a look a -E flag and use regexps...
grial
July 18, 2007, 11:41am
5
Try this, then:
#!/bin/ksh
read line
str="$(echo $line | sed 's/ /\|/g')"
grep -E "$str" ./mqm.list
syndex
July 18, 2007, 11:56am
6
This works!!!
Can you explain the sed part please?
grial
July 19, 2007, 3:10am
7
Of course.
In $line is stored a string consisting of words separated by blanks (" ").
The sed juts changes those blanks into "|" which means "OR" on regular expressions. As you may know, "|" is a special character to the shell that needs to be scaped with a "\". The "g" at the end tells sed to process the whole string, not just the first blank.
Regards.