0
Frequencies -- 1403.6738 1403.6738 1403.6738
IR Inten -- 25.0809 25.0809 25.0809

I want to get two columns

Frequencies     IR Inten
1403.6738       25.0809

and so on

  • 2
    Welcome to the site. Can you tell us how large these files are approximately (i.e. how many entries) – AdminBee Sep 25 at 15:19
  • 4
    What should "and so on" look like? Should there be a new header for the next pair of values? The example data is not to large, so maybe you could give a more complete expected output. – Kusalananda Sep 25 at 15:19
  • No, I want to complete the number of frequencies under it and the number of IR become under the IR like two column Frequencies IR Inten 1403.6738 25.0809 1403.6738 25.0809 – Abdelazim Sep 25 at 15:38
  • If you need, I can explain more – Abdelazim Sep 25 at 15:51
  • Ok, just to be sure--each "Frequency" value pairs up with an "IR Inten" value? There are no blanks, extra-spaces, NAs, etc.? Thx. – jubilatious1 Sep 25 at 16:41
0

Using Raku (the programming language formerly known as Perl6)

Raku has a nifty Z operator for problems like this, to 'zip up' items, one from each list:

~$ raku -e 'my @array1 = lines(); put ( [Z] @array1[0].words, @array1[1].words).join("\n");'  Freq_IR.txt
Frequencies IR_Inten
-- --
1403.6738 25.0809
1403.6738 25.0809
1403.6738 25.0809

Note: I had to correct the header line above so that the second column is titled "IR_Inten". You can edit by hand or run the code below, prior to rearranging the data with the code above:

~$ raku -pe 's/IR \s Inten/IR_Inten/;'  Freq_IR.txt > Freq_IR2.txt

HTH.

https://raku.org
https://docs.raku.org/language/operators#Zip_metaoperator
https://docs.raku.org/language/operators#index-entry-[+]_(reduction_metaoperators)

| improve this answer | |
  • If you used .comb with a regex instead of .words you don't need the preprocess step. ~$ raku -e '.put for [Z] lines>>.comb: /[<:L>+]+ % \s+ || \S+/' (runs of <:L>etters separated by [\s]paces or a run of non-[\S]paces.) Other options for the regex /^ .* <before " -"> | \S+/ or / "IR Inten" | \S+ / I also simplified it by removing the needless array. Note that you can write ( [Z] … ) as [Z]‍( … ) – Brad Gilbert Oct 15 at 19:56
0

Using any awk in any shell on every Unix box:

$ awk -F' -- ' -v OFS='\t' '
    { hdr[NR]=$1; n=split($2,v," "); for (i in v) vals[NR,i]=v[i] }
    END { print hdr[1], hdr[2]; for (i=1; i<=n; i++) print vals[1,i], vals[2,i] }
' file
Frequencies     IR Inten
1403.6738       25.0809
1403.6738       25.0809
1403.6738       25.0809

and to work for any number of output rows and columns:

awk -F' -- ' -v OFS='\t' '
    { hdr[NR]=$1; n=split($2,v," "); for (i in v) vals[NR,i]=v[i] }
    END {
        for (c=1; c<=NR; c++) {
            printf "%s%s", hdr[c], (c<NR ? OFS : ORS)
        }
        for (r=1; r<=n; r++) {
            for (c=1; c<=NR; c++) {
                printf "%s%s", vals[c,r], (c<NR ? OFS : ORS)
            }
        }
    }
' file
| improve this answer | |
0

Using awk we can do the data transformation.

awk -F ' -- ' '
{
  # populate the array
  a[++N] = $1; split($2, t, " ")
  for (k in t) { a[++N] = t[k] }
}
END {ORS = ""
  for (c=i=1; i<=N/NR; i++) {
    for (j=i; j<=N; j+=N/NR) {
      print a[j] (c++%NR ? "\t" : "\n")
    }
  }
}
' file
Frequencies IR Inten
1403.6738   25.0809
1403.6738   25.0809
1403.6738   25.0809

Basic idea is to first populate the array and record the length of the array at the end. Then chop the array into N/NR parts and pick corresponding elements from each portion. In other languages like Python this operation is called zip.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.