LINES in shell programming


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting LINES in shell programming
# 1  
Old 01-24-2013
LINES in shell programming

my script.
My program is called match. Also file 1.txt contains
Code:
apple orange juice
table chair cup

Code:
#!/bin/sh
program=$1
while read a b
do
if ["$a"="$program"]
then
echo a
fi
done<1.txt

I type MATCH APPLE. It has to compare "apple" with first token of each line of file 1.txt. If they are the same than print WHOLE LINE. In my example it has to be "apple orange juice". But my code does not work properly. Not sure what is wrong. Help me please.

Last edited by Franklin52; 01-25-2013 at 05:39 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 01-24-2013
'type 'match apple'? Output whole line (minus any white space around field 1):
Code:
 
echo "$a $b"

# 3  
Old 01-24-2013
try:
Code:
#!/bin/sh
 
program=$1
 
while read a b
do
   if [ "$a" = "$program" ]
   then
      echo "$a" "$b"
   fi
done < 1.txt

# 4  
Old 01-24-2013
Quote:
Originally Posted by rdrtx1
try:
Code:
#!/bin/sh
 
program=$1
 
while read a b
do
   if [ "$a" = "$program" ]
   then
      echo "$a" "$b"
   fi
done < 1.txt

Yes, I have changed but it gives me wrong things.
Code:
./match: line 5: [apple=apple]: command not found
./match: line 5: [table=apple]: command not found
./match: line 5: [home=apple]: command not found

# 5  
Old 01-24-2013
Make sure you include spaces around square brackets and "=" in if statement.
This User Gave Thanks to rdrtx1 For This Post:
# 6  
Old 01-24-2013
Quote:
Originally Posted by DGPickett
'type 'match apple'? Output whole line (minus any white space around field 1):
Code:
 
echo "$a $b"

I meant in command line I type MATCH APPLE Smilie sorry....

---------- Post updated at 04:22 PM ---------- Previous update was at 04:17 PM ----------

thank you so much for helping me Smilie
# 7  
Old 01-24-2013
I'll bite:
Code:
$ cat x
#!/bin/sh

program="$1"

while read a b
do

  if [ "$a" = "$program" ]; then
    echo "$a $b"
  fi

done < 1.txt

exit 0
$ ./x apple
apple orange juice
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with shell programming

1)Shell program which displays a list of all files in the current directory to which you (other than owner and group) have only read and execute permission. 2)Shell program which deletes all the lines containing the word 'UNIX' in the files supplied as arguments to this program. 3)Shell... (1 Reply)
Discussion started by: Ramanath
1 Replies

2. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

3. Shell Programming and Scripting

SHELL PROGRAMMING

Using shell scripting, implement ‘scan.sh' that scans the file system recursively starting from current working directory and generates the file ‘index.txt' that contains a line for each file (or directory) with following fields in tab separated format: 1. The full path of the directory... (1 Reply)
Discussion started by: kranthikiran
1 Replies

4. Shell Programming and Scripting

Needs help on shell programming

Hi guys, I need some help to create a script or command :rolleyes: I do not have much experience in shell programming :confused: I have a file with the lines below on 10000 lines: 12.06.09/14:20:13 4 sms/s 12.06.09/14:20:16 4 sms/s 12.06.09/14:20:17 3 sms/s 12.06.09/14:20:18 3... (5 Replies)
Discussion started by: julienp
5 Replies

5. Shell Programming and Scripting

Shell programming

Hi, Iam new to shell program, I want to check a file which is having same lines 2 times and i want to display it in a seperate file. File format is : AQWERTYU|1234567890 ASDFGHJK|0987654321 ZXCVBNML|1098576453 AQWERTYU|1234567890 I need to take the 1st and 4th lines in the above... (5 Replies)
Discussion started by: nivas
5 Replies

6. Shell Programming and Scripting

{} in shell programming

Could someone please tell me what {} mean when they surround a variable? For instance, $FILE = 'basename $1' //what is passed into this script $BANK = 'dirname $1' $INFILE = ${FILE}.${BANK}.$$ What does $INFILE contain after this assignment? Please let me know Thanks G (4 Replies)
Discussion started by: vgirijanaidu
4 Replies

7. Shell Programming and Scripting

shell programming

I want notes for learning Shell programming (2 Replies)
Discussion started by: Neha Agarwal
2 Replies

8. Shell Programming and Scripting

Shell Programming?

For what purposes should we use shell /what are the tasks we can achieve using shell which is best book to learn shell programming and will nayone tell me diff between shell programming aand shell scripting? Thank u in advance. (1 Reply)
Discussion started by: shrikrishna
1 Replies

9. UNIX for Dummies Questions & Answers

Shell Programming Help

Hi, I am new to shell programming, and hve been given a request to write a shell script. Is there any good places to go to see examples of how to write shell programming scripts? Thanks (4 Replies)
Discussion started by: mec585858
4 Replies

10. UNIX for Dummies Questions & Answers

Shell Programming

I have a fix_table.ksh script that takes a TABLENAME and a date. So, in jk_table_file.txt I have the tables...one per line, and in jk_out_file.txt I have the date in the format I need. The following doesn not 'want' to work in a shell script... for TABLE in `cat jk_table_file.txt`; do ... (2 Replies)
Discussion started by: JWK1
2 Replies
Login or Register to Ask a Question