Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have the following text lines:

.add 7971 [Player: Sunfire (Account: 11309) X: 5839.623047 Y: 600.437439 Z: 650.839722 Map: 571 Selected player: Llubia (GUID: 19369)]  
.add 43956 [Player: Sunfire (Account: 11309) X: 5277.887695 Y: 2862.181641 Z: 446.735931 Map: 571 Selected none:  (GUID: 0)]  
.add 43956 [Player: Sunfire (Account: 11309) X: 5281.407715 Y: 2864.844482 Z: 446.735931 Map: 571 Selected player: Staticbaby (GUID: 19826)]  
.add 43956 [Player: Sunfire (Account: 11309) X: 5231.464844 Y: 1437.029175 Z: 648.498535 Map: 571 Selected player: Sunfire (GUID: 15295)]  
.add 44077 [Player: Sunfire (Account: 11309) X: 5231.464844 Y: 1437.029175 Z: 648.498535 Map: 571 Selected player: Sunfire (GUID: 15295)]  
.add 49285 [Player: Sunfire (Account: 11309) X: 16225.323242 Y: 16252.759766 Z: 12.790466 Map: 1 Selected none:  (GUID: 0)]  
.add 44115 175 [Player: Elmasguapo (Account: 11309) X: 1659.845093 Y: -4198.589844 Z: 56.382870 Map: 1 Selected none:  (GUID: 0)]  
.add 34078 [Player: Sunfire (Account: 11309) X: 16227.969727 Y: 16280.081055 Z: 13.175169 Map: 1 Selected none:  (GUID: 0)]  
.add  |cffffffff|Hitem:41427:0:0:0:0:0:0:0:80|h[Fuego de Artificio de Dalaran]|h|r 50 [Player: Sunfire (Account: 11309) X: 16221.392578 Y: 16260.944336 Z: 13.255954 Map: 1 Selected none:  (GUID: 0)]  
.add  |cffffffff|Hitem:45932:0:0:0:0:0:0:0:80|h[Gelatina Negra]|h|r [Player: Sunfire (Account: 11309) X: 5874.347168 Y: 679.056763 Z: 167.483719 Map: 571 Selected player: Assasins (GUID: 19438)]  
.add  |cffffffff|Hitem:45932:0:0:0:0:0:0:0:80|h[Gelatina Negra]|h|r [Player: Sunfire (Account: 11309) X: 5873.767090 Y: 679.386841 Z: 167.435257 Map: 571 Selected player: Assasins (GUID: 19438)]  
.add  |cffffffff|Hitem:45932:0:0:0:0:0:0:0:80|h[Gelatina Negra]|h|r [Player: Sunfire (Account: 11309) X: 16226.880859 Y: 16247.247070 Z: 12.286857 Map: 1 Selected player: Irmtarget (GUID: 18521)]  
.add  |cffffffff|Hitem:45932:0:0:0:0:0:0:0:80|h[Gelatina Negra]|h|r [Player: Sunfire (Account: 11309) X: 16229.297852 Y: 16251.202148 Z: 13.081388 Map: 1 Selected player: Irmtarget (GUID: 18521)]  
.add 41600 2 [Player: Sunfire (Account: 11309) X: 16223.138672 Y: 16250.496094 Z: 12.431313 Map: 1 Selected player: Eifreen (GUID: 20341)]  
.add 41600 1 [Player: Sunfire (Account: 11309) X: 16223.138672 Y: 16250.496094 Z: 12.431313 Map: 1 Selected player: Eifreen (GUID: 20341)]  
.add 40516 [Player: Sunfire (Account: 11309) X: 16223.138672 Y: 16250.496094 Z: 12.431313 Map: 1 Selected player: Eifreen (GUID: 20341)]  
.add 44661 [Player: Sunfire (Account: 11309) X: 16223.138672 Y: 16250.496094 Z: 12.431313 Map: 1 Selected player: Eifreen (GUID: 20341)]  
.add 40518 [Player: Sunfire (Account: 11309) X: 16223.138672 Y: 16250.496094 Z: 12.431313 Map: 1 Selected player: Eifreen (GUID: 20341)]  
.add 44005 [Player: Sunfire (Account: 11309) X: 16223.138672 Y: 16250.496094 Z: 12.431313 Map: 1 Selected player: Eifreen (GUID: 20341)]  
.add 45867 [Player: Sunfire (Account: 11309) X: 16223.138672 Y: 16250.496094 Z: 12.431313 Map: 1 Selected player: Eifreen (GUID: 20341)]  
.add 45316 [Player: Sunfire (Account: 11309) X: 16223.138672 Y: 16250.496094 Z: 12.431313 Map: 1 Selected player: Eifreen (GUID: 20341)]  

