HELP PLS!! Shell Scripting!!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting HELP PLS!! Shell Scripting!!
# 1  
Old 09-14-2006
HELP PLS!! Shell Scripting!!

Dear All,

forgive me as i am a complete beginner in shell scripting in UNIX.

I have a file with data similair to the following

8 McDonalds Sandwich 1.99
9 Mcdonalds Fries 1.20
13 McDonalds Milkshake 1.20
7 KFC Fillet Tower 2.50
15 KFC Fries 1.00
3 Burger King Whopper 2.99
14 Burger King Fries 1.40
17 Burger King Soda 1.60

I want to arrange this file so instead of the above, the information is shown in the following pattern;

McDonalds


Fries 9 1.20
Sandwich 8 1.99
Milkshake 13 1.20

And so on for KFC & Burger King ...

what is important is the way McDonalds is now as a heading with its menu being summarised.

Sorry if i havnt explained this well!!

Please can sum1 help me?
I know it involves writing a script in awk or sed but not sure how to start.
Major thanks in advance thank u

Mary
# 2  
Old 09-14-2006
Code:
awk ' { if ( nm != to_lower($2) { nm=to_lower($2);print nm }
          printf("%s",$1)
          for( i = 3; i <= NF ; ++i )
                printf(" %s",$i)
          printf("\n")
}' file

# 3  
Old 09-14-2006
Or:

Because your data has no conclusive way to tell fields apart, you need to
add field sep characters. I used | - without this you would have to resort
to a huge nest of if-then-else logic, because you want to rearrange by what
amounts to a single number column then a multi-column value (1..2)

data
Code:
8 |McDonalds| Sandwich| 1.99
9 |McDonalds| Fries |1.20
13|McDonalds| Milkshake 1.20
7 |KFC| Fillet Tower |2.50
15|KFC| Fries |1.00
3 |Burger King| Whopper |2.99
14|Burger King| Fries |1.40
17|Burger King| Soda |1.60

Code:
 awk -F'|' '{if($2!=old) {print $2; old=$2}
             print $3,$1,$4
            }' filename

output
Code:
McDonalds
 Sandwich 8   1.99
 Fries  9  1.20
 Milkshake 1.20 13 
KFC
 Fillet Tower  7  2.50
 Fries  15 1.00
Burger King
 Whopper  3  2.99
 Fries  14 1.40
 Soda  17 1.60

Anbu does your code produce this output?
# 4  
Old 09-14-2006
It wont produce that output.
# 5  
Old 09-16-2006
Hi Anbu23,

Can you pls explain the following awk code:
Code:
awk ' { if ( nm != to_lower($2) { nm=to_lower($2);print nm }
          printf("%s",$1)
          for( i = 3; i <= NF ; ++i )
                printf(" %s",$i)
          printf("\n")
}' file

Thanks in advance.
# 6  
Old 09-16-2006
alternative in Python:

Code:
#assume known data of food provider.
data = ['McDonalds' , 'KFC' , 'Burger King']
basket = {} #to store results

def insert(dictionary,key,val):
    if dictionary.has_key(key):
      dictionary[key].append(val)
    else:
      dictionary[key] = [val]

for items in data:      
	for lines in open("inputfile.txt"):
		splitted = lines.split(" ")		
		qty,middle,price = splitted[0], ' '.join(splitted[1:-1]), splitted[-1]	
		if items in middle:			
			food = middle[len(items):] #eg get Fries, Whopper
			insert(basket,items, ' '.join([food,qty,price]))
			
for i in sorted(basket.keys()):
	print i
	print " " + ' '.join(basket[i])
	print

Output:

Code:
/home> python test.py
Burger King
  Whopper 3 2.99
  Fries 14 1.40
  Soda 17 1.60

KFC
  Fillet Tower 7 2.50
  Fries 15 1.00


McDonalds
  Sandwich 8 1.99
  Fries 9 1.20
  Milkshake 13 1.20

