File manipulations with Awk (in Terminal)

Posted:
in Mac Software edited January 2014
I need some help with the Awk application within the Terminal app.



I have a file with a large amount of data (rows of 3D cartesian coordinates). The data is typically like the following example (actually, the data is made of several thousands of "curves" defined by a list of points in 3D) :



Quote:

2.344\t1.453\t-1.345

1.234\t3.134\t3.123

2.566\t1.345\t2.344

-1.234\t-2.333\t-4.555

1.344\t1.453\t-1.345 # End of curve

3.234\t4.134\t6.123

2.566\t4.345\t3.344

-1.234\t-2.333\t-4.555

-7.344\t1.453\t-5.345 # End of curve

1.234\t-3.134\t3.123

6.566\t5.345\t8.344

-1.234\t2.333\t4.555 # End of curve



I need Awk to read this input file, and write out the number of lines for each "curve" (with some arbitrary symbol in front of it), followed by a list of line numbers. For the data above, Awk should output something like this :



Quote:

NumberOfLines 5

0 1 2 3 4



NumberOfLines 4

5 6 7 8



NumberOfLines 3

9 10 11



Take note that the first line is numbered as "0".

How can I do that ? Someone has an idea ?



The only data manipulation I know to do with Awk is like this example :



Quote:

cat path_to_input | awk '{ print $1 "\\t" $2 "\\t" $3 "\\t"}' > path_to_output



Thanks.

Comments

  • Reply 1 of 1
    kalikali Posts: 634member
    Okay, I've found the solution to my problem :



    Quote:

    cat path_to_input_file | awk '

    {s=n?s " " c++:c++; n++}

    /# End/{print "NumberOfLines " n; print s "\

    "; n=0; s =""}

    ' > path_to_output_file



    Worked like a charm !
Sign In or Register to comment.