The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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
Filtering and Grouping: A Comparison of SQL, Linux Scripting, and Ruby iBot Oracle Updates (RSS) 0 04-06-2008 06:10 AM
Grouping and summing data through unix pcharanraj Shell Programming and Scripting 1 12-14-2007 10:20 AM
grouping of numbers with script cdfd123 Shell Programming and Scripting 2 11-01-2007 05:37 PM
grouping of objects and print their number cdfd123 Shell Programming and Scripting 3 10-04-2007 02:49 AM
egrep and grouping effigy Shell Programming and Scripting 1 10-14-2004 11:45 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-15-2008
dhanamurthy dhanamurthy is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 105
awk - Grouping lines

Hi All

I have the input file as

INFOR00028114 GRAINS BAKERY 4000
INFOR00028114 GRAINS BAKERY 4000
INFOR00028114 GRAINS BAKERY 4000
INFOR0009183-RIVERS - IC 2672
INFOR0009183-RIVERS - IC 2672
INFOR0009183-RIVERS - IC 2672
INFOR0009183-RIVERS - IC 2671

I want the output like
BRAND 14 GRAINS BAKERY 000281 3 4000
BRAND 3-RIVERS - IC 000918 1 2671
BRAND 3-RIVERS - IC 000918 3 2672
BRAND 5 STAR 001972 2 3618



The Layout would be like
postion 1-5 for NAME1
position 6-6 for NAME2
position 12-41 for NAME3
position 42-46 for NAME4

I framed the below logic but i am getting the output like
BRAND 14 GRAINS BAKERY 000281 3 4000
BRAND 3-RIVERS - IC 000918 1 2671
BRAND 5 STAR 001972 2 3618
which is not that expected.

awk '{
c[$0]++
a=substr($0,1,5)
b=substr($0,12,30)
ff=substr($0,6,6)
d=substr($0,42,4)
j[a" "b" "ff]=c[$0]" " d
}END {for(i in j) print i, j[i]}' tes|sort

I am not sure what needs to be changed.
Can any one help me?

Regards
Dhana
  #2 (permalink)  
Old 05-15-2008
dhanamurthy dhanamurthy is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 105
Awk - Grouping Lines

Hi
Any one with some clues ?

Regards
Dhana
  #3 (permalink)  
Old 05-15-2008
dhanamurthy dhanamurthy is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 105
Awk - Grouping Lines

Hi
I framed the below awk script

awk '{
key=substr($0,1,41)
key1=substr($0,1,11)
curr=$NF
c[key]++
curr=arr[key]
curr_key=key
arr[key]= sprintf("%s %-50s %6s %-6s %s",substr($0,1,5),substr($0,12,30),substr($0,6,6),c[key],$NF)
prev_key=curr_key
prev=curr

}
END {for (i in arr) {print arr[i]} } ' /HS_Data_00/tmp/test/tes | sort


When the input file is
VARTY002639DUTCHY 4000
VARTY0026592% REDUCED FAT RC 3590
VARTY0026592% REDUCED FAT RC 3590
VARTY0026592% REDUCED FAT RC 3590
VARTY002660SACCHET 3360
VARTY0026621.5% REDUCED FAT 3550
VARTY0026621.5% REDUCED FAT 3550
VARTY0026631% REDUCED FAT 3550
VARTY0026631% REDUCED FAT 3550
VARTY002675BORSETTI 3360
VARTY002705CRISP 4000
VARTY002705CRISP 4000
VARTY002705CRISP 4001
VARTY002734CRISPY CRUNCHY 4000
VARTY002742CRACKLING GOLDEN 4000
VARTY002784STEAM IN BAG 2622
VARTY002784STEAM IN BAG 2625
VARTY002784STEAM IN BAG 2625
VARTY002784STEAM IN BAG 2626

I expect the output to be
VARTY 1% REDUCED FAT 002663 2 3550
VARTY 1.5% REDUCED FAT 002662 2 3550
VARTY 2% REDUCED FAT RC 002659 3 3590
VARTY BORSETTI 002675 1 3360
VARTY CRACKLING GOLDEN 002742 1 4000
VARTY CRISP 002705 3 4001
VARTY CRISPY CRUNCHY 002734 1 4000
VARTY DUTCHY 002639 1 4000
VARTY SACCHET 002660 1 3360
VARTY STEAM IN BAG 002784 4 2626 2622 2625


i am getting the count of VARTY002784 (how many times it had occured) and grouping the last columns to have unique values.

But i got the output for my program as

VARTY 1% REDUCED FAT 002663 2 3550
VARTY 1.5% REDUCED FAT 002662 2 3550
VARTY 2% REDUCED FAT RC 002659 3 3590
VARTY BORSETTI 002675 1 3360
VARTY CRACKLING GOLDEN 002742 1 4000
VARTY CRISP 002705 3 4001
VARTY CRISPY CRUNCHY 002734 1 4000
VARTY DUTCHY 002639 1 4000
VARTY SACCHET 002660 1 3360
VARTY STEAM IN BAG 002784 4 2626


Can any one help me?

Regards
Dhana
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:39 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0