Joining of Commands.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Joining of Commands.
# 1  
Old 01-07-2010
Joining of Commands.

Hello, i have a few questions which i hope you guys can address. the 1st prob:

The database i am using is a text file which displays the information like this :
Title : Quantity : Price
Persia : 30 : 20
Australia : 40 : 20

Code:
Number=
echo -n "Title : "
read Title
echo -n " Price : "
read Price
if grep -q"$Title" Hello1 | -q"$Price" Hello1 ; then   -- line 7
echo "A"
else 
echo "not there"
fi

What i am trying to do is that when it searches,both the information must be the same , then it will come out the output "A". For example, if i input "Persia" into Title and "20" into Price , only then will it display the output"A". When i ran this, an error like the one below came out.
Code:
 line 7: -q20: command not found

what is the problem with this and how do i solve it?

2nd problem : how can i make sure that when it searches for the information, it is from the correct row? as you can see from the database, both books have the same price.

3rd) After searching and getting the right row , how do i delete it?
# 2  
Old 01-07-2010
What if you changed it to this:
Code:
if grep -q "$Title" Hello1 | grep -q "$Price" Hello1 ; then   #-- line 7

# 3  
Old 01-07-2010
still the same problem. is it possible to use the Sed command to filter the information then delete it?
# 4  
Old 01-07-2010
Change your if statement to:

if grep "$Title" Hello1 | grep -q"$Price" ; then

You were missing your grep command after the pipe, so the shell was trying to find a command called "-q20" and so it gave the error.

Also you should remove the first "-q" so that the output passes through the pipe for the next grep.

This will help the first problem but you will run into issues if you search for a $Price that equals the Quantity value. You will have to use awk or cut to check the position of the value to make sure you found the correct entry.
# 5  
Old 01-08-2010
O could you give some guidance on how the awk code be like when I try to retreive data? For example when I search for apple, how do I get it to display all the information such as price and quantity?
# 6  
Old 01-08-2010
Sure. Since your Price value is the 3rd field delimited by ":", then you could look it up this way:

Code:
# Lookup price
price_value=`grep $Title filename | grep $Price | awk -F":" '{print $3}'`

# Check if price_value matches $Price
if [ $price_value eq $Price ]
then
  # Print record
  echo "Found the following record that matched:"
  grep $Title filename | grep $Price
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Joining lines in different way

Hi all, I'm excited to the part of unix.com forum, and noob to it. I have an query, where I have an file and it contains data like this use thread when posting do no I was expecting the result as use thread thread when when posting posting do do no use thread when thread when... (6 Replies)
Discussion started by: Jose Nirmal
6 Replies

2. Shell Programming and Scripting

Joining

I do have many files of the following format. File_1.txt 1 24 2 25 3 27 4 28 5 29 6 1 File_2.txt 2 5 3 8 4 9 5 10 File_3.txt (3 Replies)
Discussion started by: Lucky Ali
3 Replies

3. Shell Programming and Scripting

Need help joining lines

Hi All, I need the command to join 2 lines into one. I found lots of threads but none give me the sollution. Probably because unix scripting is one of my best features ;) I got a logfile where line 2 needs to be joined with line 1, lines 4 needs to be joined with line 3 etc If you need... (16 Replies)
Discussion started by: rene21976
16 Replies

4. Shell Programming and Scripting

joining two lines

Hi , I want to join two lines in a file, where the second line contain query string. if it doesn't contain that string i don't want to join e.g. Input file is as following: name fame game none none none name fame game cat eat mice I need output file as name fame game none none... (2 Replies)
Discussion started by: ashrafonics
2 Replies

5. Shell Programming and Scripting

Joining files

Hi, Whats the unix function to join multiple files? is it cat? so I have multiple files in the same format and I want to join then by row eg. FILE1 1 3 1 3 1 3 1 3 FILE2 2 4 2 4 2 4 (1 Reply)
Discussion started by: kylle345
1 Replies

6. Shell Programming and Scripting

Joining Variables

Hi Everyone, Thanks in advance for what is hopefully a simple thing. Big picture I am trying to bind computers to AD using their existing name but add a suffix. Here is the part I am having trouble with: computername=`/usr/sbin/scutil --get LocalHostName` suffix=suffix... (2 Replies)
Discussion started by: dcc186
2 Replies

7. Shell Programming and Scripting

Can BASH execute commands on a remote server when the commands are embedded in shell

I want to log into a remote server transfer over a new config and then backup the existing config, replace with the new config. I am not sure if I can do this with BASH scripting. I have set up password less login by adding my public key to authorized_keys file, it works. I am a little... (1 Reply)
Discussion started by: bash_in_my_head
1 Replies

8. Programming

code that reads commands from the standard i/p and executes the commands

Hello all, i've written a small piece of code that will read commands from standard input and executes the commands. Its working fine and is execting the commands well. Accepting arguments too. e.g #mkdir <name of the directory> The problem is that its not letting me change the directory i.e... (4 Replies)
Discussion started by: Phrozen Smoke
4 Replies

9. UNIX for Dummies Questions & Answers

joining files

Hi, Could anyone help me ? I'm trying to join two files, but no common field are on them. So I think on generate \000\ sequence to add for each line on both files, so then will be able to join these files. Any idea? Thanks in advance, (2 Replies)
Discussion started by: Manu
2 Replies

10. UNIX for Dummies Questions & Answers

joining 2 files

Hi, I have two files that I need to find difference between. Do I use diff or join? If join, how do I use it? thanks, webtekie (1 Reply)
Discussion started by: webtekie
1 Replies
Login or Register to Ask a Question