Find the answer to your Linux question:
Results 1 to 3 of 3
I have a line containing junk characters e.g. ^D^H^HHello world, I am doing $i = s/\^D//ig; $i = s/\^H//ig; is there any better way to remove only these junk char?...
  1. #1
    Just Joined!
    Join Date
    Apr 2008
    Posts
    19

    Is there any way remove all junk char from a string/line using perl?

    I have a line containing junk characters e.g. ^D^H^HHello world,

    I am doing

    $i = s/\^D//ig;
    $i = s/\^H//ig;

    is there any better way to remove only these junk char?

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Sure is:
    Code:
    $i =~ tr/a-zA-Z0-9 _,.-//cd
    We are using the 'tr' operator here. You give tr two lists, and it maps each character from the first list to the corresponding character of the second list. For instance:
    Code:
    tr /ab/AB/
    would translate every 'a' to 'A', and every 'b' to 'B'.

    We are using two special options here. The first is /c. /c means to take the complement of the first set: so rather than use the first set, use everything that is not in the first set. This allows us to specify what we want to keep, and get rid of everything else.

    The second option is /d. /d means that if a character does not have a corresponding character in the second set (imagine, for instance, that the second set is smaller than the first), you should just delete the character (default behavior is to use the last character in the second set repeatedly).

    So the above command says to take every character that is not one that we specified, and delete it.

    Hope that helps!
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Apr 2008
    Posts
    19
    yeah, it works it deletes all junk char but now I realised that my hash is also not able to store whole string containing junk value.

    I am retrieving the data from XML file and there is a data which has junk char. Looks like hash which i am using for storing the values itself is not picking whole string containing junk values.

    Can I do something so that is should pick the whole string?

Posting Permissions

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