Professional Web Applications Themes

How to replace variable spaces with a tab - Please Help - PERL Beginners

I have a text file that is a mess. It contains various fields with lots of spaces between the fields. The amount of spaces differs from field to field. Here is a sample of the file: <8sp>NAME:<13sp>JOHN SMITH<22sp>RANK:<3sp>CAPTAIN<15sp>WIFE:<6sp>POCAHO NTAS... What I would like to have after processing the file is a tab delimited file like: <tab>NAME:<tab>JOHN SMITH<tab>RANK:<tab>CAPTAIN<tab>WIFE:<tab>POCAHONT AS... I have been experimenting with the s/// and had some success. The only problem is that I have about 30 of the s/// statements to try and deal with all of the <sp> in the file. I know this is not efficient. ...

  1. #1

    Default How to replace variable spaces with a tab - Please Help

    I have a text file that is a mess. It contains various fields with lots
    of spaces between the fields. The amount of spaces differs from field to
    field. Here is a sample of the file:

    <8sp>NAME:<13sp>JOHN
    SMITH<22sp>RANK:<3sp>CAPTAIN<15sp>WIFE:<6sp>POCAHO NTAS...

    What I would like to have after processing the file is a tab delimited
    file like:

    <tab>NAME:<tab>JOHN SMITH<tab>RANK:<tab>CAPTAIN<tab>WIFE:<tab>POCAHONT AS...

    I have been experimenting with the s/// and had some success. The only
    problem is that I have about 30 of the s/// statements to try and deal
    with all of the <sp> in the file. I know this is not efficient.

    What I want to do is replace any repeating spaces (two or more) with a
    tab. I know the examples below do not work but am just giving it as
    "pseudocode" of replacing repeating spaces (between 2 and 50) with a tab:

    s/<sp>[2..50]/\t/gi;

    or maybe hex value (hex 20 is the space character)

    s/\x20[2..50]/\t/gi;

    Thanks.


    Perlwannabe Guest

  2. #2

    Default Re: How to replace variable spaces with a tab - Please Help

    On Saturday, September 6, 2003, at 07:59 AM, perlwannabe wrote:
    > I have a text file that is a mess. It contains various fields with
    > lots
    > of spaces between the fields. The amount of spaces differs from field
    > to
    > field. Here is a sample of the file:
    >
    > <8sp>NAME:<13sp>JOHN
    > SMITH<22sp>RANK:<3sp>CAPTAIN<15sp>WIFE:<6sp>POCAHO NTAS...
    >
    > What I would like to have after processing the file is a tab delimited
    > file like:
    >
    > <tab>NAME:<tab>JOHN
    > SMITH<tab>RANK:<tab>CAPTAIN<tab>WIFE:<tab>POCAHONT AS...
    >
    > I have been experimenting with the s/// and had some success. The only
    > problem is that I have about 30 of the s/// statements to try and deal
    > with all of the <sp> in the file. I know this is not efficient.
    >
    > What I want to do is replace any repeating spaces (two or more) with a
    > tab. I know the examples below do not work but am just giving it as
    > "pseudocode" of replacing repeating spaces (between 2 and 50) with a
    > tab:
    >
    > s/<sp>[2..50]/\t/gi;
    >
    > or maybe hex value (hex 20 is the space character)
    >
    > s/\x20[2..50]/\t/gi;
    s/\s{2,}/\t/g;

    James

    James Edward Gray II Guest

  3. #3

    Default Re: How to replace variable spaces with a tab - Please Help

    <SNIP, SNIP>
    >> What I want to do is replace any repeating spaces (two or more) with a
    >> tab. I know the examples below do not work but am just giving it as
    >> "pseudocode" of replacing repeating spaces (between 2 and 50) with a
    >> tab:
    >>
    >> s/<sp>[2..50]/\t/gi;
    >>
    >> or maybe hex value (hex 20 is the space character)
    >>
    >> s/\x20[2..50]/\t/gi;
    >
    > s/\s{2,}/\t/g;
    Perfect, thank you very much. I knew I was close, but just couldn't get
    there.


    Antigone Guest

Similar Threads

  1. Regex - assign to new variable and replace in one line?
    By Brad Walton in forum PERL Miscellaneous
    Replies: 12
    Last Post: June 22nd, 12:17 PM
  2. replace spaces with 0's in a string
    By weezerboy in forum Macromedia ColdFusion
    Replies: 1
    Last Post: February 16th, 09:56 PM
  3. Regular expression - replace non printable chars with spaces
    By Francesco in forum PHP Development
    Replies: 3
    Last Post: December 1st, 05:48 PM
  4. Display variable with spaces
    By Pushpinder Singh Garcha in forum PHP Development
    Replies: 3
    Last Post: August 13th, 05:47 PM
  5. Perl variable "leading white spaces" Please help
    By \Dandy\ Randy in forum PERL Miscellaneous
    Replies: 2
    Last Post: July 21st, 12:33 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139