adding columns

Hey everyone!

I have a need to add 2 files together as columns.

For instance, I have one file that has several rows of data and I want to take data from another file and add Line 1 to the end of Line1 in the first file

file1 line1.........file2 line1
file1 line2.........file2 line2
file1 line3.........file2 line3

etc.....

I know that there are at least 2 ways to do this either with awk or with the cut command. I just can't remember right now.

Thanks in advance.

Lord of the Rings RRRRRRRRRUUUUUUUUUULLLLLLLLEEEEESSSSSSSSSS!!!!!!!!!
:smiley:

I think paste is more suited to this task. Since you do not show any space between your concatenated lines, you would want:

paste -d"\0" file1 file2 > newfile

If you want a separator (colon, space, etc):

paste -d: file1 file2 > newfile
paste -d" " file1 file2 > newfile

Separation by tab character is the default:

paste file1 file2 > newfile

Thanks it works great. I have been an Admin for about 4 years, but I am still amazed at the versatility of UNIX and the variety of commands that are available.

Another command that I had not used before is "vacation". A very good command if you send and receive email primarily from your UNIX box.

:smiley:

awk -F"," '{print $1 $2}'

This should do it for you!

that appears to be a comma separator. Just replace the "," with "." if not.

davidl,
Your awk solution does not specify a file to process. If we add a file:

awk -F"," '{print $1 $2}' myfile

then your solution will process each line of myfile, printing the first two words of each line (as delimited by a comma) with no separation.

Kelam_Magnus was wanting a horizontal (side-by-side) merge of two files. awk can do that, but not as easily as paste.

thanks for the redirect :->

#######################################################################
## add_up_columns
## utility that takes input and whenever Number-of-fields (NF)
## is greater than 0, it adds up by column. Totals line is
## formatted identically to the last data line.
#######################################################################

tr "%" " " |\
awk 'BEGIN { limit = 1e-8 }
{ ### if this is a data line, sum the columns ###
if (NF > 0)
{ if (NF > maxNF) { maxNF = NF }
if (length($0) > maxlen) { maxlen = length($0) }
for (f = 1; f <= NF; f++)
{ sum[f] += $f
if ($f+0.0 != 0) numeric_line = 1
if ($f+0.0 != 0) { ftype[f] = "f" } else { ftype[f] = "s" }
}
if (numeric_line) { observations += 1; numeric_line = 0 }
numflds = split($0,fieldarr," ")
print $0
tempstr=$0
got_data = 1
}

   \#\#\# if this line has no fields, then print column totals \#\#\#
   if \(NF == 0 && got_data\)

       \{ \#\#\# if all totals are zero then don\`t print cause its prob. a title \#\#\#
         valid_total = 0
         for \(f = 1; f &lt;= maxNF; f\+\+\) \{ if \(sum[f] != 0\) valid_total = 1 \}

         if \(!valid_total\)
            \{ printf "\\n" \}
         else
            \{ \#\#\# get starting location of each field in last data line \#\#\#  
               lastchar = " ";  fld = 0
               for \(c = 1; c &lt;= length\(tempstr\); c\+\+\)
                   \{ char = substr\(tempstr,c,1\)
                     if \(char != " " && lastchar == " "\) \{ fld \+=1;  fldpos[fld] = c-1 \}
                     lastchar = char
                   \} 

               \#\#\# get remaining format data from last data line \#\#\#
               for \(f = 1; f &lt;= numflds; f\+\+\)
                   \{ fs = f ""
                     fdata = fieldarr[fs]
                     fldlen[f] = length\(fdata\)
		 if \(ftype[f] == "s"\)
		     \{ digit[f] = "";  period = "" \}
                     else
		     \{ period = "."
		       if \(index\(fdata,"."\) != 0\) \{ digit[f] = fldlen[f]-index\(fdata,"."\) \} else \{ digit[f] = 0 \}
                         \}
                     pad[f] = fldpos[f] - fldpos[f-1] \+ fldlen[f-1] - 1
                     format[f] = "%" \(fldlen[f] \+ fldpos[f] - fldpos[f-1] - fldlen[f-1]\) period digit[f] ftype[f]
                   \}

               \#\#\# print dotted line and totals row \#\#\#
               for \(i = 1; i &lt;= maxlen; i\+\+\) \{ printf "-" \}
               printf "\\n"
               for \(f = 1; f &lt;= maxNF; f\+\+\) \{ avg[f] = sum[f]/observations \}
	   if \(sum[1]\+0.0 == 0\) \{ sum[1] = "TOT";  avg[1] = "AVG" \}
               for \(f = 1; f &lt;= maxNF; f\+\+\) \{ printf format[f],sum[f] \}
               printf "\\n"
               for \(f = 1; f &lt;= maxNF; f\+\+\) 
                   \{ printf format[f],avg[f]
                     format[f] = ""; sum[f] = 0; avg[f] = 0
                   \}
               printf "\\n"; printf "\\n"; printf "\\n"
             \}

         observations = 0
         maxNF = 0
         maxlen = 0
         got_data = 0
       \}
 \}

