UNIX awk help


 
Thread Tools Search this Thread
# 1  
UNIX awk help

I am new to UNIX and I need a sample awk or sed program to solve the following problem.

I have a file with the following format. The file is comma delimited. Each record has header information followed by looping detailed information. In addition the detailed information will vary based on a value found in the header information. For example if the header information has a value of 48 then the detail will have 48 sets of detail information. A set of detailed information is a set of 3 fields. The other value is 24, so the detail will either have 48 sets or 24 sets of detail.

What I want to do is replace the delimiter after the value 48 or 24 and around each triplete of information. I highligted the areas in bold where I want to change the comma to a semi colon.

Sample of current record:
MEPMD01,19970819,Sensus,SDGE,SDGE,,200705221435,1888961,OK,E,KWH,1,00000015,48,200705210715,R 00 40,2.0000,200705210730,R 00 40,1.0000,200705210745,R 00 40,2.0000,200705210800,R 00 40,1.0000,200705210815,R 00 40,2.0000,200705210830,R 00 40,2.0000,200705210845,R 00 40,1.0000,200705210900,R 00 40,2.0000,200705210915,R 00 40,2.0000,200705210930,R 00 40,1.0000,200705210945,R 00 40,2.0000,200705211000,R 00 40,1.0000,200705211015,R 00 40,2.0000,200705211030,R 00 40,2.0000,200705211045,R 00 40,1.0000,200705211100,R 00 40,2.0000,200705211115,R 00 40,2.0000,200705211130,R 00 40,1.0000,200705211145,R 00 40,2.0000,200705211200,R 00 40,1.0000,200705211215,R 00 40,2.0000,200705211230,R 00 40,2.0000,200705211245,R 00 40,1.0000,200705211300,R 00 40,2.0000,200705211315,R 00 40,2.0000,200705211330,R 00 40,1.0000,200705211345,R 00 40,2.0000,200705211400,R 00 40,1.0000,200705211415,R 00 40,1.0000,200705211430,R 00 40,2.0000,200705211445,R 00 40,1.0000,200705211500,R 00 40,1.0000,200705211515,R 00 40,1.0000,200705211530,R 00 40,1.0000,200705211545,R 00 40,1.0000,200705211600,R 00 40,1.0000,200705211615,R 00 40,1.0000,200705211630,R 00 40,1.0000,200705211645,R 00 40,1.0000,200705211700,R 00 40,1.0000,200705211715,R 00 40,1.0000,200705211730,R 00 40,1.0000,200705211745,R 00 40,1.0000,200705211800,R 00 40,1.0000,200705211815,R 00 40,1.0000,200705211830,R 00 40,1.0000,200705211845,R 00 40,1.0000,200705211900,R 00 40,1.0000

Roy Ayala
# 2  
Try...
Code:
awk -F, '{for(i=1;i<NF;i++) printf $i (i>13&&i%3==2?";":",");print $NF}' file1

Tested...
MEPMD01,19970819,Sensus,SDGE,SDGE,,200705221435,1888961,OK,E,KWH,1,00000015,48;200705210715,R 00 40,2.0000;200705210730,R 00 40,1.0000;200705210745,R 00 40,2.0000;200705210800,R 00 40,1.0000;200705210815,R 00 40,2.0000;200705210830,R 00 40,2.0000;200705210845,R 00 40,1.0000;200705210900,R 00 40,2.0000;200705210915,R 00 40,2.0000;200705210930,R 00 40,1.0000;200705210945,R 00 40,2.0000;200705211000,R 00 40,1.0000;200705211015,R 00 40,2.0000;200705211030,R 00 40,2.0000;200705211045,R 00 40,1.0000;200705211100,R 00 40,2.0000;200705211115,R 00 40,2.0000;200705211130,R 00 40,1.0000;200705211145,R 00 40,2.0000;200705211200,R 00 40,1.0000;200705211215,R 00 40,2.0000;200705211230,R 00 40,2.0000;200705211245,R 00 40,1.0000;200705211300,R 00 40,2.0000;200705211315,R 00 40,2.0000;200705211330,R 00 40,1.0000;200705211345,R 00 40,2.0000;200705211400,R 00 40,1.0000;200705211415,R 00 40,1.0000;200705211430,R 00 40,2.0000;200705211445,R 00 40,1.0000;200705211500,R 00 40,1.0000;200705211515,R 00 40,1.0000;200705211530,R 00 40,1.0000;200705211545,R 00 40,1.0000;200705211600,R 00 40,1.0000;200705211615,R 00 40,1.0000;200705211630,R 00 40,1.0000;200705211645,R 00 40,1.0000;200705211700,R 00 40,1.0000;200705211715,R 00 40,1.0000;200705211730,R 00 40,1.0000;200705211745,R 00 40,1.0000;200705211800,R 00 40,1.0000;200705211815,R 00 40,1.0000;200705211830,R 00 40,1.0000;200705211845,R 00 40,1.0000;200705211900,R 00 40,1.0000
# 3  
awk code

