Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I think this forum rocks it has saved my bacon many many times..."

Geography

Where in the world do Tek-Tips members come from?

building from multiple filesHelpful Member! 

sbgirl54 (Programmer)
16 Aug 12 9:45
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?
sbgirl54 (Programmer)
16 Aug 12 10:39
I forgot to say x1 to x13 in the last table are headers, they're not part of any previous file.
Annihilannic (MIS)
17 Aug 12 2:30
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

sbgirl54 (Programmer)
17 Aug 12 3:59
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.
sbgirl54 (Programmer)
17 Aug 12 4:40
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?
Helpful Member!  Annihilannic (MIS)
19 Aug 12 19:15
"-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

sbgirl54 (Programmer)
24 Aug 12 9:03
That clarifies so many things. Thank you

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!

Back To Forum

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