awk - list to tables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - list to tables
# 1  
Old 04-23-2008
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 . . .
. . .
# 2  
Old 04-23-2008
Somebody posted a matrix transform a few weeks back, search for that term? Not sure if it would work here but worth a shot.
# 3  
Old 04-23-2008
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

# 4  
Old 04-23-2008
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

#########################
# 5  
Old 04-23-2008
Quote:
Originally Posted by Lobaria
I'm not sure it the problem is the $1 at the end?
Yep, that's the problem, where you declare $1? Smilie
# 6  
Old 04-23-2008
Question Errors

Show the error you are getting.
# 7  
Old 04-23-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare with 2 tables

I have 3 file inputs, file1 20160302|5485368299953|96|510101223440252|USA|5485368299953|6|800|2300|0 20160530|5481379883742|7|510101242850814|USA|5481379883742|5|540|2181|1500 20160513|5481279653404|24|510100412142433|INDIA|5481279653404|3|380|1900|0... (1 Reply)
Discussion started by: radius
1 Replies

2. Shell Programming and Scripting

How to find the X highest values in a list depending on the values of another list with bash/awk?

Hi everyone, This is an exemple of inpout.txt file (a "," delimited text file which can be open as csv file): ID, Code, Value, Store SP|01, AABBCDE, 15, 3 SP|01, AABBCDE, 14, 2 SP|01, AABBCDF, 13, 2 SP|01, AABBCDE, 16, 3 SP|02, AABBCED, 15, 2 SP|01, AABBCDF, 12, 3 SP|01, AABBCDD,... (1 Reply)
Discussion started by: jeremy589
1 Replies

3. Shell Programming and Scripting

Selecting tables

Hi, Can anyone help me that, How to see the table fields in Oracle database through shell script(ksh). I have tried with the following: sqlplus -s $user/$passwd@$sid << SQL >> session.log select * from Testtab.sql I'm not able to see anything.. Thanks (4 Replies)
Discussion started by: zxcjggu708
4 Replies

4. Shell Programming and Scripting

Display tables of 6

I'm new to linux and unix I would like to know how to display tables of 6 line after line Output should be 1*6 = 6 2*6 = 12 3*6 = 18 4*6 = 24 etc I can display line by line but not continuously Any suggestion (3 Replies)
Discussion started by: electricair
3 Replies

5. Shell Programming and Scripting

awk to create two HTML Tables

I am working on awk script to generate an HTML format output. With input file as below I am able to generate a HTML file however I want to saperate spare devices in a different table than rest of the devices and which has only Bunch ID, RAW Size and "Bunch Spare" status columns. INPUT File : ... (2 Replies)
Discussion started by: dynamax
2 Replies

6. UNIX for Dummies Questions & Answers

VI editor and tables

I have a host table that looks similar like this: # IP Name Comment 10.10.10.1 Lab1-1 # Static for Lab1 subnet 10.10.10.2 Lab1-2 # Static for Lab1 subnet 10.10.10.3 Lab1-3 ... (2 Replies)
Discussion started by: mjl927
2 Replies

7. Shell Programming and Scripting

Converting tables of row data into columns of tables

I am trying to transpose tables listed in the format into format. Any help would be greatly appreciated. Input: test_data_1 1 2 90% 4 3 91% 5 4 90% 6 5 90% 9 6 90% test_data_2 3 5 92% 5 4 92% 7 3 93% 9 2 92% 1 1 92% ... Output:... (7 Replies)
Discussion started by: justthisguy
7 Replies

8. Shell Programming and Scripting

PHP: list into tables

how would i go about having a index.php page in a folder, in the folder several files named greece.rar and france.rar and other countrys. could php get a list of these, put the names into a table, listed downwards, and the file-names clickable to open that file.? (2 Replies)
Discussion started by: perleo
2 Replies
Login or Register to Ask a Question