Convert Column to rows


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert Column to rows
# 1  
Old 03-31-2010
Convert Column to rows

Hi,

I have a file with below contents.

Code:
Heading1 Heading2 Heading3 Heading4
Value1 Value2 Value3 Value4

The file has only 2 rows and is tab separated
The desired output is :

Code:
Heading1 Value1
Heading2 Value2
Heading3 Value3
Heading4 Value4

CAn you please help?

Last edited by radoulov; 03-31-2010 at 06:22 AM.. Reason: Please use code tags!
# 2  
Old 03-31-2010
Use gawk, nawk or /usr/xpg4/bin/awk on Solaris:
Code:
awk 'END { 
  while (++j < i) 
    print _[j] 
    }
{
  for (i = 0; ++i <= NF;)
    _[i] = _[i] ? _[i] FS $i : $i
  }' infile

# 3  
Old 03-31-2010
Thanks for your reply, however i am unable to run the script, which shell are you using.
Below r the details of my env.


Machine hardware: sun4us
OS version: 5.10
Processor type: sparc
Hardware: FJSV,GPUZC-M

The following components are installed on your system:


Sun Studio 11
Sun Studio 11 C Compiler
Sun Studio 11 C++ Compiler
Sun Studio 11 Tools.h++ 7.1
Sun Studio 11 C++ Standard 64-bit Class Library
Sun Studio 11 Garbage Collector
Sun Studio 11 Fortran 95
Sun Studio 11 Debugging Tools (including dbx)
Sun Studio 11 IDE
Sun Studio 11 Debugger GUI
Sun Studio 11 Performance Analyzer (including collect, ...)
Sun Studio 11 X-Designer
Sun Studio 11 VIM editor
Sun Studio 11 XEmacs editor
Sun Studio 11 Native Connector Tool
Sun Studio 11 Performance Library
Sun Studio 11 LockLint
Sun Studio 11 Building Software (including dmake)
Sun Studio 11 Documentation Set

version of "/opt/SUNWspro/bin/../prod/bin/../../bin/cc": Sun C 5.8 2005/10/13
version of "/opt/SUNWspro/bin/../prod/bin/../../bin/CC": Sun C++ 5.8 2005/10/13
version of "/opt/SUNWspro/bin/../prod/bin/../../bin/f90": Sun Fortran 95 8.2 2005/10/13
version of "/opt/SUNWspro/bin/../prod/bin/../../bin/dbx": Sun Dbx Debugger 7.5 2005/10/13
version of "/opt/SUNWspro/bin/../prod/bin/../../bin/analyzer": Sun Performance Analyzer 7.5 2005/10/13
version of "/opt/SUNWspro/bin/../prod/bin/../../bin/dmake": Sun Distributed Make 7.7 2005/10/13
# 4  
Old 03-31-2010
Quote:
Originally Posted by kaponeh
Thanks for your reply, however i am unable to run the script, which shell are you using.
[...]
What happens? Do you get errors or an incorrect output?
If you're using (t)csh, you should run the command as a script.

script content:

Code:
END {     
  while (++j < i) 
    print _[j] 
    }
{
  for (i = 0; ++i <= NF;)
    _[i] = _[i] ? _[i] OFS $i : $i
  }

To execute:

Code:
nawk -f <script_name> OFS='\t' <input_file_name>

P.S. Added OFS as per OP requirement.
# 5  
Old 03-31-2010
Code:
#head -1 infile | tr " " "\n"  > tmp
#tail -1  infile | tr " " "\n"  > tmp1

Code:
#paste tmp tmp1
Heading1        Value1
Heading2        Value2
Heading3        Value3
Heading4        Value4

Thanks,
Penchal

Last edited by radoulov; 03-31-2010 at 07:23 AM.. Reason: Please use code tags!
# 6  
Old 03-31-2010
Thanks a lot both of you !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

DB2 Query -Convert multi values from column to rows

