Adding variable values to output (to file)


I have a ruby script the reads a delimited text input file, does some
filtering based on value of specific fields, and writes two output
files. There is a pass file for rows that pass the conditionals and
there is a fail file for rows that fail.

I would like to add some columns to the pass file that indicate a
classification for passing rows.

The output is created with this code,

[[‘pass’,pass],[‘fail’,fail]].each { |name,data|"#{name}_#{outsuffix}",‘w’) { |fh|
fh.write([colnames,*data].map { |r| r[1].join("\t")+"\n" }.join)

The data is read is and stored with this code,

(colnames,*records)=File.readlines("#{filename}").map { |l|
[l,l.chomp.split(/\t/)] }


I need to add a column called “class” and add a value to the column. The
value will be the same for all rows (excepting the header row).

What I have done so far is to pass in a value for what is to be added,
(classname is the value)

and then add that value to every row of input by changing the above to,

(colnames,*records)=File.readlines("#{filename}").map { |l|
[l,l.chomp.split(/\t/) << classname] }

This gives me a column at the end where every row has the value of what
was passed in the classname argument. At this point, I have to do
some post processing in awk to change the value to “class” for the
header row and then move the column to be the second column in the file
instead of the last one. This is alright, but is seems silly to create a
file with ruby and then have to tweak it in awk. There are also some
more complicated things I need to do where the value won’t be the same
for every row, so I need to dig into this a bit more. I need to learn
the syntax for adding to a specific row of output at a specific column
position. I didn’t write the above code and it’s not obvious to me where
this should go.

I have posted an sample script and sample input file in case that would
be helpful. The script is run as,
$./C0014_filter_amine_alcohol.rb _ActiMol-HTS_500.txt output.txt C0014


I also forgot to mention that I am wondering if it would be easier to
already have a column in the input file named class with some default
value and to change the default value to something else, as opposed to
trying to shoehorn in additional columns.

Any thoughts on that???