AWK Programming Problem


 
Thread Tools Search this Thread
Top Forums Programming AWK Programming Problem
# 1  
Old 02-16-2011
AWK Programming Problem

I need development help with a AWK Program.

1
2
3 THIS IS THE INPUT FILE – EACH EMPLOYEE MAY HAVE FROM 1 TO 30 RECORDS
4 INPUT
5 1-4 Project
6 5-12 Employee
7 13-14 Sequence (MAYBE FROM 1 TO 30)
8 15-21 Work Code
9 22-22 Pri Code
10
11 9991|80100001|1|0389999|1|
12 9991|80100001|2|7070007|2|
13 9991|80100001|3|4912100|1|
14 9991|80100001|4|5990000|2|
15 9991|80100001|5|2500200|1|
16 9991|80100001|6|41401|2|
17 9991|80100001|7|515|1|
18 9991|80100001|8|4019|2|
19 9991|80100001|9|56210|1|
20
21 THIS IS THE OUTPUT (OPTION ONE)
22 ( NEED ONE CONTIGUOUS RECORD PER EMPLOYEE )
23 OUTPUT = project,employer, workcode,workcode,...,pricode, pricode,...
24 1586|80100001|0389999|7070007|4912100|599000|25002 00|41401|515|4019|56210|1|2|1|2|1|2|1|2|1|
25
26
27 THIS IS THE OUTPUT (OPTION TWO)
28 ( NEED ONE CONTIGUOUS RECORD PER EMPLOYEE WITH OUTPUT CONTAINING ROOM FOR 30 WORK CODES AND 30 PRI CODES )
29
30 1586|80100001|0389999|7070007|4912100|599000|25002 00|41401|515|4019|56210||||||||||||||||||||||1|2|1 |2|1|2|1|2|1||||||||||||||||||||||
31
thank for the help.




***
Please do not embed formatting of font/color/etc... when posting.


Input file:
Code:
9991|80100001|1|0389999|1|
9991|80100001|2|7070007|2|
9991|80100001|3|4912100|1|
9991|80100001|4|5990000|2|
9991|80100001|5|2500200|1|
9991|80100001|6|41401|2|
9991|80100001|7|515|1|
9991|80100001|8|4019|2|
9991|80100001|9|56210|1|

Output 1:
Code:
1586|80100001|0389999|7070007|4912100|599000|25002 00|41401|515|4019|56210|1|2|1|2|1|2|1|2|1|

Output 2:
Code:
1586|80100001|0389999|7070007|4912100|599000|25002 00|41401|515|4019|56210||||||||||||||||||||||1|2|1 |2|1|2|1|2|1||||||||||||||||||||||


Last edited by joeyg; 02-16-2011 at 09:53 AM.. Reason: Pulled out input & output files and wrapped with CodeTags
# 2  
Old 02-16-2011
Can One employee work at the same time on 2 projects ?
# 3  
Old 02-16-2011
Yes they can.

---------- Post updated at 08:48 AM ---------- Previous update was at 08:33 AM ----------

They must be in order by their sequence number.
# 4  
Old 02-16-2011
Which fields is the primary key made of ?
# 5  
Old 02-16-2011
Primary key is made up of the project and employee fields.
# 6  
Old 02-16-2011
Try this:
Code:
awk -F\| 'p!=$1 OFS $2{if(s)print s t; t=x; s=p=$1 OFS $2}{s=s OFS $4; t=t OFS $5}END{print s t}' OFS=\| infile

If the records are not sorted, then you can sort first:
Code:
sort -t\| -k1,3n infile | 
awk -F\| 'p!=$1 OFS $2{if(s)print s t; t=x; s=p=$1 OFS $2}{s=s OFS $4; t=t OFS $5}END{print s t}' OFS=\|


Last edited by Scrutinizer; 02-16-2011 at 01:26 PM..
# 7  
Old 02-16-2011
@Scruti

Ok now i am trying to understand ... Smilie

1) is s=p=$1 equivalent to p=$1; s=p
or is it equivalent to s=p ; p=$1 ?

2) I don't get why the p is mandatory : i have tried to run the following :

