Construct 3 column table from one column list


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Construct 3 column table from one column list
# 8  
Old 07-26-2011
how about this:

Code:
 
cat input_file | paste - - - -

You might need to do a bit of formating and may need to "cut" the company name from first line and append at the start of next line.

some thing like this:

Code:
 
cat input_file| paste - - - - | awk 'NR==1 {a=$1;$1="";print;next} {$0=a" "$0}1'

Need to do a bit formatting with tabs.
# 9  
Old 07-27-2011
that didn't quite to the trick I'm afraid.

I also can't modify the input file. It will look like this

company_name, registration_number
company_name, registration_number
company_name, registration_number
company_name, registration_number

I'm using this to access a website using cURL to download each company's turnover. After I sanitize the output from cURL i get a list like this. Comments are for illustration only here, not in actual output.

Code:
2010-12 # year 1
2009-12 # year 2
2008-12 # year 3
46.706 # turnover year 1
37.984  # turnover year 2
53.632 # turnover year 3

In my script I append the company name from the original input file as i don't want to get that from the web each time. Currently I add this to the top of each batch of 6 lines so the output looks like

Code:
Burgundy # Company name appended from input file
2010-12 # year 1
2009-12 # year 2
2008-12 # year 3
46.706 # turnover year 1
37.984  # turnover year 2
53.632 # turnover year 3

Now, I need to have all this in 4 columns like this
Code:
Burgundy year1 year2 year3
- to1 to2 to3

Your example gave an output like this

10.5322
2.0842 0

so no year information is there...
# 10  
Old 07-27-2011
Code:
 
/home/TESTBOX>cat input_file
Burgundy
2010-12
2009-12
2008-12
46.706
37.984
53.632
/home/TESTBOX>cat input_file  | paste - - - -
Burgundy        2010-12         2009-12         2008-12
46.706  37.984  53.632
 
/home/TESTBOX>cat input_file  | paste - - - - | awk 'NR>1{$1="-\t"$1}1'
Burgundy        2010-12         2009-12         2008-12
-       46.706 37.984 53.632

Any thing else?
This User Gave Thanks to panyam For This Post:
# 11  
Old 07-27-2011
Thanks for your help! The problem was that the output had DOS newlines and I had to take care of the trailing and leading spaces. So the paste didn't work. Once converted it works like a charm. The full, ugly script now looks like
Code:
#!/bin/sh

INPUTFILE=$1
OUTPUTFILE=$2

awk '{print $1, $2}' $INPUTFILE | while read BNAME BNUMBER
touch $OUTPUTFILE
do
URL="http://allabolag.se?what=$BNUMBER"
echo $BNAME >> $OUTPUTFILEcurl -s $URL |  grep bokTable | sed -e 's/<[^>]*>//g' | awk 'NR==1,NR==9' | awk 'NR!=4 && NR!=5 && NR!=6' | awk '{
gsub(/^[ \t]+|[ \t]+$/,"");print}' | sed 's/\ /./g' |sed 's/.$//' | paste - - - >> $OUTPUTFILE
#echo "--"
done

# 12  
Old 08-05-2011
Code:
#!/bin/ksh                                           
start="Y"                                            
while read comp                                      
do                                                   
        read year1                                   
        read year2                                   
        read year3                                   
        if [ start = "Y" ]                           
        then                                         
                echo Company $year1 $year2 $year3  
                start="N"                            
                                                     
        fi                                           
        read a b                                     
        read a1 b1                                   
        read a2 b2                                   
        echo $comp $a $b $a1 $b1 $a2 $b2             
done

If the two -- characters between the sets are actually part of the data, either remove them first, or add another read statement before the done statement.

I didn't see all the solutions on page 2 until I finished my post. -Smilie
This User Gave Thanks to jgt For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split column data if the table has n number of column's with some record

Split column data if the table has n number of column's with some record then how to split n number of colmn's line by line with records Table --------- Col1 col2 col3 col4 ....................col20 1 2 3 4 .................... 20 a b c d .................... v ... (11 Replies)
Discussion started by: Priti2277
11 Replies

2. Shell Programming and Scripting

Split column data if the table has n number of column's

please write a shell script Table -------------------------- 1 2 3 a b c 3 4 5 c d e 7 8 9 f g h Output should be like this --------------- 1 2 3 3 4 5 7 8 9 a b c c d e f g h (1 Reply)
Discussion started by: Priti2277
1 Replies

3. UNIX for Dummies Questions & Answers

Creating a two column list of date pairs form a single column list

Hi all, looking for some help here. I'm what you'd call a dirty programmer. my shell scripts might be ugly, but they (usually) function... Say I have a single column text file with a list of dates (yyyymmdd) that represent the elevation of a point on that date (I work with land subsidence, so... (2 Replies)
Discussion started by: jbrandt1979
2 Replies

4. Shell Programming and Scripting

Difference of the same column when two other column matches and one column differs less than 1 hour

This is my input file : # cat list 20130430121600, cucm, location,76,2 20130430121600,cucm1,location1,76,4 20130430122000,cucm,location,80,8 20130430122000,cucm1,location1,90,8 20130430140000,cucm1,location1,87,11 20130430140000, cucm,location,67,9 This is the required output ... (1 Reply)
Discussion started by: Lakshmikumari
1 Replies

5. Shell Programming and Scripting

From column to table

Hi, I have an ascii file containing information. This file is n x m lines. Is there a way to generate an ascii file where the information is stored as n x m table instead? The m columns should be tab separated. Thanks a lot, Sarah (11 Replies)
Discussion started by: f_o_555
11 Replies

6. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

7. UNIX for Advanced & Expert Users

Data from table to column

Hi All, in bash I have a text file which is something like 7.96634E-07 1.0000 0.00000E+00 0.0000 0.00000E+00 0.0000 1.59327E-06 0.7071 2.23058E-05 0.1890 6.61207E-05 0.1098 1.13919E-04 0.0865 1.47377E-04 0.0747 .... .... 0.00000E+00 0.0000 0.00000E+00 0.0000 ... (6 Replies)
Discussion started by: f_o_555
6 Replies

8. UNIX for Dummies Questions & Answers

average of a column in a table

Hello, Is there a quick way to compute the average of a column data in a numerical tab delimeted file? Thanks, Gussi (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

9. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies

10. Shell Programming and Scripting

file in to the column of the table

Hi I am ETL person. I have a table in Oracle having one of its column defined as BLOB where it can hold huge data. When i generate a file in DataStage and place it in a path. and i would like dump the file in to the column of the table having datatype as BLOB. (0 Replies)
Discussion started by: sant_leo
0 Replies
Login or Register to Ask a Question