[R] using a regular expression

David Wolfskill r at catwhisker.org
Sun Sep 11 15:50:14 CEST 2016

On Sat, Sep 10, 2016 at 07:23:37PM +0000, Glenn Schultz wrote:
> ...
> Below is the function that I am writing.  I am using sed to replace the hex characters.  First, to get past NUL I use sed to replace hex 00 with hex 20.  This has worked.  Once the Nul is removed and can successfully parse the file with ReadLine sub_str.  This final step before delimiting the file and making it nice and tidy is to remove the hex 20 characters.   I am using the same strategy to eliminate the spaces and sed command works in a shell but does not work in the R function.  What am I doing wrong?  I have dput - some of the nastier lines with hex 20 characters below my code.

I believe that you will find that the sed "d" command deletes the
"pattern space" (in a simple text file, it would delete the line) in
which the specified regular expression is found.

I suspect that you actually want to eliminate the "space" characters
themselves, so rather than:

> ...
> # The file has been parsed accroding to length 400 for each data element.
> # The next step is to remove all the trailing white space hex character
> # x20
> sedcommand2 <- paste("sed -e '/\\x20/d' <", 

what is wanted is:

sedcommand2 <- paste("sed -e 's/\\x20//g' <", 

> ... 

Note that you might consider using R's gsub() function to perform that
"space elimination"both natively and a bit earlier.

David H. Wolfskill				r at catwhisker.org
Those who would murder in the name of God or prophet are blasphemous cowards.

See http://www.catwhisker.org/~david/publickey.gpg for my public key.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 603 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20160911/9bf52a5e/attachment.bin>

More information about the R-help mailing list