END { ### if last total has not been printed, then print it now ###
if (got_data)
{ ### get starting location of each field in last data line ###
lastchar = " "; fld = 0
for (c = 1; c <= length(tempstr); c++)
{ char = substr(tempstr,c,1)
if (char != " " && lastchar == " ") { fld +=1; fldpos[fld] = c-1 }
lastchar = char
}

         \#\#\# get remaining format data from last data line \#\#\#
         for \(f = 1; f &lt;= numflds; f\+\+\)
             \{ fs = f ""
               fdata = fieldarr[fs]
               fldlen[f] = length\(fdata\)
	   if \(ftype[f] == "s"\)
	       \{ digit[f] = "";  period = "" \}
               else
	       \{ period = "."
	         if \(index\(fdata,"."\) != 0\) \{ digit[f] = fldlen[f]-index\(fdata,"."\) \} else \{ digit[f] = 0 \}
                   \}
               pad[f] = fldpos[f] - fldpos[f-1] \+ fldlen[f-1] - 1
               format[f] = "%" \(fldlen[f] \+ fldpos[f] - fldpos[f-1] - fldlen[f-1]\) period digit[f] ftype[f]
             \}

         \#\#\# print the totals row \#\#\#
         for \(i = 1; i &lt;= maxlen; i\+\+\) \{ printf "-" \}
         printf "\\n"
         for \(f = 1; f &lt;= maxNF; f\+\+\) \{ avg[f] = sum[f]/observations \}
     if \(sum[1]\+0.0 == 0\) \{ sum[1] = "TOT";  avg[1] = "AVG" \}
         for \(f = 1; f &lt;= maxNF; f\+\+\) \{ printf format[f],sum[f] \}
         printf "\\n"
         for \(f = 1; f &lt;= maxNF; f\+\+\) \{ printf format[f],avg[f] \}
         printf "\\n"
       \}
 \}'

This is a great little script for adding_up_columns try it, if you like it I might post some other goodies. Keepin it free....

Instructions:

cat FILENAME | add_up_columns

Actually, I was talking about combining two files together in a columnar format. Not adding as in mathematical "adding"

But, thanks for the cool script anyway!

:wink:

I should read the thread before posting. I thought you guys might get a kick out of this shell anyway.

Hello everyone,
I have a number n of files and I need to add the first column of each file in another output using awk
for example:
file1 file1 ... outputfile
1 2 3 4 2 3 1 4...
1 2 3 4 2 3 1 4
1 2 3 4 2 3 1 4
Thanks!!!

Sorry....
the example maybe is more clear in this way
file1
1 2 ...
1 2...
1 2...
file2
3 2 ...
3 2...
3 2...
outfile
1 3 ...
1 3 ...
1 3 ...

Pls open a new thread... its because the person who started this thread will get unnecessary intimation and moreover this thread is dead for more than 6 yrs...