Code:
# nawk -F\| 's!=$1 OFS $2{if(s)print s t; t=x; s=$1 OFS $2}{s=s OFS $4; t=t OFS $5}END{print s t}' OFS=\| infile
9991|80100001|0389999|1
9991|80100001|7070007|2
9991|80100001|4912100|1
9991|80100001|5990000|2
9991|80100001|2500200|1
9991|80100001|41401|2
9991|80100001|515|1
9991|80100001|4019|2
9991|80100001|56210|1

... but i get a multi line answer instead of 1 line answer ...
what do i miss ?

Thx in advance for your lights

--- The still awk Noobzor, but keeping up trying Smilie ---
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk programming -Passing variable to awk for loop

Hi All, I am new to AWK programming. I have the following for loop in my awk program. cat printhtml.awk: BEGIN -------- <some code here> END{ ----------<some code here> for(N=0; N<H; N++) { for(M=5; M<D; M++) print "\t" D ""; } ----- } ... (2 Replies)
Discussion started by: ctrld
2 Replies

2. Programming

Gcc openmp programming problem

Dear Linux users, I'm a noob at openmp, gcc and c programming. I can run my own openmp code in terminal with no problem, eg. gcc -fopenmp program.c -o program. But now I'm trying to compile and run another person's code, it contains a makefile and multiple .c and .h files. I don't know how to... (2 Replies)
Discussion started by: pigeon151
2 Replies

3. Shell Programming and Scripting

AWK programming

Hi All, I read the AWK manual in the MAN page. But i didn't understand the below piece of code in the script TABLE=`echo "${FILE}" | awk -F"/" '{print $NF}' | cut -d"." -f1 | awk -F"_" '{print $NF}' 2>> ${LOGFILE}`; Please explain the above code. Thanks in advance ....... Regards,... (4 Replies)
Discussion started by: pdathu
4 Replies

4. Programming

Programming problem

Hi Everybody. I have an Ubuntu linux 10.04 os and want be able to use the website of intuit but it only runs on unix. Is there any way that I can put the needed drivers in this os since both linux/unix share some distinctive programs. If this is not possible how can I save the information that I... (1 Reply)
Discussion started by: mk631219
1 Replies

5. Shell Programming and Scripting

awk programming

Hi I have a multi -line file which is sorted by the 1-st colomn in the following format: 400 0000 0001 1000 1010 0111 0000 1000 0000 402 1101 0000 1100 1010 0111 1000 1000 0000 403 1001 0000 1100 1010 0111 0000 1000 0000 495 1000 0000 1100 ... (4 Replies)
Discussion started by: aoussenko
4 Replies

6. Shell Programming and Scripting

Problem with awk programming

i want to calculate power of 10 as follows: coef=2.0588 ra1=$(awk 'BEGIN{print 10^$coef}') it gives a result 1 when, if i calculate ra1=$(awk 'BEGIN{print 10^2.0588}') it gives a result 114.499 what should i do? (2 Replies)
Discussion started by: SANGUINE
2 Replies

7. UNIX for Advanced & Expert Users

RS232 programming problem

Hi all I encountered a strange phenomenon when reading / writing to RS232 serial device (on my machine /dev/ttyS0) I have simple 2 processes: 1) process which WRITE characters from /dev/ttyS0 For example write the characters... (2 Replies)
Discussion started by: dudi.forum
2 Replies

8. UNIX for Dummies Questions & Answers

awk (?) programming

Hello i need help with following problem: i need to update a file containing records in following format: student1 classa student2 classb student3 student4 classc i need to associate EACH student with a class in my output file ... so for students 3 and 4, i need to create a... (12 Replies)
Discussion started by: alrinno
12 Replies

9. Programming

C programming + problem with char arrays

Im trying to write some code atm which gets the complete pathname of a folder and strips off references to the parent folders. The end result should be just the name of the folder. Currently Im able to extract the folder name, however Im getting junk added onto the name as well which is making... (7 Replies)
Discussion started by: JamesGoh
7 Replies

10. Shell Programming and Scripting

awk programming

Hi folks, I would like to know the difference between shell programming and awk programming. Actually i have developed a few applications in both but i could not find a better difference other than the syntax differences. For example, the awk programming syntax is complicated. It has "{" and... (4 Replies)
Discussion started by: Nisha
4 Replies
Login or Register to Ask a Question