I Am Stumped, Please Help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting I Am Stumped, Please Help
# 1  
Old 05-16-2008
I Am Stumped, Please Help

I have a CSV file that I am trying to parse with awk in a table. I think I am going about this the wrong way. I can't get the awk arrays to output with a tab between them in one line. They print out vertically and I need them to print out horizontally. WHAT AM I DOING WRONG?? I want to know if it's
possible to do in awk, because it will become a function in a bigger script if this ever works out.

csv input file:
Code:
175,5  ,0,2 ,4  ,0,1 ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,3
176,5  ,0,2 ,4  ,0,1 ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,3
177,14 ,0,2 ,7  ,0,2 ,11 ,0,2 ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,3
178,5  ,0,2 ,2  ,0,2 ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,3
179,5  ,0,2 ,4  ,0,1 ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,3
18 ,8  ,0,1 ,13 ,0,2 ,3  ,0,2 ,3  ,0,2 ,   , ,  ,   , ,  ,3  ,0,2 ,3  ,0,2 ,3  ,0,2 ,3  ,0,2 ,3
180,5  ,0,1 ,4  ,0,2 ,12 ,0,2 ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,3
181,1  ,0,2 ,4  ,0,2 ,12 ,0,2 ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,3
182,14 ,0,2 ,7  ,0,2 ,3  ,0,1 ,3  ,0,1 ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,3

script:
Code:
#!/usr/bin/nawk -f
BEGIN {
FS=";"
}
{
x[$2*1"-"$4*1]=x[$2*1"-"$4*1] " " $32 * 1000 + $1"-1"ORS;
x[$5*1"-"$7*1]=x[$5*1"-"$7*1] " " $32 * 1000 + $1"-2"ORS;
x[$8*1"-"$10*1]=x[$8*1"-"$10*1] " " $32 * 1000 + $1"-3"ORS;
x[$11*1"-"$13*1]=x[$11*1"-"$13*1] " " $32 * 1000 + $1"-4"ORS;
x[$14*1"-"$16*1]=x[$14*1"-"$16*1] " " $32 * 1000 + $1"-5"ORS;
x[$17*1"-"$19*1]=x[$17*1"-"$19*1] " " $32 * 1000 + $1"-6"ORS;
x[$20*1"-"$22*1]=x[$20*1"-"$22*1] " " $32 * 1000 + $1"-7"ORS;
x[$23*1"-"$25*1]=x[$23*1"-"$25*1] " " $32 * 1000 + $1"-8"ORS;
x[$26*1"-"$28*1]=x[$26*1"-"$28*1] " " $32 * 1000 + $1"-9"ORS;
x[$29*1"-"$31*1]=x[$29*1"-"$31*1] " " $32 * 1000 + $1"-10"ORS;
}
END {
for ( i in x )
printf "%-3s%-4s\n%-8s\n%-7s\n","UFC ",i,"--------",x[i]
}

output:

Code:
output:

UFC 5-2
--------
 3175-1
 3176-1
 3178-1
 3179-1

UFC 5-1
--------
 3180-1

UFC 14-2
--------
 3177-1
 3182-1

UFC 8-1
--------
 3018-1

UFC 1-2
--------
 3181-1

UFC 4-1
--------
 3175-2
 3176-2

desired output:

Code:
UFC 5-2     UFC 5-1   UFC 14-2
--------   --------   --------
 3175-1     3180-1     3177-1
 3176-1                3182-1
 3178-1
 3179-1

I hope you get the idea of the desired output, too hard to put everything in thread. I want the output to be in a table format, and sorted by UFC numbers from left to right.

Thanks in advance.
# 2  
Old 05-17-2008
Your script already formats the tables at the time it constucts them; the ORS variable evaluates to a newline (and changing it does not really solve anything, at least not trivially).

As a basic principle, you want something like (pseudocode)

Code:
separator=  # nothing
for table in tables
  print separator table.name
  separator=tab # or suitable number of spaces, or something
done
print "\n"

separator=  # nothing
for table in tables
  print separator "-" x (lenght table.name)   # print as many underlines as required
  separator=tab # or suitable number of spaces, or something
done
print "\n"

for i=0 to (however long the longest table is)
  separator=  # nothing
  for table in tables
    print separator
    if (table.value[i])
      table.value[i]
    else
      print ""   # print whitespace if we ran out of values for this table
    separator=tab # or suitable number of spaces, or something
  done
  print "\n"
