![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 10:52 PM |
| Adding columns to excel files using Perl | dolo21taf | Shell Programming and Scripting | 1 | 02-20-2008 03:13 AM |
| Adding columns of two files | chandra321 | Shell Programming and Scripting | 6 | 04-06-2007 06:36 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
| Forum Sponsor | ||
|
|
|
|||
|
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.
__________________
My brain is your brain |
|
|||
|
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. |
|
|||
|
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" } }' |
|||
| Google The UNIX and Linux Forums |