# 7  
Old 09-16-2006
CPU & Memory another simple soln

Code:
for ((i=1;i<=`awk -F'|' '{print $2}' file | uniq | wc -l`;i++))
do
        word=`awk -F'|' '{print $2}' file | uniq |head -$i | tail -1`
        echo $word
        grep "$word" file | awk -F'|' '{print $3,$1,$4}'
done

the output is as follows:-
Code:
[sayonm@zion ~]$ sh script.sh
McDonalds
 Sandwich 8   1.99
 Fries  9  1.20
 Milkshake 13  1.20
KFC
 Fillet Tower  7  2.50
 Fries  15 1.00
Burger King
 Whopper  3  2.99
 Fries  14 1.40
 Soda  17 1.60

NOTE: assumed that the file was delimited by "|"
cheers,
Sayon

ps: edited previous post

Last edited by sayonm; 09-16-2006 at 05:24 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help with first shell script pls.

Hi, I'm trying to extract information from one file to update another one and am a bit stuck. the first file is made up of tags e.g. <item>a@b.com</item> jksdhfjkdsh sldkjfds l klsjdf <item> c@d.com </item> what i'd like to do is extract the email addresses between these tags,... (6 Replies)
Discussion started by: newb1000
6 Replies

2. Shell Programming and Scripting

Shell scripting : pls help me

I have an input file in this format (shown below). I have to select the lines which doesnt followed by 'miR-" and to save such lines into an output file. For easy identification they are shown here in blue color. They have to be selected. Pls. help me to write a shell script to select those lines... (5 Replies)
Discussion started by: hravisankar
5 Replies

3. Shell Programming and Scripting

Shell program help pls

first queestion if what does "-s" mean? second seqx -n 10000000 -c 10 < $seqfile >$temp seqx? -n? -c? what do these mean? third if && ! cmp -s $missing $temp explain these codes ty (2 Replies)
Discussion started by: imtheone
2 Replies

4. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

5. Shell Programming and Scripting

shell script, pls help

# for i in `cat oo`;do ls -ld $i;done ls: /var/tmp/i: No such file or directory ls: i: No such file or directory ls: /var/tmp/ii: No such file or directory ls: i: No such file or directory ls: /var/tmp/iii: No such file or directory ls: i: No such file or directory ls: /var/tmp/iiii: No such... (2 Replies)
Discussion started by: cpttak
2 Replies

6. UNIX for Advanced & Expert Users

pls help me in scripting

i want to write a script that if the time is 8 then it give a message that the time is 8 and if it is 9-10 then it gives a message that time to go to office like that. can any one help me? (3 Replies)
Discussion started by: lakshmananindia
3 Replies

7. Shell Programming and Scripting

hi..all..pls...help..!!..profile scripting..

hi.. I want to customize a user in unix which could have only ftp access and NO TELNET ACCESS! ..... As being a newbie to unix to my understanding the user .profile has to be edited but if someone can explain how to edit it so that i can block telnet access for that user.....Is /etc/profile has... (5 Replies)
Discussion started by: rookie250
5 Replies

8. Shell Programming and Scripting

HELP me PLS... Simple Scripting!

this is my script.... SQL> select * from dba_profiles 2 where resource_name in ('FAILED_LOGIN_ATTEMPTS','PASSWORD_LOCK_TIME') 3 order by profile; and this is the output... PROFILE RESOURCE_NAME RESOURCE... (2 Replies)
Discussion started by: liezer
2 Replies

9. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies

10. Shell Programming and Scripting

scripting guru's pls help me with scripting on AIX

can someone pls help me with the script for a files coming from one system to a particular directory and i want to write a script to move those files to another directory on different system by renaming the files... pls someone help me on this... thanking in anticipation.... (1 Reply)
Discussion started by: thatiprashant
1 Replies
Login or Register to Ask a Question