done

As you can tell, this entails changing your data structure drastically, and possibly even changing to a different language. (Perl or Python would be easier than awk for this I think.)

A less drastic solution would be to output each table to a temporary file and use pr to print it in columnar format. On Linux, I use pr -bt 3 to tabulate into three columns; other platforms offer slightly different options.

Last edited by era; 05-17-2008 at 05:59 AM.. Reason: pr -bt3
# 3  
Old 05-18-2008
era,
Thanks soo much for the quick reply. Unfortunalely this post is a segment of a bigger awk script, that I hope to use as a awk function. Changing to a different language would be time prohibitive. I feel that the only thing that is wrong is the <TABS> between the defined awk arrays in this post that I can't seem to figure out. Does anyone know a way to ouput across a page and not down a page?? Is there another way to capture and sort/group the data like above?
# 4  
Old 05-18-2008
You can embed perl (or python) scripts in awk, just like you can call any other command line utility. They might be hard to read if they get really big. But that is another issue.
# 5  
Old 05-19-2008
Quote:
Originally Posted by timj123
I have a CSV file that I am trying to parse with awk in a table. I think I am going about this the wrong way. I can't get the awk arrays to output with a tab between them in one line. They print out vertically and I need them to print out horizontally. WHAT AM I DOING WRONG?? I want to know if it's
possible to do in awk, because it will become a function in a bigger script if this ever works out.

csv input file:
Code:
175,5  ,0,2 ,4  ,0,1 ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,3
176,5  ,0,2 ,4  ,0,1 ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,3
177,14 ,0,2 ,7  ,0,2 ,11 ,0,2 ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,3
178,5  ,0,2 ,2  ,0,2 ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,3
179,5  ,0,2 ,4  ,0,1 ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,3
18 ,8  ,0,1 ,13 ,0,2 ,3  ,0,2 ,3  ,0,2 ,   , ,  ,   , ,  ,3  ,0,2 ,3  ,0,2 ,3  ,0,2 ,3  ,0,2 ,3
180,5  ,0,1 ,4  ,0,2 ,12 ,0,2 ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,3
181,1  ,0,2 ,4  ,0,2 ,12 ,0,2 ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,3
182,14 ,0,2 ,7  ,0,2 ,3  ,0,1 ,3  ,0,1 ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,   , ,  ,3

script:
Code:
#!/usr/bin/nawk -f
BEGIN {
FS=";"
}
{
x[$2*1"-"$4*1]=x[$2*1"-"$4*1] " " $32 * 1000 + $1"-1"ORS;
x[$5*1"-"$7*1]=x[$5*1"-"$7*1] " " $32 * 1000 + $1"-2"ORS;
x[$8*1"-"$10*1]=x[$8*1"-"$10*1] " " $32 * 1000 + $1"-3"ORS;
x[$11*1"-"$13*1]=x[$11*1"-"$13*1] " " $32 * 1000 + $1"-4"ORS;
x[$14*1"-"$16*1]=x[$14*1"-"$16*1] " " $32 * 1000 + $1"-5"ORS;
x[$17*1"-"$19*1]=x[$17*1"-"$19*1] " " $32 * 1000 + $1"-6"ORS;
x[$20*1"-"$22*1]=x[$20*1"-"$22*1] " " $32 * 1000 + $1"-7"ORS;
x[$23*1"-"$25*1]=x[$23*1"-"$25*1] " " $32 * 1000 + $1"-8"ORS;
x[$26*1"-"$28*1]=x[$26*1"-"$28*1] " " $32 * 1000 + $1"-9"ORS;
x[$29*1"-"$31*1]=x[$29*1"-"$31*1] " " $32 * 1000 + $1"-10"ORS;
}
END {
for ( i in x )
printf "%-3s%-4s\n%-8s\n%-7s\n","UFC ",i,"--------",x[i]
}

output:

Code:
output:

UFC 5-2
--------
 3175-1
 3176-1
 3178-1
 3179-1

UFC 5-1
--------
 3180-1

UFC 14-2
--------
 3177-1
 3182-1

UFC 8-1
--------
 3018-1

UFC 1-2
--------
 3181-1

UFC 4-1
--------
 3175-2
 3176-2

desired output:

