INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

building from multiple files

building from multiple files

(OP)
Hi! Basically, I have 2 text files and need to combine them into one while making a few changes. The commands I came up with were too long/slow so I figured some more familiar with awk could help me.

Basically I have these 2 files, were N~50000 :

file1.res

1 a1 a2 ... a13
2 b1 b2 ... b13
.
.
.
N x1 x2 ... x13

and file2.res

1 aa1 aa2 ... aa13
2 bb1 bb2 ... bb13
.
.
.
N xx1 xx2 ... xx13

I need an output file of the form:

x1 a1 aa1 b1 bb1 ... x1 xx1
x2 a2 aa2 b2 bb2 ... x2 xx2
.
.
x13 a13 aa13 b13 bb13 ... x13 xx13


I manage to do it using intermediate files, but for really big N that's most inconvenient, so can anyone see a one-liner for this one?

RE: building from multiple files

(OP)
I forgot to say x1 to x13 in the last table are headers, they're not part of any previous file.

RE: building from multiple files

Something like this?

CODE --> awk

awk '
        NR==FNR {
                if (NF>maxnf) maxnf=NF
                for (i=1;i<=NF;i++) f1[FNR,i]=$i
                file1nr=FNR
                next
        }
        {
                if (NF>maxnf) maxnf=NF
                for (i=1;i<=NF;i++) f2[FNR,i]=$i
        }
        END {
                for (i=2;i<=maxnf;i++) {
                        printf "%s ",f1[file1nr,i]
                        for (j=1; j<=file1nr; j++)
                                printf "%s %s ",f1[j,i],f2[j,i]
                        printf "\n"
                }
        }
' file1.res file2.res 

Not sure it caters for your second comment about headers correctly... but should point you in the right direction.

Annihilannic
tgmlify - code syntax highlighting for your tek-tips posts

RE: building from multiple files

(OP)
Yo, thank you :) That's very helpful. It work and I also sorted the headers. I really augth to learn this awk thing better, it's quite impressive what it can achieve.

RE: building from multiple files

(OP)
Thanks again! Could you just explain a detail to me please?

My file ended up with too many lines, so I added this command:

awk -v n=13 'NR>n{print a[NR%n]}{a[NR%n]=$0}'

to removes the last 13 lines, it works, but I would rather use

awk -v n=NR/2 'NR>n{print a[NR%n]}{a[NR%n]=$0}'

to remove half of the lines (might not always be 13 in the future). However an error message says it's trying to divide by 0... What's going on?

RE: building from multiple files

"-v n=NR/2" is processed only once before the main script is run, also it isn't interpreted as awk code, so you just end up with the text "NR/2" in the variable.

Also NR is a dynamic value; it increases as awk processes each line of input, so NR/2 would be a moving target.

If you are processing the file in 2 phases (like my example), then you'll need to calculate your equivalent to NR/2 in the END { ... } clause of the script, something like this:

CODE --> AWK

                        # ...
                        for (j=1; j<=file1nr/2; j++)
                                printf "%s %s ",f1[j,i],f2[j,i]
                        # ... 

Annihilannic
tgmlify - code syntax highlighting for your tek-tips posts

RE: building from multiple files

(OP)
That clarifies so many things. Thank you

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close