The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Tables and borders amatuer_lee_3 Shell Programming and Scripting 8 05-15-2008 01:57 PM
Converting tables of row data into columns of tables justthisguy Shell Programming and Scripting 7 07-16-2007 01:42 PM
Routing tables kingdbag UNIX for Dummies Questions & Answers 9 10-06-2006 05:01 PM
viewing tables itldp UNIX for Dummies Questions & Answers 2 12-17-2003 05:19 AM
PHP: list into tables perleo Shell Programming and Scripting 2 10-01-2003 06:50 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-23-2008
Registered User
 

Join Date: Apr 2008
Posts: 4
awk - list to tables

Hello there,

I have large tab-delimitated lists (see input file) and I need to convert it into tab-delimitated tables (see output file). I would like to do it with an awk script. Any ideas?

Input file:
header1 <tab> a <tab> a <tab> a <tab> a . . .
header2 <tab> b <tab> b <tab> b <tab> b . . .
header3 <tab> c <tab> c <tab> c <tab> c . . .
header4 <tab> d <tab> d <tab> d <tab> d . . .
. . .

Output file:
header1 <tab> header2 <tab> header3 <tab> header4 . . .
a <tab> b <tab> c <tab> d . . .
a <tab> b <tab> c <tab> d . . .
a <tab> b <tab> c <tab> d . . .
a <tab> b <tab> c <tab> d . . .
. . .
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-23-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,094
Somebody posted a matrix transform a few weeks back, search for that term? Not sure if it would work here but worth a shot.
Reply With Quote
  #3 (permalink)  
Old 04-23-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 500
Code:
awk '{
  for(f=1; f<=NF; f++)
      r2c[f","NR] = $f
} END {
  for(f=1; f<=NF; f++) {
    for(row=1; row<=NR; row++)
      printf("%s\t", r2c[f","row])
    print ""
  }
}' file
Reply With Quote
  #4 (permalink)  
Old 04-23-2008
Registered User
 

Join Date: Apr 2008
Posts: 4
Dear shamrock,

Thanks for the help - I have problems to execute the script.
I'm not sure it the problem is the $1 at the end?


./r2c.sh file.input

###########################

#!/bin/bash

awk '
{
for(f=1; f<=NF; f++)
r2c[f","NR] = $f
} END {
for(f=1; f<=NF; f++) {
for(row=1; row<=NR; row++)
printf("%s\t", r2c[f","row])
print ""
}
}' $1

#########################
Reply With Quote
  #5 (permalink)  
Old 04-23-2008
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 458
Quote:
Originally Posted by Lobaria View Post
I'm not sure it the problem is the $1 at the end?
Yep, that's the problem, where you declare $1?
Reply With Quote
  #6 (permalink)  
Old 04-23-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 500
Question Errors

Show the error you are getting.
Reply With Quote
  #7 (permalink)  
Old 04-23-2008
Registered User
 

Join Date: Apr 2008
Posts: 4
I tried your version of the script with the following ending:

...}' file

executing the script with: r2c.sh

---

and I tried the following ending:

...}' $1

executing the script with: r2c.sh file

But nothing happens.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:08 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0