×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Searching for '\' characters

Searching for '\' characters

Searching for '\' characters

(OP)
Hey there,

   it's wierd but I can't get the following to work, must be doing something silly somewhere. I need to remove the following strings from a text file:

"\"    i.e. the backslash characeter (usually at the end of a line)
and
"\n\"  i.e. backslash n backslash (usually at the end of the line or the only thing on a line)

I am using:

$string =~ s/\\//g;
and
$string =~ s/\\n\\//g;

But they don't seem to be working, anyone got any better regexps (i.e. one that works  ;-))

Many thanx!
Loon

RE: Searching for '\' characters

Here's how to remove a \ from a string.

$s="hello\\";
print "$s\n";   # displays 'hello\'
$s=~s/\\//;
print "$s\n";   # displays 'hello'

that sequence of chars at the end of lines in your text files looks weird.

\n is the normal "new-line" character -- so I would expect to see that on the end of each line

Look again at your data.... you might be misreading it?

Mike
michael.j.lacey@ntlworld.com
Cargill's Corporate Web Site

RE: Searching for '\' characters

(OP)
Mike,
    if it looks wierd it's HPs fault!! It's from one of their HPUX message catalogues that their STM tool uses, for example:

$
$ STM_DIAG2_WARNING
$
1 WARNING: Could not open DIAG2 file. \n\
\n\
Possible Causes/Recommended Action: \n\
\n\
   The diag2 pseudo driver may not be configured into this operating system. \
This driver is required for the proper functioning of various support tools. \
Please consult the system installation manual on how to configure \
diag2 driver.
$


So the \n\ characters are actually different to the \n in the catalog iteself as they are used to format the messages when they are displayed by the STM application.

And the reason my regexps weren't working was I was writing $_ to a file rather than my modified string! DOH! Friday afternoon bug....

Cheers!
Loon

RE: Searching for '\' characters

Hi Loon,

So, in Perl terms, that line

WARNING: Could not open DIAG2 file. \n\

is equivalent to:

"WARNING: Could not open DIAG2 file. \\\n\\\n"

which looks more than a bit weird but look at the last three (visible) chars on the line, they each have to be escaped out. So - to remove them then, the regex:

$var =~ s/\\\n\\\$//;

will match those three chars when they occur at the end of a string (or line) in $var (that's the $ bit at the end of the expression) and remove them from $var.

You can do something similar when it's just a \ char at the end of the line like this:

$var =~ s/\\$//;

Backslashes R Us, unfortunately.

Hope I understood your problem correctly and that the response is useful. Please let me know if I got it wrong at any point.

Mike
michael.j.lacey@ntlworld.com
Cargill's Corporate Web Site

RE: Searching for '\' characters

(OP)
Tip top Mike!

Cheers
Loon

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close