grep unknown number of params


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep unknown number of params
# 1  
Old 10-02-2011
grep unknown number of params

Hey i got trivial question but i cant figure it out. I want to grep a file for multiple unknown parameters. The user will enter these parameters at the command line. For example...

./program red apple filename

This would grep for the phrase red apple. But i cant just do $1 $2 because the user could have entered red gala apple or just apple. I tryed using "$@" but it still doesnt work. Any suggestions? Thanks.
# 2  
Old 10-02-2011
Try this...

Code:
#!/bin/bash
search=$( echo $* | tr ' ' '|' )
egrep "($search)" input_file

--ahamed
# 3  
Old 10-02-2011
didnt work.
# 4  
Old 10-02-2011
did you try the same code?
Please paste the output run with -x option.

Code:
#!/bin/bash
set -x
search=$( echo $* | tr ' ' '|' )
egrep "($search)" input_file


--ahamed
# 5  
Old 10-02-2011
ya i used exactly what you have. The output is too long for the parameters im giving it. The parameters i am searching should only output one line but its giving me every line in the file.
# 6  
Old 10-02-2011
It will search the entire file and give the output. What is your requirement exactly by the way?

Can you paste the input file contents, the arguments you are giving and the expected output?

--ahamed
# 7  
Old 10-02-2011
the file is too big to paste here but here is an example....
some info from the file:

78 16300 Charlotte Amalie town VI 12331 4741 0000003024 0000000002 +18344032 -064933536
78 18100 Charlotte Amalie East CDP VI 2836 951 0000000467 0000000000 +18337018 -064912684
78 19000 Charlotte Amalie West CDP VI 5422 1956 0000004574 0000001576 +18339165 -064960538

what i want to do is type in the command line ./program Charlotte Amalie East CDP

the program would then output the information from that line but not the line before or after. So it has to match the parameters exactly. Thats why i thought $@ would work because its a list of the parameters given. Also note that the example given is 4 parameters long. However it can be 3 parameters long for CHarlotte Amalie town. So the number of parameters differs.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print Unknown Number of User Inputs in awk

Hello, I am new to awk and I am trying to figure out how to print an output based on user input. For example: ubuntu:~/scripts$ steps="step1, step2, step3" ubuntu:~/scripts$ echo $steps step1, step2, step3 I am playing around and I got this pattern that I want: ... (3 Replies)
Discussion started by: tattoostreet
3 Replies

2. Shell Programming and Scripting

Grep lines for number greater than given number

Hello, I am newbie to bash scripting. Could someone help me with the following. I have log file with output as shown below **************************LOG************************* 11/20/2013 9:11:23.64 Pinging xx.xx.xx.xx with 32 bytes of data: 11/20/2013 9:11:23.64 Reply from xx.xx.xx.xx:... (4 Replies)
Discussion started by: meena_2013
4 Replies

3. Shell Programming and Scripting

awk code to ignore the first occurence unknown number of rows in a data column

Hello experts, Shown below is the 2 column sample data(there are many data columns in actual input file), Key, Data A, 1 A, 2 A, 2 A, 3 A, 1 A, 1 A, 1 I need the below output. Key, Data A, 2 A, 2 A, 3 A, 1 A, 1 A, 1 (2 Replies)
Discussion started by: ks_reddy
2 Replies

4. Shell Programming and Scripting

Grep with unknown pattern?

Hi. I have a small problem that I haven't been able to find out of on my own. I am not much into bash scripting, however I use grep now and then when working on my code in order to locate specific objects, so I'll just state my exact problem: The code is huge, and has a function that is... (2 Replies)
Discussion started by: Morridini
2 Replies

5. Shell Programming and Scripting

Check params for number

I have 2 and three params, both I should make sure thay numbers at one single line insted of checking for each one . Example I wroote the following way.. checking for 2 and three seperately but I shud be able to do it at on statement echo $2 | egrep '^+$' >/dev/null 2>&1 if ; then echo... (2 Replies)
Discussion started by: raopatwari
2 Replies

6. Shell Programming and Scripting

Grep to match unknown pattern

Hi there I would like to search a file for a certain pattern, but i don't know the exact pattern i need to search for. What i do know is that i need to search for the pattern that exists the most in a certain file. so if a file looksike this: summer, summer, winter, spring, summer... (4 Replies)
Discussion started by: mrchilly
4 Replies

7. Solaris

PING - Unknown host 127.0.0.1, Unknown host localhost - Solaris 10

Hello, I have a problem - I created a chrooted jail for one user. When I'm logged in as root, everything work fine, but when I'm logged in as a chrooted user - I have many problems: 1. When I execute the command ping, I get weird results: bash-3.00$ usr/sbin/ping localhost ... (4 Replies)
Discussion started by: Przemek
4 Replies

8. Shell Programming and Scripting

Creating an unknown number of arrays

I need to create arrays like this: cnt=0 { while read myline; do if ];then firstpass="${myline##<meas>}" meas="${firstpass%%</meas>}" tempmeas="${meas%%;*}" MEAS$cnt=$tempmeas print $cnt print ${MEAS'$cnt'} ... (2 Replies)
Discussion started by: ajgwin
2 Replies

9. Shell Programming and Scripting

searching and storing unknown number of lines based on the string with a condition

Dear friends, Please help me to resolve the problem below, I have a file with following content: date of file creation : 12 feb 2007 ==================== = name : suresh = city :mumbai #this is a blank line = date : 1st Nov 2005 ==================== few lines of some text this... (7 Replies)
Discussion started by: swamymns
7 Replies

10. Shell Programming and Scripting

printf returning unknown number

Hi, Can anybody tell me why this function returns 49? printf "%d\n" \'10 I don't understand what the \ and ' are there for? thanks!!! (1 Reply)
Discussion started by: m0nk3y
1 Replies
Login or Register to Ask a Question