![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help need in Shell Scripting | raghav1982 | Shell Programming and Scripting | 11 | 12-04-2007 06:05 AM |
| Regarding Shell Scripting | anilkarikkandar | UNIX for Dummies Questions & Answers | 3 | 11-20-2006 12:26 AM |
| difference between AIX shell scripting and Unix shell scripting. | haroonec | Shell Programming and Scripting | 2 | 04-12-2006 08:12 AM |
| something else on shell scripting | master_6ez | Shell Programming and Scripting | 1 | 11-21-2004 11:42 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
|
||||
|
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
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 |
|
||||
|
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
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 |
|
||||
|
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
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 cheers, Sayon ps: edited previous post Last edited by sayonm; 09-16-2006 at 04:24 AM.. |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|