Hi Team I am using DB2 artisan tool and struck to handle multi values present in columns that are comma(,) separated. I want to convert those column values in separate rows . For example : Column 1 Column2 Jan,Feb Hold,Sell,Buy Expected Result Column1 ... (3 Replies)
Discussion started by: Perlbaby
3 Replies

2. Shell Programming and Scripting

Convert rows into column along with header

Hi, I have a requirement to format the data in a new order. Here is my source format : ppp ***Wed Dec 16 10:32:30 GMT 2015 header1 header2 header3 header4 header5 server1 0.00 0.02 0.07 0.98 server2 0.01 0.00 0.08 0.79 server3 0.05 0.82 0.77 0.86 ... (18 Replies)
Discussion started by: john_prince
18 Replies

3. Shell Programming and Scripting

Convert Column data values to rows

Hi all , I have a file with the below content Header Section employee|employee name||Job description|Job code|Unitcode|Account|geography|C1|C2|C3|C4|C5|C6|C7|C8|C9|Csource|Oct|Nov|Dec|Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep Data section ... (1 Reply)
Discussion started by: Hypesslearner
1 Replies

4. Shell Programming and Scripting

Convert rows to column and add header

Hi, I need help to convert rows in input file into a table. inputfile 192.98.1 192.98.192.98.17 VVC family Zorro 10 192.98.1 192.98.192.98.17 VVC family Ace 1 192.98.1 192.98.192.98.17 VVC family ... (4 Replies)
Discussion started by: redse171
4 Replies

5. Shell Programming and Scripting

Convert single column into multiple rows

Convert Single column to multiple rows file a.txt contains data like below Server=abc Run=1 Tables=10 Sessions=16 Time=380 Jobs=5 Server=abc Run=2 Tables=15 Sessions=16 Time=400 Jobs=5 Server=abc Run=3 Tables=20 Sessions=16 Time=450 (5 Replies)
Discussion started by: sol_nov
5 Replies

6. Shell Programming and Scripting

Convert Rows into Column

Hi Experts, I have a requirement to convert rows into columns. For e.g. Input File: Output File should be like Appreciate if you could suggest code snippet(may be awk) for above requirement... Thanks in Advance for your help... (3 Replies)
Discussion started by: sai_2507
3 Replies

7. Shell Programming and Scripting

convert columns into rows with respect to first column

Hello All, Please help me with this file. My input file (Tab separated) is like: Abc-01 pc1 -0.69 Abc-01 E2cR 0.459666666666667 Abc-01 5ez.2 1.2265625 Xyz-01 pc1 -0.153 Xyz-01 E2cR 1.7358 Xyz-01 5ez.2 2.0254 Ced-02 pc1 -0.5714 Ced-02 ... (7 Replies)
Discussion started by: mira
7 Replies

8. Shell Programming and Scripting

Convert rows into column groups

Hi I have the text file like this "A" "AA Info" "AA Text" "AAA" "ABC" "ABC Info" "ABC Tech" "AGH" "SYN" "SYMBony" "SYN BEREN" Like about 2000 lines Output would be in Column with groups like following "A" "AA Info", "AA Text" "AAA" "ABC","ABC Info","ABC Tech" (0 Replies)
Discussion started by: selvanraj
0 Replies

9. UNIX for Dummies Questions & Answers

How to convert a single column into several rows and columns?

I have a program which gives me the output as a single column with hundreds of rows like: 213 314 324 324 123 I want to be able to create a new file from this file which allows me to set the number of rows and columns in the new file, i.e. for this example, if I specify 3 rows and 2... (5 Replies)
Discussion started by: ashton_smith
5 Replies

10. Shell Programming and Scripting

convert rows into column

if u have a data 2 4 6 8 5 4 4 5 6 then result shud be like 2 4 6 7 5 4 4 5 6 (3 Replies)
Discussion started by: cdfd123
3 Replies
Login or Register to Ask a Question