Hi,
Ive used unix.com to help learn the basics of AWK to format txt files however ive run out of talent and could do with some help. Im not sure if this is possible using awk but I have an input as follows
L73-10 342 0 1480
L73-10 342 100 1480
L73-10 342 250 1656
L73-10 342 500 1746
L73-10 342 750 1910
L73-10 350 0 1480
L73-10 350 100 1480
L73-10 350 250 1656
L73-11 300 0 1480
L73-11 300 100 1480
L73-11 300 250 1656
L73-11 300 500 1746
$1 is line, $2 is cdp, $3 is time, $4 is velocity
I need the script to print something like
L73-10/342:0-1480,100-1480,250-1656,500-1746,750-1910/350:0-1480,100-1480,250-1656/
L73-11/300:0-1480,100-1480,250-1656,500-1746/
The number of Time Velocity values is never constant between lines and cdps so im unsure if using something like below is the right way to go.
{NF==4
if (NR==1)
printf $1"/"$2":"$3"-"$4","
$2=cdp
{
if (NR>1||$2==cdp)
printf ORS=$3"-"$4","
else
$2!=cdp(NR=1)
next
{
if (NR==1)
$2=cdp
printf $1"/"$2":"$3"-"$4","
{
if (NR>1||$2==cdp)
printf ORS=$3"-"$4","
}
}
}
}
This gives an error saying cdp is not defined however I dont understand how. Do I need to create a loop to repeat the process to the end of the file or is an array more appropriate?
The above script also does not account for "line" variations. I have written something to print all line values to separate files so if needs be I can run a script on each file. Also it does not separate the boundary between new cdp's with a /.
Hopefully Ive got the point across ok. This level of problem is well beyond my ability and I have little idea of whether im using the correct approach of trying to reset NR when cdp changes. Any help would be greatly appreciated.
Thanks
Ryan
Use CODE tags when posting code, data or logs to enhance readability and to preserve formatting like indention etc., ty.