Comparison in shellscripts.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparison in shellscripts.
# 1  
Old 07-18-2007
Data Comparison in shellscripts.

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...
# 2  
Old 07-18-2007
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.
# 3  
Old 07-18-2007
Quote:
Originally Posted by vino
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.
Just a push for now, if I can't figure it out, I'll need a little more help. Smilie
# 4  
Old 07-18-2007
Quote:
Originally Posted by vino
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.
You may also have a look a -E flag and use regexps... Smilie
# 5  
Old 07-18-2007
Quote:
Originally Posted by syndex
Just a push for now, if I can't figure it out, I'll need a little more help. Smilie
Try this, then:

Code:
#!/bin/ksh

read line

str="$(echo $line | sed 's/ /\|/g')"

grep -E "$str" ./mqm.list

# 6  
Old 07-18-2007
Quote:
Originally Posted by grial
Try this, then:

Code:
#!/bin/ksh

read line

str="$(echo $line | sed 's/ /\|/g')"

grep -E "$str" ./mqm.list

This works!!!

Can you explain the sed part please?
# 7  
Old 07-19-2007
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help about comparison

Hello folks, I have two files, which have usernames, I want to see the contents of file1.txt which is missing in file2.txt and another comparison file2.txt contents which is missing in file1.txt. please suggest. file1.txt user u2 u8 a9 p9 p3 u4 z8 aaa ahe oktlo (7 Replies)
Discussion started by: learnbash
7 Replies

2. UNIX for Dummies Questions & Answers

comparison

hi guys i need a program that can compare a value read from a com-port and one from the terminal. can somebody help me??? using linux kernel 2.6.14-M5 can only use standard function in sh and bash... (5 Replies)
Discussion started by: metal005
5 Replies

3. Shell Programming and Scripting

$((...)) and $[...] comparison

Does $((mathematical expression)) and $ mean the same? (7 Replies)
Discussion started by: proactiveaditya
7 Replies

4. UNIX for Dummies Questions & Answers

Cygwin help needed! for copying shellscripts

Hello, this is not exactly a unix specific question however i am sure someone out there may answer my question. The problem is that i write shellscripts and now i want to convert all these shellscripts to .txt. Is it possible? or if someone knows how to copy the content of shellscript then also... (3 Replies)
Discussion started by: salman4u
3 Replies

5. Shell Programming and Scripting

SFTP through shellscripts

Hi Everybody, I am in urgent need of a solution. I need to carry out SFTP activities through a shell script. I have generated public and private keys as shown below: Shell-Prompt$> ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key... (2 Replies)
Discussion started by: $antaclau$
2 Replies

6. Shell Programming and Scripting

need some help..Comparison

I need some help which would probably be for most of you a simple script. I need to read in the data from a .dat file and then compare avg to see who is the highest avg. Here is my script so far. #!/bin/ksh #reading in the data from lab3.dat filename=$1 while read name o1 o2 o3 o4 o5 o6... (0 Replies)
Discussion started by: bluesilo
0 Replies

7. Shell Programming and Scripting

string comparison

Hello experts, (tcsh shell) Quite new to shell scripting... I have got a file with a single word on each line. Want to be able to make a comparison such that i can read pairs of words that are ROT13 to each other. Also, i would like to print the pairs to another file. Any help... (5 Replies)
Discussion started by: Jatsui
5 Replies

8. UNIX for Dummies Questions & Answers

Reengineering ShellScripts

Hi, I'm new in Unix an ShellScripting and I need a programm that create a sequence diagramm graphically from a shell script. I am just working for 2 weeks in Unix and the Shell. And it would help to understand the whole shellsscripts. Is there a freeware tool, that can create such a thing?... (1 Reply)
Discussion started by: roessrob
1 Replies

9. Shell Programming and Scripting

"SCRIPT" (recording session) from ShellScripts

Hi all, Hope that we are all familiar with the "script" command, which helps us to record the session into any file, until we give "exit". Can anyone help me, how to do this process from a shell script!? I face problem while ending the script using "exit" which comes out of the program. This... (3 Replies)
Discussion started by: mohanprabu
3 Replies

10. Filesystems, Disks and Memory

comparison

can anyone point me to a comparison of *nix file systems ? i think i prefer a journalling fs but i would like to see a comparison between several fs's before i make up my mind (2 Replies)
Discussion started by: cnf
2 Replies
Login or Register to Ask a Question