The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Adding columns to a file figaro UNIX for Dummies Questions & Answers 5 07-20-2008 10:50 PM
comparing files - adding/subtracting/formating columns oabdalla Shell Programming and Scripting 7 06-13-2008 12:20 AM
Perl: adding columns in CSV file with information in each dolo21taf Shell Programming and Scripting 1 03-04-2008 11:52 PM
Adding columns to excel files using Perl dolo21taf Shell Programming and Scripting 1 02-20-2008 04:13 AM
Adding columns of two files chandra321 Shell Programming and Scripting 6 04-06-2007 06:36 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 01-05-2002
Kelam_Magnus's Avatar
Registered User
 

Join Date: Aug 2001
Location: DFW McKinney, TX,
Posts: 1,069
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!!!!!!!!!
__________________
My brain is your brain
Reply With Quote
Forum Sponsor
  #2  
Old 01-05-2002
Jimbo
Guest
 

Posts: n/a
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
Reply With Quote
  #3  
Old 01-07-2002
Kelam_Magnus's Avatar
Registered User
 

Join Date: Aug 2001
Location: DFW McKinney, TX,
Posts: 1,069
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.


__________________
My brain is your brain
Reply With Quote
  #4  
Old 01-07-2002
Registered User
 

Join Date: Jan 2002
Posts: 5
awk -F"," '{print $1 $2}'

This should do it for you!

that appears to be a comma separator. Just replace the "," with "." if not.
Reply With Quote
  #5  
Old 01-07-2002
Jimbo
Guest
 

Posts: n/a
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.
Reply With Quote
  #6  
Old 01-07-2002
Registered User
 

Join Date: Jan 2002
Posts: 5
thanks for the redirect

thanks for the redirect :->
Reply With Quote
  #7  
Old 01-25-2002
Registered User
 

Join Date: Sep 2001
Location: Switzerland
Posts: 13
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"
}
}'
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:22 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0