And I want to parse it so it outputs something like this:

Line 1 For example - 7971 Llubia
Line 3 For example - 43956 Staticbaby
Line 9 For Example - 45932 Assassins

And so on.. This is done in the terminal with commands like cut, grep, cat, etc..

UPDATE: Here is the whole file: http://paste2.org/p/1744102 to parse in that way.

UPDATE2: Please forgive me if I do not accept an answer just yet. Am waiting for the bounty option to appear since for me, stuff like this deserve a bounty. So I will add a bounty and give it to the correct answer or answers.

share|improve this question
    
Do you just want the numbers and strings ("7971 Llubia")? Or do you want the line numbers as well ("Line 1 For Example - 7971 Llubia")? –  Kris Harper Oct 28 '11 at 19:17
    
Without the line numbers. Just the "7971 Llubia". –  Luis Alvarado Oct 28 '11 at 19:46
    
Updated to show a part of the file to parse just to make sure it works before posting an answer. –  Luis Alvarado Oct 28 '11 at 20:43

3 Answers 3

up vote 2 down vote accepted
+50
awk '
  # ignore lines with "none:" or "player:" in $(NF-2)
  $(NF-2) ~ /^(none|player):$/ { 
    next   # read next line
  }

  { 
    # remove dots from $2 in every line
    gsub(/\./,"",$2) 
  }

  /\|Hitem:/ {
    # if it has "|Hitem:" in $2, remove everything
    # before and after the colons
    sub(/.+\|Hitem:/,"",$2)
    sub(/:.+/,"",$2)
    print $2,$(NF-2)
    next   # read next line
  }

  {
    print $2,$(NF-2)
  }
' your_file.txt

Line 380 has Selected player: (GUID: 6873)]. You have to decide what to do with such lines (ignore them? print the GUID instead?).

share|improve this answer
    
It would be ignore them since they do not show a name. –  Luis Alvarado Oct 28 '11 at 22:27
    
Ok, I edited my answer accordingly. –  elmicha Oct 28 '11 at 22:51
1  
WOW! elmicha, that is an excellent EXCELLENT script. Tested and works excellent. Now let's wait until the bounty option appears so I can give a bonus for the answer. –  Luis Alvarado Oct 28 '11 at 23:14

This can be what you want?

grep -v 'Selected none' input-file | awk '$2+0 > 0 { print $2, $(NF-2) }'
share|improve this answer
    
Works good by changing print $2 to $5 so it appears the numbers and names like I mentioned above but in cases like line 9 something else appears instead of the numbers. +1 for a cool script. –  Luis Alvarado Oct 28 '11 at 20:34

It's not really feasible to use command line tools to parse irregular ouput. Would be better to use python, etc..

If you had complete regularity, e.g., you could use something as simple as:

awk '{print $2 " " $17}' INPUT

This misses the 'Hitem' lines from your example.

This is an ugly hack, but it takes the 'Hitem' lines into account:

 sed 's/^.*Hitem:\([0-9]*\):0/\1/' INPUT | awk -F: '{print $1 " " $0}' | sed 's/\.add //' | sed 's/^\([0-9]* \).*Selected player: \([A-Za-z]* \).*$/\1 \2/' | egrep -v 'Selected none'

Which gave me:

7971  Llubia 
43956  Staticbaby 
43956  Sunfire 
44077  Sunfire 
45932  Assasins 
45932  Assasins 
45932  Irmtarget 
45932  Irmtarget 
41600  Eifreen 
41600  Eifreen 
40516  Eifreen 
44661  Eifreen 
40518  Eifreen 
44005  Eifreen 
45867  Eifreen 
45316  Eifreen 

This fails on the additional info you've added, however, which is why I've added the comments/caveat about regularity here.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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