Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching for '\' characters

Status
Not open for further replies.

Loon

Programmer
May 24, 2000
100
GB
Hey there,<br><br>&nbsp;&nbsp;&nbsp;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:<br><br>&quot;\&quot;&nbsp;&nbsp;&nbsp;&nbsp;i.e. the backslash characeter (usually at the end of a line)<br>and<br>&quot;\n\&quot;&nbsp;&nbsp;i.e. backslash n backslash (usually at the end of the line or the only thing on a line)<br><br>I am using:<br><br><FONT FACE=monospace>$string =~ s/\\//g;</font><br>and<br><FONT FACE=monospace>$string =~ s/\\n\\//g;</font><br><br>But they don't seem to be working, anyone got any better regexps (i.e. one that works&nbsp;&nbsp;;-))<br><br>Many thanx!<br>Loon
 
Here's how to remove a \ from a string.<br><br>$s=&quot;hello\\&quot;;<br>print &quot;$s\n&quot;;&nbsp;&nbsp;&nbsp;# displays 'hello\'<br>$s=~s/\\//;<br>print &quot;$s\n&quot;;&nbsp;&nbsp;&nbsp;# displays 'hello'<br><br>that sequence of chars at the end of lines in your text files looks weird.<br><br>\n is the normal &quot;new-line&quot; character -- so I would expect to see that on the end of each line<br><br>Look again at your data.... you might be misreading it? <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Mike,<br>&nbsp;&nbsp;&nbsp;&nbsp;if it looks wierd it's HPs fault!! It's from one of their HPUX message catalogues that their STM tool uses, for example:<br><FONT FACE=monospace><br>$<br>$ STM_DIAG2_WARNING<br>$<br>1 WARNING: Could not open DIAG2 file. \n\<br>\n\<br>Possible Causes/Recommended Action: \n\<br>\n\<br>&nbsp;&nbsp;&nbsp;The diag2 pseudo driver may not be configured into this operating system. \<br>This driver is required for the proper functioning of various support tools. \<br>Please consult the system installation manual on how to configure \<br>diag2 driver.<br>$<br></font><br><br>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.<br><br>And the reason my regexps weren't working was I was writing $_ to a file rather than my modified string! DOH! Friday afternoon bug....<br><br>Cheers!<br>Loon<br>
 
Hi Loon,<br><br>So, in Perl terms, that line <br><br>WARNING: Could not open DIAG2 file. \n\<br><br>is equivalent to:<br><br>&quot;WARNING: Could not open DIAG2 file. \\\n\\\n&quot;<br><br>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:<br><br>$var =~ s/\\\n\\\$//;<br><br>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.<br><br>You can do something similar when it's just a \ char at the end of the line like this:<br><br>$var =~ s/\\$//;<br><br>Backslashes R Us, unfortunately.<br><br>Hope I understood your problem correctly and that the response is useful. Please let me know if I got it wrong at any point.<br> <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Tip top Mike!<br><br>Cheers<br>Loon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top