Code:
UFC 5-2     UFC 5-1   UFC 14-2
--------   --------   --------
 3175-1     3180-1     3177-1
 3176-1                3182-1
 3178-1
 3179-1

I hope you get the idea of the desired output, too hard to put everything in thread. I want the output to be in a table format, and sorted by UFC numbers from left to right.

Thanks in advance.
I think the trick is related to ORS. I remember I had a similar case when I had to put the output horizontally.
As far as I remember it was something like that

............................{ORS=","}{print variable}

for all the lines I applied this rule
# 6  
Old 05-19-2008
Thanks elthox, this will take me in a new direction that I think I can work with. I don't know if I need to start a new thread but there are 2 more issues that I need help with.

1) What would be the best way to sort the arrays? i.e. (UFC 1-1 UFC 1-2, UFC 2-1, etc.) as they output?

2) This script output a "null" array, basically it creates a all others that I don't want it to output. Basically all the null fields in the CSV file get bunch together. How do I get rid of that?

example:

UFC 0-0 #<- This should not be printed
--------
3001-3 3001-4 3001-5
# 7  
Old 05-19-2008
awkologist,
I figured out number 2 by changing the END statement from:
Code:
END {
for ( i in x )
printf "%-3s%-4s\n%-8s\n%-7s\n","UFC ",i,"--------",x[i]
}

to:

Code:
END {
for ( i in x )
if ( i != "0-0" ) {
printf "\n%-3s%-4s\n%-8s\n%-7s\n","UFC ",i,"--------",x[i] }
}


I know I should have figured out sooner, but getting burnt out on this. If anyone has a quick down & dirty way to sort the arrays that would be great, I am pretty sure it can be done with for loops, but there might be a easier way.

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Stumped .. is this a command line arg?

I need a bit of explanation: LogFile=${LOGS_DIR}/${1}_$$ I know: - LOGS_DIR is an environment variable - $$ is the PID ... but what is ${1} ?? Is it another method to access a command line variable, or the job name? Thanks! Jon (3 Replies)
Discussion started by: jdorn001
3 Replies

2. Shell Programming and Scripting

Stumped on simple BASH Script

Hello All, First and foremost, if I have posted this question in the wrong forum/section, I apologize. Okay so here is my dilemma. I have written a BASH script that automatically restarts a tomcat on a given server. That part was simple enough. However, now I would like to not only restart... (14 Replies)
Discussion started by: UNM_Lobo
14 Replies

3. Shell Programming and Scripting

Command not found in shell script - stumped for 4 days

Hello, I like to begin with :wall:.. literally... It has been 4 days and I have no idea how to fix it. Environment - AIX 5.3 I wrote a script to call on ssh to log into another box via PKA to do something else. If I run the script on the terminal, it works 100%. If the SAP customised... (11 Replies)
Discussion started by: plonkagain
11 Replies

4. Shell Programming and Scripting

perl replace command, stumped!

Ok, I stole some code from a program that takess a hash of a password from PasswdMD5 and replaces it in the /etc/shadown file on a linux system. I run his program and it's fine. Well I took the same code and put it in another program that won't ask for prompgx and such and this code won't work:... (2 Replies)
Discussion started by: benefactr
2 Replies

5. Shell Programming and Scripting

Stumped

Hi, I'm pretty new to UNIX shell scripting and need some help. We have an Informatica interface that dumps any files that have errors into a directory. I need to check that directory for any of up to 9 files that might be in it and run a specific process for each file found. Here's what I... (3 Replies)
Discussion started by: JeffR
3 Replies

6. UNIX for Dummies Questions & Answers

RegEx question has me stumped

Hi All, I'm fairly new to Regular Expressions, and have made some decent progress, but this one has me scratching my head. I'm trying to match the class name in my scripts as shown in the examples below. Any ideas? Thanks! -Mark :: Looking for a regex to match the red text :: import... (7 Replies)
Discussion started by: tolmark
7 Replies

7. IP Networking

httpd.conf - stumped

Have been asked to remove all images from being logged to the access_log ... where am I going wrong?<VirtualHost 123.456.789.99> ServerName www.somedomain.com.au DocumentRoot /agents/tts Redirect /wap http://somewap.com.au/traveler LogFormat "%v %h %l %u %t \"%r\" %>s %b" comonvhost... (2 Replies)
Discussion started by: Cameron
2 Replies
Login or Register to Ask a Question