|
|
|
|
google site
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|||
|
help with ksh/awk/sed script, random # of fields
Hello all, I'm working on an attendance callout script for a school district. I need to change our current layout for the vendor. Currently the data is in the form of: studentid,period,building,Heres a sample of some made up records: 500,1,30,I need to reformat this, omitting the header row, to look like the following: 500,1,2,3,6,30,I've done stuff like this with awk in the past when the number of fields were a constant. Just not sure how to loop through this when the number of fields vary. The kids can have an unexcused in any number of periods throughout the day. Some of our buildings have over 9 periods configured for attendance purposes as well. If anyone has some code to get me going in the right direction that would be great. I will read the file first then do something like this to compare the current record with the next record to verify its the same kid. (beyond that I'm not sure how to proceed) Code:
awk -F, '
$1 == lastid {
do something
}
$1 != lastid {
lastid=$1
do something
}'Thanks, |
| Sponsored Links |
|
|
|
|||
|
Code:
awk -F, -v OFS=, '{ for (I=2; I<NF; I++)
{print $1, $I}
}' <<END |\
sort -kn1,1 -kn2,2 -t, -u |\
awk -F, 'NR==1{Save0=$1}
Save0 == $1{Line=Line "," $2; next}
Save0 != $1 {print Save0 Line;
Line="," $2;
Save0=$1;
}
END{print Save0 Line;}'
500,1,30,
500,2,30,
500,3,30,
500,6,30,
7899,2,31,
9021,1,33,
9021,6,33,
907711,5,40,
907711,6,40,Produced Code:
500,1,2,3,6,30 7899,2,31 9021,1,6,33 907711,5,6,40 |
|
|||
|
Quote:
thanks again |
|
|||
|
Ideally, I should have had the word END on a line by itself after the data. It shows the korn shell where to stop the input of data to the awk program.
Still the script terminated at that point, the shell script figures out the END is implied. Sorry, I will remember to put it in future posts. |
| Sponsored Links |
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| random script termination | mph | Shell Programming and Scripting | 6 | 07-18-2008 09:03 AM |
| how do i generate random integer using only shell script | sridhusha | Shell Programming and Scripting | 1 | 12-31-2007 03:46 AM |
| random function script | prash_b | Shell Programming and Scripting | 4 | 09-15-2006 05:54 AM |
| Random files do not FTP in the shell script | RLatham20 | Shell Programming and Scripting | 1 | 06-29-2006 01:07 PM |
| Random parameters passing in FTP script | sourabhshakya | Shell Programming and Scripting | 2 | 06-12-2006 12:20 PM |