modify and use awk sed program


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting modify and use awk sed program
# 1  
Old 03-25-2008
modify and use awk sed program

The following awk script creates a file b.dat.

Code:
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
Code:
W2_2009275 2    8 
W2_2009275 2    7 
W1_2009275 1   5 
W1_2009275 1   4

New file
Code:
W2_2009275 2 15 
W1_2009275 1 9


Last edited by Yogesh Sawant; 03-26-2008 at 02:41 AM.. Reason: added code tags
# 2  
Old 03-26-2008
Try:
Code:
awk '{d1=substr($0,1,27);d2=substr($2,index($2,"_")+1);d3=substr($0,49)+0;a[d1" "d2]=(a[d1" "d2]+0)+d3}END{for(i in a)print i,a[i]}' a.dat > b.dat

# 3  
Old 03-26-2008
Thank you

It works now. Thank you.
# 4  
Old 03-26-2008
Another one:

Code:
awk '{a[$1" "$2]+=$3}END{for(i in a){print i,a[i]}} 'file

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed, awk or another bash command to modify string with the content of another file

Hello everybody, I would like modify some strings using sed or another command line with the content file. For example: - {fqdn: "server-01" , ip: "server-01"} - {fqdn: "server-02" , ip: "server-02"} - {fqdn: "server-03" , ip: "server-03"} - {fqdn: "server-04" , ip: "server-04"} My... (4 Replies)
Discussion started by: dco
4 Replies

2. Shell Programming and Scripting

Using awk and sed to modify a create sql script

Hi, I have a file which contains the following data claim_src|clm_id,typ_id pat_src|pat_id prov_src|prov_id,clm_id,prov_name The first field is table name and second field is primary keys of the table Now I have three files which contain ddl of each table. clam_src.sql... (4 Replies)
Discussion started by: wahi80
4 Replies

3. Shell Programming and Scripting

Modify xml using sed or awk

Hi All, I want to modify(changing the status from "on" to "off" status of Stage-element value from the below xml file using sed or awk: File Name: global.xml <?xml version="1.0" encoding="UTF-8"?> <config> <widget> <name>HTTP-POOL</name> <attributes> ... (5 Replies)
Discussion started by: wamqemail2
5 Replies

4. Shell Programming and Scripting

Modify text file using sed

Hello all, I have some text files I need to do the following on: Delete banner page (lines 1-56) --I am doing this using sed Remove ^M --I am doing this using vi Remove trailer page --this can vary based on the contents of the file, it usually starts with *************************** I am... (5 Replies)
Discussion started by: jeffs42885
5 Replies

5. Shell Programming and Scripting

Modify the file with awk,sed or perl

Hi All, I need help from any of you.Would be so thankful for your help. I/P DDDD,1045,161,1557,429,1694,800,1911,1113,2460,1457,2917> 1609,3113,1869,3317,2732,3701,3727,4132,5857,5107> 9004,6496 DDDD,1125,157,1558,429,1694,800,1911,1117,2432,1444,2906>... (2 Replies)
Discussion started by: Indra2011
2 Replies

6. Shell Programming and Scripting

Modify sed script

I'm trying to take out strings from log files and add them to a csv. For example, in the directory now, there are 2 log files. I get the following results: sed -e '/custodian/b' -e '/packaged by/b' -e '/package name/b' -e '/Total Data (MB) Read/b' -e '/Begin Time/b' -e d * packaged by =... (10 Replies)
Discussion started by: chipperuga
10 Replies

7. Shell Programming and Scripting

Sed or Awk for modify hour in a crontab AIX

Hi, I want to modifiy the hour in the crontab AIX 5.3 for this line: Input: 00 22 * * * /outillage/script_exploit/bin/SavOffline.ksh > /dev/null 2>&1 Output: 30 20 * * * /outillage/script_exploit/bin/SavOffline.ksh > /dev/null 2>&1 With the awk or sed function through a ssh -q... (1 Reply)
Discussion started by: khalidou13
1 Replies

8. UNIX for Advanced & Expert Users

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... (4 Replies)
Discussion started by: mnnarendra
4 Replies

9. UNIX for Advanced & Expert Users

Modify A Program

hi all, I'm sorry for my english. I have this problem: I and my collegues can modify the same file at the same time. Is it possible delete this problem? The editor is VI. thanks. (4 Replies)
Discussion started by: tullo
4 Replies

10. UNIX for Dummies Questions & Answers

sed modify problem in script

I am having problems with the following "sed" command only when it is issued within a bash script. #!/bin/bash cat config.xml | sed -e 's/yes/no/g' > newconfig.xml When I enter this command from the command line it works like a charm, but when run in a script as shown it "zero's out" my... (2 Replies)
Discussion started by: darthur
2 Replies
Login or Register to Ask a Question