0

I have a rather large text file(thousands of lines) in following format:

123.123 5  
123.123 91  
156.456 45  
233.185 85  
233.185 4  
257.448 1  
455.456 60  
455.456 2

The first column are sorted coordinates (x.y) and the second one is the label (0 to 1000). As it can be seen the coordinate 123.123 has label 5 and label 91 or coordinates 156.456 has only label 45

I want another file as output with all the coordinates (not duplicate) and the different labels (this label will appear several times duplicate), the output for this example must be:

123.123  5   91  
156.456  45   
233.185  85  4  
257.448  1  
455.456  60  2   

My idea was to remove duplicate strings (coordinates, so only first column), leaving alone labels and then reorganize the lines or another way but I am stuck with which commands to use.
Another option is with databases.

1

looks like an awk's job

 awk '{ c[$1]=c[$1] "\t" $2 ; } END { for ( c2 in c ) printf c2 c[c2] "\n" ; } '

where

  • c[$1]=c[$1] "\t" $2 will store label,
  • END clause is run at end of file.
  • for ( c2 in c ) printf c2 c[c2] "\n" ; will lopp through coordinates, printing label.
|improve this answer|||||
0
perl -p0e 'while(s/^(\S+)( .*)(\n\1)/\1\2/m){}'

The basic idea is:

  • -p0 load the file in memory and print it in the end;
  • -e execute the next argument on the loaded file
  • while possible, substitute <word> <line>\n<word> by <word> <line>
|improve this answer|||||
  • 1
    maybe edit your post to add a few line to explain, just in case someone came with a similar problem but with 4 columns or some variation ? – Archemar Sep 7 '15 at 9:04
  • @Archemar, thank you for the wise sugestion. – JJoao Sep 8 '15 at 22:41

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.