AWK Programming Problem


 
Thread Tools Search this Thread
Top Forums Programming AWK Programming Problem
# 8  
Old 02-16-2011
Hi ctsgnb,

s=p=$1 OFS $2 is the same as p=s=$1 OFS $2. Afterwards both s and p are equal to $1 OFS $2, after the first line this is "9991|90100001"

But where p stays the same, s gets appended with $4's and goes on to grow into the first part of the line to be printed. The second part is t which gets appended with the $5's.

Your version would have worked if you used s!~"^"$1 OFS $2instead of s!=$1 OFS $2

Last edited by Scrutinizer; 02-16-2011 at 04:03 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 9  
Old 02-16-2011
" s=p=$1 OFS $2 is the same as s=p=$1 OFS $2" really ??? Smilie

With the s=p=$1 i was just wondering if the affectation sequence was processed from right to left order but ok, this is the case so it just equivalent to p=$1 ; s=p so at the end s and p have the same value which in fact quite intuitive regarding to the notation, but i wasn't sure.

Because if the affectation would have been processed from left to right , this would have meant a rotation so that it could have been used for shifting field to the left.

But , ok i got it now ... Thx
# 10  
Old 02-16-2011
Oops.. corrected it.. to p=s...Smilie
# 11  
Old 02-17-2011
Scrutinizer / ctsgnb

Thx you for responding to my request. I'm a novice awk programmer.
If you have the time, could you explain how your solution to the request works? Also how would option two work? Where the output needs 30 work code fields for every employee? If the input record has only ten valid work code fields. but I need to output the ten valid work code fields plus twenty more so that this employee has thirty work code fields in the output file. The invalid fields would be blank.
Again thx you for your assistance.
# 12  
Old 02-17-2011
Hi Gator, you're welcome. Here is an explanation:
-F\pipe-symbolUse pipe-symbol as field separator
'p!=$1 OFS $2 if variable p (previous key) is not equal to the current key ($1 OFS $2, i.e. field1 pipe-symbol field2, OFS is the output field separator which is set to the pipe-symbol )
{if(s)print s t if string s exists then print s concatenated with string t
t=xt=""; t becomes an empty string
s=p=$1 OFS $2}p becomes the new key and s becomes equal to p
{s=s OFS $4append OFS with field4 to string s
t=t OFS $5}append OFS with field5 to string t
END{print s t}'After all lines have been processed, print s concatenated with string t
OFS=\pipe-symbolset OFS to pipe-symbol

As for option 2, try this:
Code:
awk -F\| 'function pr(){$0=s;NF+=30;$0=$0 t;NF+=30;print} p!=$1 OFS $2{if(s)pr(); t=x; s=p=$1 OFS $2}{s=s OFS $4; t=t OFS $5} END{pr()}' OFS=\| infile

# 13  
Old 02-23-2011
Scrutinizer,

THX again for your help. I've been out sick for the past days.
I'll try your solution for option 2, and if I have any questions I will get back to you.
THX
# 14  
Old 03-02-2011
HI Scrutinizer,


THIS IS THE INPUT FILE

1-4 PROJECT
5-12 EMPLOYEE
13-14 SEQUENCE NO (MAYBE FROM 1 TO 30)
15-21 WORK CODE (MAYBE FROM 1 TO 30)
22-22 PRI CODE (MAYBE FROM 1 TO 30)


1586
1586
1586
1586
1586
1586
1586
1586
1586
1586
20

THIS IS THE REQUIRED OUTPUT
SORTED BY PROJECT, EMPLOYEE, AND SEQUENCE NO
CONTROL BREAK ON PROJECT AND EMPLOYEE
IN ORDER BY THE SEQ NO THE WORK CODE SHOULD HAVE 30 FILEDS 10 FILLED AND 20 EMPTY
NEXT THE PRI CODE SHOULD ALSO HAVE 30 FIELDS 10 FILLED AND 20 EMPTY


PHP Code:
1586|80100001|0389|70707|49121|5990|25002|41401|515|4019|56210|56999|||||||||||||||||||||1|2|1|2|1|1|2|2|2|1||||||||||||||||
    
29  ||||| 


YOUR CODE

PHP Code:
    34  awk -F\| 'function pr() {$0=s;NF+=30;$0-$0 t;NF+=30;print} p!=$1 OFS $2{if(s)pr(); t=x; s=p=$1 OFS $2} {s=s OFS $4; t=t OFS $5} END{p
r() }' 
OFS=\| infile 


YOUR CODE RETURN THE FOLLOWING RESULTS
PHP Code:
1586|80100001|0389|70707|49121|5990|25002|41401|515|4019|56210|56999|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 

What changes do I need to make to get this to work?

THX

---------- Post updated at 09:42 AM ---------- Previous update was at 09:38 AM ----------

Hi, Scrutinizer

THIS IS THE INPUT FILE

PROJECT
5-12 EMPLOYEE
13-14 SEQUENCE NO (MAYBE FROM 1 TO 30)
15-21 WORK CODE (MAYBE FROM 1 TO 30)
22-22 PRI CODE (MAYBE FROM 1 TO 30)


PHP Code:
1586|80100001|1|0389|1|
1586|80100001|2|70707|2|
1586|80100001|3|49121|1|
1586|80100001|4|5990|2|
1586|80100001|5|25002|1|
1586|80100001|6|41401|1|
1586|80100001|7|515|2|
1586|80100001|8|4019|2|
1586|80100001|9|56210|2|
1586|80100001|10|56999|1


THX again,
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