Input file 'order-pref.txt' contains two sql UPDATE commands for each item.
$ cat order-pref.txt
computer networking book
UPDATE orders SET quantity = '5' WHERE id = '9';
UPDATE orders SET quantity = '5' WHERE id = '8';
puzzle and skill games
UPDATE orders SET quantity = '5' WHERE id = '99';
UPDATE orders SET quantity = '5' WHERE id = '98';
arithmetic sequence
UPDATE orders SET quantity = '5' WHERE id = '55';
UPDATE orders SET quantity = '5' WHERE id = '56';
Required:
For each item find and print the first UPDATE sql command (i.e. first occurrence of each set of sql commands for each item).
i.e. required output:
UPDATE orders SET quantity = '5' WHERE id = '9';
UPDATE orders SET quantity = '5' WHERE id = '99';
UPDATE orders SET quantity = '5' WHERE id = '55';
Awk solution:
$ awk '/^UPDATE/{++c;if(c%2==1)print}' order-pref.txt > o.sql
Output:
$ cat o.sql
UPDATE orders SET quantity = '5' WHERE id = '9';
UPDATE orders SET quantity = '5' WHERE id = '99';
UPDATE orders SET quantity = '5' WHERE id = '55';
Related posts:
- Print first or last occurrence of pattern range using awk
- Print lines with more than two occurrence of a pattern using sed, awk, grep
- Count total occurrence of a pattern using awk
- Replace a field other than the first occurrence using awk
- Find pattern with maximum occurrence using awk
- Find nth occurrence of pattern in vi editor

0 comments:
Post a Comment