# HELP PLS!! Shell Scripting!!

**URL:** <https://community.unix.com/t/help-pls-shell-scripting/165156>\
**Category:** Shell Programming and Scripting\
**Created:** [September 14, 2006, 4:13pm UTC](https://community.unix.com/t/help-pls-shell-scripting/165156 "2006-09-14T16:13:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mary\_xxx](https://community.unix.com/letter_avatar/mary_xxx/32/5_5575768a8748004e209b776fc1b2916d.png) [@Mary\_xxx](https://community.unix.com/u/Mary_xxx)\
**Post date:** [September 14, 2006, 4:13pm UTC](https://community.unix.com/t/help-pls-shell-scripting/165156/1 "2006-09-14T16:13:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anbu23](https://community.unix.com/user_avatar/community.unix.com/anbu23/32/351_2.png) [@anbu23](https://community.unix.com/u/anbu23)\
**Post date:** [September 14, 2006, 5:22pm UTC](https://community.unix.com/t/help-pls-shell-scripting/165156/2 "2006-09-14T17:22:36Z")

</div>

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/5_5575768a8748004e209b776fc1b2916d.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [September 14, 2006, 5:27pm UTC](https://community.unix.com/t/help-pls-shell-scripting/165156/3 "2006-09-14T17:27:49Z")

</div>

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

```nohighlight
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

```

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

```

output

```nohighlight
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?

---

<div class="post-metadata">

**Author:** ![anbu23](https://community.unix.com/user_avatar/community.unix.com/anbu23/32/351_2.png) [@anbu23](https://community.unix.com/u/anbu23)\
**Post date:** [September 14, 2006, 5:40pm UTC](https://community.unix.com/t/help-pls-shell-scripting/165156/4 "2006-09-14T17:40:13Z")

</div>

It wont produce that output.

---

<div class="post-metadata">

**Author:** ![nervous](https://community.unix.com/letter_avatar/nervous/32/5_5575768a8748004e209b776fc1b2916d.png) [@nervous](https://community.unix.com/u/nervous)\
**Post date:** [September 16, 2006, 12:55am UTC](https://community.unix.com/t/help-pls-shell-scripting/165156/5 "2006-09-16T00:55:36Z")

</div>

Hi Anbu23,

Can you pls explain the following awk code:

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![ghostdog74](https://community.unix.com/letter_avatar/ghostdog74/32/5_5575768a8748004e209b776fc1b2916d.png) [@ghostdog74](https://community.unix.com/u/ghostdog74)\
**Post date:** [September 16, 2006, 2:04am UTC](https://community.unix.com/t/help-pls-shell-scripting/165156/6 "2006-09-16T02:04:12Z")

</div>

alternative in Python:

```nohighlight

#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)
	print	

```

Output:

```nohighlight
/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

```

---

<div class="post-metadata">

**Author:** ![sayonm](https://community.unix.com/letter_avatar/sayonm/32/5_5575768a8748004e209b776fc1b2916d.png) [@sayonm](https://community.unix.com/u/sayonm)\
**Post date:** [September 16, 2006, 4:15am UTC](https://community.unix.com/t/help-pls-shell-scripting/165156/7 "2006-09-16T04:15:38Z")

</div>

```nohighlight
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:-

```nohighlight
[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

---

<div class="post-metadata">

**Author:** ![ghostdog74](https://community.unix.com/letter_avatar/ghostdog74/32/5_5575768a8748004e209b776fc1b2916d.png) [@ghostdog74](https://community.unix.com/u/ghostdog74)\
**Post date:** [September 16, 2006, 4:47am UTC](https://community.unix.com/t/help-pls-shell-scripting/165156/8 "2006-09-16T04:47:49Z")

</div>

> [@sayonm](#):
>
> ```plaintext
> 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
> 
> ```

just my opinion, there are too many pipes , and file i/o.

---

<div class="post-metadata">

**Author:** ![sayonm](https://community.unix.com/letter_avatar/sayonm/32/5_5575768a8748004e209b776fc1b2916d.png) [@sayonm](https://community.unix.com/u/sayonm)\
**Post date:** [September 16, 2006, 4:54am UTC](https://community.unix.com/t/help-pls-shell-scripting/165156/9 "2006-09-16T04:54:14Z")

</div>

ya right.....just my style 🆒

anyways i just wrongly named my previous post as "just another simple soln"....it should have been "just another soln"..... 😃

---

<div class="post-metadata">

**Author:** ![matrixmadhan](https://community.unix.com/letter_avatar/matrixmadhan/32/5_5575768a8748004e209b776fc1b2916d.png) [@matrixmadhan](https://community.unix.com/u/matrixmadhan)\
**Post date:** [September 16, 2006, 6:52am UTC](https://community.unix.com/t/help-pls-shell-scripting/165156/10 "2006-09-16T06:52:24Z")

</div>

```nohighlight
awk -F" " 'BEGIN{x=""} { if ( $2 != x ) { print $2, "\n", $3, $1, $4; x=$2 } else { print $3, $1, $4 } }' filename

```
