Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add a new column!

Status
Not open for further replies.

abdu22

Programmer
Sep 13, 2005
14
FR
Hi,

I have this output file:

RET1 Src1
Tgt1 5
Tgt2 6
Tgt3 8
.
.

I want to read other data and add the results in the third column of previous output file.. to get this one:

RET1 Src1 Src2
Tgt1 5 2
Tgt2 6 3
Tgt3 8 1
.
.

How can I add the new data to be in columns?..

Thanks
 
if you have two files on disk

paste file1 file2 >file_with_cols_of_both_files

I believe you can also paste stdin

whatever_command|paste file1 - >file_with_cols_of_file1_and_output_of_cmd

(file2 is specified as a dash -> paste reads from stdin

if file1 contains a header line, file2 needs one too
if file1 contains a header line, stdout of cmd needs to provide one too

see man page for paste


HTH,

p5wizard
 
Thanks p5wizard but I don't have two files to be merged!.. my script has many steps; after the first step I have an output file, in the next steps I'd like to continue writing on the same output file but adding the results in columns.. I wish it's clear now :)
 
Hi

So, use [tt]printf[/tt]instead of [tt]print[/tt] to not add the record separator by default, or set [tt]ORS[/tt] to empty string. Then write whatever you want in the same line and ant the end print the newline character ( \n ) yourself.

But this should by too easy, I think there are missing some details of the problem.

Feherke.
 
Hi feherke... you are right.. but it is difficult to explain the whole problem.. I think I will output the results of each step to a temp file then join the temp files togther... Thanks
 
Hi abdu22,

Have you thought of using 'arrays'? Most shells support single dimesion arrays up to 1024 (0 to 1023) elements.
EG:
heading[0]="RET1"
heading[1]="Src1"
target1[0]="Tgt1"
target1[1]="5"

Then using counters and while loops you can manipulate the arrays and finally output to a file.

Just a thought, I hope it helps.

Mike
 
Thank you Mike042 for this thought.... May be it will help me for the second choice.. :)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top