adding columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting adding columns
# 1  
Old 01-05-2002
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!!!!!!!!!
Smilie
# 2  
Old 01-05-2002
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
Jimbo
# 3  
Old 01-07-2002
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.


Smilie
# 4  
Old 01-07-2002
awk -F"," '{print $1 $2}'

This should do it for you!

that appears to be a comma separator. Just replace the "," with "." if not.
# 5  
Old 01-07-2002
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.
Jimbo
# 6  
Old 01-07-2002
thanks for the redirect

thanks for the redirect :->
# 7  
Old 01-25-2002
For the almighty of adding columns

#######################################################################
## 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 <= 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 <= 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 <= 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 <= maxlen; i++) { printf "-" }
printf "\n"
for (f = 1; f <= maxNF; f++) { avg[f] = sum[f]/observations }
if (sum[1]+0.0 == 0) { sum[1] = "TOT"; avg[1] = "AVG" }
for (f = 1; f <= maxNF; f++) { printf format[f],sum[f] }
printf "\n"
for (f = 1; f <= 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 <= 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 <= maxlen; i++) { printf "-" }
printf "\n"
for (f = 1; f <= maxNF; f++) { avg[f] = sum[f]/observations }
if (sum[1]+0.0 == 0) { sum[1] = "TOT"; avg[1] = "AVG" }
for (f = 1; f <= maxNF; f++) { printf format[f],sum[f] }
printf "\n"
for (f = 1; f <= maxNF; f++) { printf format[f],avg[f] }
printf "\n"
}
}'
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding columns from 2 files with variable number of columns

I have two files, file1 and file2 who have identical number of rows and columns. However, the script is supposed to be used for for different files and I cannot know the format in advance. Also, the number of columns changes within the file, some rows have more and some less columns (they are... (13 Replies)
Discussion started by: maya3
13 Replies

2. Shell Programming and Scripting

Adding columns with values dependent on existing columns

Hello I have a file as below chr1 start ref alt code1 code2 chr1 18884 C CAAAA 2 0 chr1 135419 TATACA T 2 0 chr1 332045 T TTG 0 2 chr1 453838 T TAC 2 0 chr1 567652 T TG 1 0 chr1 602541 ... (2 Replies)
Discussion started by: plumb_r
2 Replies

3. Shell Programming and Scripting

Help with ask adding columns

Hello, I am using AWK in UBUNTU 12.04. I have a dataset as follows: 1 2 12 1 4 1 4 1 7 9 4 6 1 2 4 5 7 8 45 7 4 5 7 5 What I want to do is to add the values of some columns to each other and print it in the same file as the new column while omitting the previous two columns to have... (3 Replies)
Discussion started by: Homa
3 Replies

4. Shell Programming and Scripting

Adding columns of time

Hello all, I'm in the process of writing a script, and I need to be able to add columns of time in the following format (time elapsed Net Backup logs): 000:01:03 000:00:58 000:00:49 Does anyone have a way of converting and/or adding timestamps such as these accurately? Thanks in... (9 Replies)
Discussion started by: LinuxRacr
9 Replies

5. Shell Programming and Scripting

sorting and adding columns

i have a file with two columns, and i want to uniquely sort the values in fist column and add the corresponding values in the second columns eg file a contents tom 200 john 300 sow 500 tom 800 james 50 sow 300 output shpould be in file b as tom 1000 john 300 sow 800 james 50 (0 Replies)
Discussion started by: dealerso
0 Replies

6. UNIX for Dummies Questions & Answers

Adding loads of columns

Hi All, I've got file1 like this: aaa bbb ccc ddd eee fff ggg hhh kkk ppp mmm nnn and file 2 like this: aaa qqq www ddd fff ggg ggg sss zzz ppp vvv yyy and file 3 like this: aaa ggg ppp I need to match the first column of file3 and file1, then add the rest of the file 1 to... (3 Replies)
Discussion started by: zajtat
3 Replies

7. UNIX for Dummies Questions & Answers

Adding lines and columns to a file

Hi everybody, I've got two simples file1 like: aaa aaa aaa bbb bbb bbb ccc ccc ccc and file2 like: 111 111 111 222 222 222 333 333 333 I need to: 1) add a line say "new line" as the first line of the file 2)add a column from file2 (say column3) to file1; the new column should... (14 Replies)
Discussion started by: zajtat
14 Replies

8. Shell Programming and Scripting

Adding columns of two files

Hello everyone, I have two files containing 6 columns and thousands of rows. I want to add them (i.e. first column of first file + first column of second file and so on) and print the output in a third file. Can you please help me. Thanks a lot (7 Replies)
Discussion started by: chandra321
7 Replies

9. UNIX for Dummies Questions & Answers

Adding columns to a file

I want to select the first column from a daily file called foo.csv. The result is written to file foo.txt. Currently the following script is used for that: cut -d, -f 1 foo.csv > foo.txt A typical result would yield : A12 A45 B11 B67 What needs to happen in addition is that two columns... (5 Replies)
Discussion started by: figaro
5 Replies

10. UNIX for Advanced & Expert Users

Adding columns in csv

I have the following data in FILE1.CSV: code: Amount1: Amount2: xxxxx ,, 200 ,,400 yyxxa ,,200 bbcgu ,,2500 ,,300 i want to be able to produce the following FILE2.CSV: code: Amount xxxxx ,, 600... (7 Replies)
Discussion started by: chachabronson
7 Replies
Login or Register to Ask a Question