Maheen Siddiqui
Maheen Siddiqui

Reputation: 185

Changing a file

My program was writing two values to a file side by side - (as two columns) By some stupid programming mistake I have managed to the following

fprintf(network_energy_delta_E_BM, "%f\n\t %f\n", delta_network_energy_BM,
        network_energy_initial);

"%f\n\t %f\n"

Means that my data ended up looking a bit like this:

1234
     56
24
     99

and so on and so forth. This causing some problems for me and what I need to do.. Is there any way to amend the file so that they are side by side? I tried

paste network_energy_delta_E_BM.dat foo.dat | awk '{ print $1 }' > new.dat

where network_energy_delta_E_BM.dat is my file and foo.dat is just an empty file. But it takes all the entries and just puts them into one column. Can anyone help fix these please? There are over 2000000 entries so I can't fix this by hand.

Or is there any way of taking this new file new.dat and taking every second entry and writing those to a new column?

Thank you

Upvotes: 0

Views: 48

Answers (1)

fedorqui
fedorqui

Reputation: 289795

What about using this awk expression?

awk '!(NR%2){print p,$0*1; next}{p=$0}' file

It joins two lines into one, where NR means number of record (line, in this case). Note that $0*1 is used to delete the trailing tab of the 2nd line.

Test

$ cat a
1234
     56
24
     99
$ awk '!(NR%2){print p,$0*1; next}{p=$0}' a
1234 56
24 99

Upvotes: 3

Related Questions