unix awk/sed program


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users unix awk/sed program
# 1  
Old 03-25-2008
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
# 2  
Old 03-25-2008
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 }'

# 3  
Old 03-25-2008
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
# 4  
Old 03-25-2008
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
# 5  
Old 03-26-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX for learning sed/awk/grep..etc..

Greetings all, I am looking for a version of Linux that I can practice my scripting skills on. Currently, I support a massive system running on AIX. I want to do more with awk, sed, grep, and even perl. I am looking for something I can throw on a VM on my personal laptop and mess around with.... (5 Replies)
Discussion started by: jeffs42885
5 Replies

2. Shell Programming and Scripting

Help me in awk or sed in UNIX

I have a fixed length file a.txt that looks like a@ a00 a00000 a00 a000000 a00 a0000 a0000 a00000000 a01 a@ a1 a2 a11 a22 a12 a13 a44 a45 a54 a65 a76 a77 a@ a1 a3 a6 a7 a9 a8 a2 a7 a8 a8 a9 a0 b@ b00... (7 Replies)
Discussion started by: RJG
7 Replies

3. How to Post in the The UNIX and Linux Forums

Help me in awk or sed in UNIX

I have a fixed length file that looks like a@ a00 a00000 a00 a000000 a00 a0000 a0000 a00000000 a01 a@ a1 a2 a11 a22 a12 a13 a44 a45 a54 a65 a76 a77 a@ a1 a3 a6 a7 a9 a8 a2 a7 a8 a8 a9 a0 b@ b00 b00000 b00... (1 Reply)
Discussion started by: RJG
1 Replies

4. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

5. Shell Programming and Scripting

Join the line on delimiter using sed/awk in UNIX

I've input as , abcd| ef 123456| 78| 90 Desired output as, abcdef 1234567890 Anyone please give the solution. (5 Replies)
Discussion started by: jinixvimal
5 Replies

6. UNIX for Dummies Questions & Answers

What should be precedence of using awk, sed, head and tail in UNIX?

Hi All, I am new to unix. In this forum some days back, I have read something like below: 1) Do not use perl if awk can do your work. 2) Do not use awk if sed can do your work. . . . I do not re-collect the whole thing. I think it is good to know the precedence of using these... (2 Replies)
Discussion started by: Prathmesh
2 Replies

7. Shell Programming and Scripting

Problem with awk awk: program limit exceeded: sprintf buffer size=1020

Hi I have many problems with a script. I have a script that formats a text file but always prints the same error when i try to execute it The code is that: { if (NF==17){ print $0 }else{ fields=NF; all=$0; while... (2 Replies)
Discussion started by: fate
2 Replies

8. Shell Programming and Scripting

shell program with sed

I want to substitute a charactor "PAN" with "TAN" in a shell, I used sed command in shell, it wo'nt work but the same is run from command prompt it was successful. the command is sed ' s/PAN/TAN/g ' <i/p> > <o/p> sed ' s/^M/^M/g ' <i/p> > <o/p> (1st ^M is Ctrl+V+M, 2nd should be line feed/next... (1 Reply)
Discussion started by: anil_kut
1 Replies

9. Shell Programming and Scripting

modify and use awk sed program

The following awk script creates a file b.dat. awk '{print substr($0,1,27),substr($2,index($2,"_")+1)," ",substr($0,49)}' a.dat > b.dat I need this script to be modified to also sum $3 values by distinct $1 and $2 fields. Current file W2_2009275 2 8 W2_2009275 2 7 W1_2009275 1... (3 Replies)
Discussion started by: mnnarendra
3 Replies

10. Shell Programming and Scripting

combining unix commands and awk program

Dear Experts I am trying to find if it is possible to combine unix commands in awk program. For example if it is possible embed rm or ls or any unix command inside the awk program and while it is reading the file besides printing be able to do some unix commands. I am thinking may be just print... (2 Replies)
Discussion started by: Reza Nazarian
2 Replies
Login or Register to Ask a Question