Ygor,

Fantatic, this code worked!!! I appreciate your help. Now I will try and figure out the code.
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #931
Difficulty: Medium
64-bit computer system storage allocation for timekeeping will allow them to represent dates more than 300 billion years into the future.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

UNIX inquiry for 'awk'

Hello Everyone, May I ask for your help regarding one of the UNIX command “awk”. So I executed a script and the output looks like this (see below): output.txt CONTRACTNAME ... (3 Replies)
Discussion started by: steven_huskie
3 Replies

2. AIX

awk UNIX

friends because I get this error when calling awk awk: 0602-546 Can not find or open file estdistica.awk. (10 Replies)
Discussion started by: tricampeon81
10 Replies

3. UNIX for Advanced & Expert Users

awk sub command in Unix

Hi, I am new to unix shell script and I have some trouble on the awk sub I would like to pick the Date "July 10 2012" into $corr_date by using sub() function, but it is not successful. The inputted text file: pic.*.txt July 10 2012 20:30:50 , 1234567.jpg July 10 2012 20:30:52 , 5648978.jpg... (1 Reply)
Discussion started by: meroko
1 Replies

4. Shell Programming and Scripting

Help in unix in awk

Hi, I have a file as given below $cat int_check 239|adf123@yahoo.com|999e 23e|get@rediff.com|987 233|get@rediff.com|987 I am running the command as given to find whether field 1 and 3 have proper integer values or not. awk -F'|' 'int($1)!=$1 || int($3)!=$3' int_check... (7 Replies)
Discussion started by: ashwin3086
7 Replies

5. UNIX for Dummies Questions & Answers

AWK Help (New to UNIX)

Write a script to calculate the average score and assign a letter grade for each student, also compute the average score for every test (the source data is in the file “grades”). Output all students’ name, scores, and grades in descending order of the average score. Note: please save the output to... (17 Replies)
Discussion started by: dsptl
17 Replies

6. Shell Programming and Scripting

need help with UNIX and awk

Hi, I am working on a script where i am fetching a word from a parameter file and using it as output file name . here is the part of parameter file contents : it basically contains SQL statements. Select 'SSFNRTP' as TABLE_NAME,UCSTDT,CVOFIDKEY from (Select 'cofvof_interface' as... (2 Replies)
Discussion started by: manmeet
2 Replies

7. Shell Programming and Scripting

unix awk scripting

Hi can anyone pls help me out in knowing how a array of elements which are fetched from database is passed to the awk program. Something like this #!/bin/sh AMT=`sqlplus -s usrname/pswd <<EOD set pagesize 0 feedback off verify off heading off echo on select amount from... (11 Replies)
Discussion started by: shaik
11 Replies

8. Shell Programming and Scripting

help Unix and Awk scripts

consider a file with the following format: a header row with colon-separated field names, and data rows with colon-separated values. for example: AGE:EYES:HAIR 32:BLUE:BLONDE 54:BROWN:BROWN write a script, named extract_columns, that prints out the... (4 Replies)
Discussion started by: ravirdprasad
4 Replies

9. Shell Programming and Scripting

Unix To Awk

How do I alias the FILE name into awk? gawk -f awk_script FILE or FILE | gawk awk_script for FILE in `find ./* -prune -type f -newer ./TIMEFILE` do # Need to have the actual FILE name ran thru an awk script done (1 Reply)
Discussion started by: goodmis
1 Replies

10. Shell Programming and Scripting

How to use unix commands in awk

Hi All, I try to use the unix commands inside awk and couldnt succeed . Please help me how to achieve it. Regards, Sukumar. (7 Replies)
Discussion started by: jerome Sukumar
7 Replies

Featured Tech Videos