-3

This question is not a duplicate as someone had suggested. Mods, pay attention

I'm running a for loop on multiple files that contain information like below

1    Leer            Normal   [status]     —      100
1    Wrap            Normal   [physical]   15     90
4    Poison Sting    Poison   [physical]   15     100
9    Bite            Dark     [physical]   60     100
12   Glare           Normal   [status]     —      100
17   Screech         Normal   [status]     —      85
20   Acid            Poison   [special]    40     100
25   Spit Up         Normal   [special]    —      100
25   Stockpile       Normal   [status]     —      —
25   Swallow         Normal   [status]     —      —
28   Acid Spray      Poison   [special]    40     100
33   Mud Bomb        Ground   [special]    65     85
36   Gastro Acid     Poison   [status]     —      100
38   Belch           Poison   [special]    120    90
41   Haze            Ice      [status]     —      —
44   Coil            Poison   [status]     —      —
49   Gunk Shot       Poison   [physical]   120    80

I need to be able to extract data from it.

The problem is, each file has different column lengths.


Column 2 sometimes has spaces in it so squeezing all spaces and using space as a delimiter for cut is not an option. I need the columns separated by tabs without using specific information because the loop goes over about 800 files.

sed 's/  \+/ /g' | cut -f 2 -d " "

^ Not what I need since column 2 has spaces in it

cut -b "5-20"

^ Can't use this either because the columns lengths are different for each file.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
knuxyl
  • 129
  • 3
  • 12

1 Answers1

1

With sed, to replace multiples consecutive spaces or tabs with one tab:

sed 's/[[:space:]]\{1,\}/\t/g' file

Explanations:

  • s: substitute
  • [[:space:]]: space or tab characters
  • \{1,\}: when at least one occurrence is found
  • g: apply substitution to all occurrences in line

Edit:

To preserve single spaces in second column, you can replace only when 2 spaces/tabs are found:

    sed 's/[[:space:]]\{2,\}/\t/g' file
SLePort
  • 15,211
  • 3
  • 34
  • 44
  • Thanks! This worked perfectly. If someone could explain this code to me also that would be great. – knuxyl Jul 08 '17 at 18:52
  • Looking bat at it, it does exactly what I didn't need it to do. It also converted the spaces in the 2nd column to tabs. I provided example scripts that do exactly what you said but much simpler to show what will not work. – knuxyl Jul 08 '17 at 18:58
  • You're right. I edited and added some explanations. – SLePort Jul 09 '17 at 05:45