The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
UNIX program? threewingedfury UNIX for Dummies Questions & Answers 1 02-05-2008 11:15 AM
How do i run a program while in Unix?.... Corrail UNIX for Dummies Questions & Answers 4 10-31-2005 10:51 AM
Help with UNIX Program brand1m UNIX for Dummies Questions & Answers 7 02-28-2005 04:55 AM
looking for a unix C program...help please richardspence2 UNIX for Advanced & Expert Users 3 03-15-2004 08:14 PM
running a c/c++ program in unix kray High Level Programming 2 07-12-2002 12:25 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-25-2008
Registered User
 

Join Date: Mar 2008
Posts: 8
unix awk/sed program

i need a sample unix awk/sed program to replace param3 in a file.

i have sample file a.dat with the following format/length (week 8, sku 20, store 20 and qty 8). all store id's which end with _2 needs to be replaced with div id 2. all store id's which end with _1 needs to be replaced with div id 1.

W23_2009275 999_2 8.991052
W24_2009275 999_2 8.991052
W25_2009275 97_2 8.991052
W26_2009275 96_2 8.991052
W51_2008102 99_1 2.851916
W52_2008102 96_1 2.851916
W51_2008104 96_1 5.468747
W52_2008104 999_1 5.468747

new file b.dat should look like this

W23_2009275 2 8.991052
W24_2009275 2 8.991052
W25_2009275 2 8.991052
W26_2009275 2 8.991052
W51_2008102 1 2.851916
W52_2008102 1 2.851916
W51_2008104 1 5.468747
W52_2008104 1 5.468747
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-25-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,084
Your terminology is weird (what's a sku? what's a div? nah, never mind) but hope this helps.

Code:
awk '{ if ($2 ~ /_1$/) { $2 = 1; } else if ($2 ~ /_2$/) { $2 = 2 } print }'
Reply With Quote
  #3 (permalink)  
Old 03-25-2008
Registered User
 

Join Date: Mar 2008
Posts: 8
syntax

The following awk syntax works, but i need this script to be modified to also sum field4 values by distinct field1, field2, field3

awk '{print substr($0,1,27),substr($2,index($2,"_")+1)," ",substr($0,49)}' testfile.dat > testfile2.dat

Current file
W2_2009 275 999_2 8
W2_2009 275 99_2 7
W25_2009 275 97_1 5
W25_2009 275 96_1 4

New file
W2_2009 275 2 15
W25_2009 275 1 9
Reply With Quote
  #4 (permalink)  
Old 03-25-2008
Registered User
 

Join Date: Mar 2008
Posts: 8
sum with fixed length

i need help with the following awk program needs to be modified to enforce fixed length of 8 char for the sum[i].

awk '{sum[substr($0,1,27),$2] += $3} END {for (i in sum) {print i," ",sum[i]}}' testfile2.dat> testfile3.dat
Reply With Quote
  #5 (permalink)  
Old 03-26-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,084
For fixed-length output, have a look at the printf function.

The "sum by distinct" can be solved by using an associative array. This is pretty much a schoolbook example of what you need it for.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:45 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0