The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages here.

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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-05-2002
Kelam_Magnus's Avatar
Kelam_Magnus Kelam_Magnus is offline Forum Advisor  
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!!!!!!!!!
  #2 (permalink)  
Old 01-05-2002
Jimbo
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
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
  #3 (permalink)  
Old 01-07-2002
Kelam_Magnus's Avatar
Kelam_Magnus Kelam_Magnus is offline Forum Advisor  
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.


  #4 (permalink)  
Old 01-07-2002
davidl davidl is offline
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.
  #5 (permalink)  
Old 01-07-2002
Jimbo
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
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.
  #6 (permalink)  
Old 01-07-2002
davidl davidl is offline
Registered User
  
 

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

thanks for the redirect :->
  #7 (permalink)  
Old 01-25-2002
calden1138 calden1138 is offline
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"
}
}'
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:36 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0