How to handling string contains single quote and double quote

Discussion in 'Perl Misc' started by vikrant, Apr 28, 2007.

  1. vikrant

    vikrant Guest

    Hi,
    I facing a problem in saving a string contains single and double quote
    into a file .The string look like the following:-

    Example:-
    Developer's community "com.lang.perl"
    --------------------------------------------------------
    The part of Code is:-

    #!/bin/perl -w
    use strict;

    my $Date="10-09-2007";

    system("echo '$Date;$String_Value' > data.txt");
    ----------------------------------------------------------------------------------------
    Here,the $String_Value Variable store the string which i have
    mentioned above.

    I cannot put the slash( \ ) before the double quote's because in my
    case value of input string is dynamic.
    Also,I know putting a slash before double quote do not solve my
    problem because single quote is left which will create the problem.I
    get to know about this when i put slash before the double quote's.

    The error coming on screen is:-
    ------------------------------------------------------------------------------------------------
    sh: -c: line 0: unexpected EOF while looking for matching `''
    sh: -c: line 1: syntax error: unexpected end of file
    -----------------------------------------------------------------------------------------------------

    So,please tell me how do i handle this type of input.
    I apologize if someone already asked this question earlier.

    Thanks and Regards,
    Vikrant
     
    vikrant, Apr 28, 2007
    #1
    1. Advertising

  2. vikrant

    vikrant Guest

    On Apr 28, 1:41 pm, vikrant <> wrote:
    > Hi,
    > I facing a problem in saving a string contains single and double quote
    > into a file .The string look like the following:-
    >
    > Example:-
    > Developer's community "com.lang.perl"
    > --------------------------------------------------------
    > The part of Code is:-
    >
    > #!/bin/perl -w
    > use strict;
    >
    > my $Date="10-09-2007";
    >
    > system("echo '$Date;$String_Value' > data.txt");
    > ----------------------------------------------------------------------------------------
    > Here,the $String_Value Variable store the string which i have
    > mentioned above.
    >
    > I cannot put the slash( \ ) before the double quote's because in my
    > case value of input string is dynamic.
    > Also,I know putting a slash before double quote do not solve my
    > problem because single quote is left which will create the problem.I
    > get to know about this when i put slash before the double quote's.
    >
    > The error coming on screen is:-
    > ------------------------------------------------------------------------------------------------
    > sh: -c: line 0: unexpected EOF while looking for matching `''
    > sh: -c: line 1: syntax error: unexpected end of file
    > -----------------------------------------------------------------------------------------------------
    >
    > So,please tell me how do i handle this type of input.
    > I apologize if someone already asked this question earlier.
    >
    > Thanks and Regards,
    > Vikrant



    Sorry for the Subject line mistake it should be something like
    following:-
    How to handle the string contains single quote and double quote
     
    vikrant, Apr 28, 2007
    #2
    1. Advertising

  3. vikrant

    Paul Lalli Guest

    On Apr 28, 4:41 am, vikrant <> wrote:
    > Hi,
    > I facing a problem in saving a string contains single and double quote
    > into a file .The string look like the following:-
    >
    > Example:-
    > Developer's community "com.lang.perl"
    > --------------------------------------------------------
    > The part of Code is:-
    >
    > #!/bin/perl -w
    > use strict;
    >
    > my $Date="10-09-2007";
    >
    > system("echo '$Date;$String_Value' > data.txt");
    > ---------------------------------------------------------------------------­-------------
    > Here,the $String_Value Variable store the string which i have
    > mentioned above.
    >
    > I cannot put the slash( \ ) before the double quote's because in my
    > case value of input string is dynamic.
    > Also,I know putting a slash before double quote do not solve my
    > problem because single quote is left which will create the problem.I
    > get to know about this when i put slash before the double quote's.
    >
    > The error coming on screen is:-
    > ---------------------------------------------------------------------------­---------------------
    > sh: -c: line 0: unexpected EOF while looking for matching `''
    > sh: -c: line 1: syntax error: unexpected end of file
    > ---------------------------------------------------------------------------­--------------------------
    >
    > So,please tell me how do i handle this type of input.
    > I apologize if someone already asked this question earlier.


    Why are you calling a system command to do the echoing? Just open the
    file for writing in Perl, and print the line to the file in Perl.
    Then you don't have to worry about any escaping of quotes.

    open my $fh, '>', 'data.txt' or die "Cannot open file: $!";
    print $fh "$Date;$String_Value\n";
    close $fh;

    Paul Lalli
     
    Paul Lalli, Apr 28, 2007
    #3
  4. vikrant

    vikrant Guest

    On Apr 28, 3:26 pm, Paul Lalli <> wrote:
    > On Apr 28, 4:41 am, vikrant <> wrote:
    >
    >
    >
    > > Hi,
    > > I facing a problem in saving a string contains single and double quote
    > > into a file .The string look like the following:-

    >
    > > Example:-
    > > Developer's community "com.lang.perl"
    > > --------------------------------------------------------
    > > The part of Code is:-

    >
    > > #!/bin/perl -w
    > > use strict;

    >
    > > my $Date="10-09-2007";

    >
    > > system("echo '$Date;$String_Value' > data.txt");
    > > ---------------------------------------------------------------------------­-------------
    > > Here,the $String_Value Variable store the string which i have
    > > mentioned above.

    >
    > > I cannot put the slash( \ ) before the double quote's because in my
    > > case value of input string is dynamic.
    > > Also,I know putting a slash before double quote do not solve my
    > > problem because single quote is left which will create the problem.I
    > > get to know about this when i put slash before the double quote's.

    >
    > > The error coming on screen is:-
    > > ---------------------------------------------------------------------------­---------------------
    > > sh: -c: line 0: unexpected EOF while looking for matching `''
    > > sh: -c: line 1: syntax error: unexpected end of file
    > > ---------------------------------------------------------------------------­--------------------------

    >
    > > So,please tell me how do i handle this type of input.
    > > I apologize if someone already asked this question earlier.

    >
    > Why are you calling a system command to do the echoing? Just open the
    > file for writing in Perl, and print the line to the file in Perl.
    > Then you don't have to worry about any escaping of quotes.
    >
    > open my $fh, '>', 'data.txt' or die "Cannot open file: $!";
    > print $fh "$Date;$String_Value\n";
    > close $fh;
    >
    > Paul Lalli


    Thanks for the information.Actually,the code is a part of a
    function,which called again and again.So,i thought that opening and
    closing a file on each call may effect the performance.That was the
    only reason of using the system command.
    Comments are most welcome.

    Thanks and Regards,
    Vikrant
     
    vikrant, Apr 28, 2007
    #4
  5. vikrant <> wrote:
    > On Apr 28, 3:26 pm, Paul Lalli <> wrote:
    > > On Apr 28, 4:41 am, vikrant <> wrote:
    > >
    > > > system("echo '$Date;$String_Value' > data.txt");

    > >
    > > Why are you calling a system command to do the echoing? Just open the
    > > file for writing in Perl, and print the line to the file in Perl.
    > > Then you don't have to worry about any escaping of quotes.
    > >
    > > open my $fh, '>', 'data.txt' or die "Cannot open file: $!";
    > > print $fh "$Date;$String_Value\n";
    > > close $fh;
    > >
    > > Paul Lalli


    > Thanks for the information.Actually,the code is a part of a
    > function,which called again and again.So,i thought that opening and
    > closing a file on each call may effect the performance.That was the
    > only reason of using the system command.


    Actually, your approach uses a lot more resources and time.
    The system() function must create a new process, which in turn
    executes a shell to deal with the command you sent to system().
    And the file must be opened and closed again anyway, but now
    the shell will do that. So all the system() call does is add
    overhead, saving you nothing at all (beside making it harder
    to check for errors).
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Apr 28, 2007
    #5
  6. vikrant

    vikrant Guest

    On Apr 28, 4:45 pm, (Jens Thoms Toerring) wrote:
    > vikrant <> wrote:
    > > On Apr 28, 3:26 pm, Paul Lalli <> wrote:
    > > > On Apr 28, 4:41 am, vikrant <> wrote:

    >
    > > > > system("echo '$Date;$String_Value' > data.txt");

    >
    > > > Why are you calling a system command to do the echoing? Just open the
    > > > file for writing inPerl, and print the line to the file inPerl.
    > > > Then you don't have to worry about any escaping of quotes.

    >
    > > > open my $fh, '>', 'data.txt' or die "Cannot open file: $!";
    > > > print $fh "$Date;$String_Value\n";
    > > > close $fh;

    >
    > > > Paul Lalli

    > > Thanks for the information.Actually,the code is a part of a
    > > function,which called again and again.So,i thought that opening and
    > > closing a file on each call may effect the performance.That was the
    > > only reason of using the system command.

    >
    > Actually, your approach uses a lot more resources and time.
    > The system() function must create a new process, which in turn
    > executes a shell to deal with the command you sent to system().
    > And the file must be opened and closed again anyway, but now
    > the shell will do that. So all the system() call does is add
    > overhead, saving you nothing at all (beside making it harder
    > to check for errors).
    > Regards, Jens
    > --
    > \ Jens Thoms Toerring ___
    > \__________________________ http://toerring.de


    Hi JensThoms,

    Thanks for sharing the information.It is very helpful to me.

    Regards,
    Vikrant
     
    vikrant, Apr 28, 2007
    #6
  7. vikrant

    Joe Smith Guest

    vikrant wrote:
    > the code is a part of a
    > function,which called again and again.So,i thought that opening and
    > closing a file on each call may effect the performance.That was the
    > only reason of using the system command.
    > Comments are most welcome.


    FYI: Invoking system() like that means 32 separate file operations each
    time it is called.

    linux% strace -f -e trace=file sh -c 'echo foo > temp.txt' |& cat -n
    1 execve("/bin/sh", ["sh", "-c", "echo foo > temp.txt"], [/* 41 vars */]) = 0
    2 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
    3 open("/etc/ld.so.cache", O_RDONLY) = 3
    4 fstat64(3, {st_mode=S_IFREG|0644, st_size=79328, ...}) = 0
    5 open("/lib/libtermcap.so.2", O_RDONLY) = 3
    6 fstat64(3, {st_mode=S_IFREG|0755, st_size=12924, ...}) = 0
    7 open("/lib/libdl.so.2", O_RDONLY) = 3
    8 fstat64(3, {st_mode=S_IFREG|0755, st_size=16244, ...}) = 0
    9 open("/lib/libc.so.6", O_RDONLY) = 3
    10 fstat64(3, {st_mode=S_IFREG|0755, st_size=1485672, ...}) = 0
    11 open("/dev/tty", O_RDWR|O_NONBLOCK|O_LARGEFILE) = 3
    12 open("/usr/lib/locale/locale-archive", O_RDONLY|O_LARGEFILE) = 3
    13 fstat64(3, {st_mode=S_IFREG|0644, st_size=49641600, ...}) = 0
    14 open("/etc/mtab", O_RDONLY) = 3
    15 fstat64(3, {st_mode=S_IFREG|0644, st_size=414, ...}) = 0
    16 open("/proc/meminfo", O_RDONLY) = 3
    17 fstat64(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
    18 stat64("/home/jms", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
    19 stat64(".", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
    20 open("/usr/lib/gconv/gconv-modules.cache", O_RDONLY) = 3
    21 fstat64(3, {st_mode=S_IFREG|0644, st_size=22294, ...}) = 0
    22 open("/usr/lib/gconv/ISO8859-1.so", O_RDONLY) = 3
    23 fstat64(3, {st_mode=S_IFREG|0755, st_size=5340, ...}) = 0
    24 stat64(".", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
    25 stat64("/usr/bin/sh", 0xbff56eb4) = -1 ENOENT (No such file or directory)
    26 stat64("/usr/sbin/sh", 0xbff56eb4) = -1 ENOENT (No such file or directory)
    27 stat64("/bin/sh", {st_mode=S_IFREG|0755, st_size=686520, ...}) = 0
    28 access("/bin/sh", X_OK) = 0
    29 stat64("/bin/sh", {st_mode=S_IFREG|0755, st_size=686520, ...}) = 0
    30 access("/bin/sh", X_OK) = 0
    31 open("temp.txt", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666
     
    Joe Smith, May 2, 2007
    #7
  8. vikrant

    Wade Ward Guest

    "Joe Smith" <> wrote in message
    news:...
    > vikrant wrote:
    >> the code is a part of a
    >> function,which called again and again.So,i thought that opening and
    >> closing a file on each call may effect the performance.That was the
    >> only reason of using the system command.
    >> Comments are most welcome.

    >
    > FYI: Invoking system() like that means 32 separate file operations each
    > time it is called.
    >
    > linux% strace -f -e trace=file sh -c 'echo foo > temp.txt' |& cat -n
    > 1 execve("/bin/sh", ["sh", "-c", "echo foo > temp.txt"], [/* 41 vars
    > */]) = 0
    > 2 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file
    > or directory)
    > 3 open("/etc/ld.so.cache", O_RDONLY) = 3
    > 4 fstat64(3, {st_mode=S_IFREG|0644, st_size=79328, ...}) = 0
    > 5 open("/lib/libtermcap.so.2", O_RDONLY) = 3
    > 6 fstat64(3, {st_mode=S_IFREG|0755, st_size=12924, ...}) = 0
    > 7 open("/lib/libdl.so.2", O_RDONLY) = 3
    > 8 fstat64(3, {st_mode=S_IFREG|0755, st_size=16244, ...}) = 0
    > 9 open("/lib/libc.so.6", O_RDONLY) = 3
    > 10 fstat64(3, {st_mode=S_IFREG|0755, st_size=1485672, ...}) = 0
    > 11 open("/dev/tty", O_RDWR|O_NONBLOCK|O_LARGEFILE) = 3
    > 12 open("/usr/lib/locale/locale-archive", O_RDONLY|O_LARGEFILE) = 3
    > 13 fstat64(3, {st_mode=S_IFREG|0644, st_size=49641600, ...}) = 0
    > 14 open("/etc/mtab", O_RDONLY) = 3
    > 15 fstat64(3, {st_mode=S_IFREG|0644, st_size=414, ...}) = 0
    > 16 open("/proc/meminfo", O_RDONLY) = 3
    > 17 fstat64(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
    > 18 stat64("/home/jms", {st_mode=S_IFDIR|0755, st_size=12288, ...}) =
    > 0
    > 19 stat64(".", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
    > 20 open("/usr/lib/gconv/gconv-modules.cache", O_RDONLY) = 3
    > 21 fstat64(3, {st_mode=S_IFREG|0644, st_size=22294, ...}) = 0
    > 22 open("/usr/lib/gconv/ISO8859-1.so", O_RDONLY) = 3
    > 23 fstat64(3, {st_mode=S_IFREG|0755, st_size=5340, ...}) = 0
    > 24 stat64(".", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
    > 25 stat64("/usr/bin/sh", 0xbff56eb4) = -1 ENOENT (No such file
    > or directory)
    > 26 stat64("/usr/sbin/sh", 0xbff56eb4) = -1 ENOENT (No such file
    > or directory)
    > 27 stat64("/bin/sh", {st_mode=S_IFREG|0755, st_size=686520, ...}) = 0
    > 28 access("/bin/sh", X_OK) = 0
    > 29 stat64("/bin/sh", {st_mode=S_IFREG|0755, st_size=686520, ...}) = 0
    > 30 access("/bin/sh", X_OK) = 0
    > 31 open("temp.txt", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666

    What's the 32nd?
    --
    WW
     
    Wade Ward, May 2, 2007
    #8
  9. vikrant

    vikrant Guest

    On May 2, 11:23 am, Joe Smith <> wrote:
    > vikrant wrote:
    > > the code is a part of a
    > > function,which called again and again.So,i thought that opening and
    > > closing a file on each call may effect the performance.That was the
    > > only reason of using the system command.
    > > Comments are most welcome.

    >
    > FYI: Invoking system() like that means 32 separate file operations each
    > time it is called.
    >
    > linux% strace -f -e trace=file sh -c 'echo foo > temp.txt' |& cat -n
    > 1 execve("/bin/sh", ["sh", "-c", "echo foo > temp.txt"], [/* 41 vars */]) = 0
    > 2 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
    > 3 open("/etc/ld.so.cache", O_RDONLY) = 3
    > 4 fstat64(3, {st_mode=S_IFREG|0644, st_size=79328, ...}) = 0
    > 5 open("/lib/libtermcap.so.2", O_RDONLY) = 3
    > 6 fstat64(3, {st_mode=S_IFREG|0755, st_size=12924, ...}) = 0
    > 7 open("/lib/libdl.so.2", O_RDONLY) = 3
    > 8 fstat64(3, {st_mode=S_IFREG|0755, st_size=16244, ...}) = 0
    > 9 open("/lib/libc.so.6", O_RDONLY) = 3
    > 10 fstat64(3, {st_mode=S_IFREG|0755, st_size=1485672, ...}) = 0
    > 11 open("/dev/tty", O_RDWR|O_NONBLOCK|O_LARGEFILE) = 3
    > 12 open("/usr/lib/locale/locale-archive", O_RDONLY|O_LARGEFILE) = 3
    > 13 fstat64(3, {st_mode=S_IFREG|0644, st_size=49641600, ...}) = 0
    > 14 open("/etc/mtab", O_RDONLY) = 3
    > 15 fstat64(3, {st_mode=S_IFREG|0644, st_size=414, ...}) = 0
    > 16 open("/proc/meminfo", O_RDONLY) = 3
    > 17 fstat64(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
    > 18 stat64("/home/jms", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
    > 19 stat64(".", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
    > 20 open("/usr/lib/gconv/gconv-modules.cache", O_RDONLY) = 3
    > 21 fstat64(3, {st_mode=S_IFREG|0644, st_size=22294, ...}) = 0
    > 22 open("/usr/lib/gconv/ISO8859-1.so", O_RDONLY) = 3
    > 23 fstat64(3, {st_mode=S_IFREG|0755, st_size=5340, ...}) = 0
    > 24 stat64(".", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
    > 25 stat64("/usr/bin/sh", 0xbff56eb4) = -1 ENOENT (No such file or directory)
    > 26 stat64("/usr/sbin/sh", 0xbff56eb4) = -1 ENOENT (No such file or directory)
    > 27 stat64("/bin/sh", {st_mode=S_IFREG|0755, st_size=686520, ...}) = 0
    > 28 access("/bin/sh", X_OK) = 0
    > 29 stat64("/bin/sh", {st_mode=S_IFREG|0755, st_size=686520, ...}) = 0
    > 30 access("/bin/sh", X_OK) = 0
    > 31 open("temp.txt", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666


    Once again thanks for providing this valuable information.i shall try
    to follow the above procedure to get the behavior of the code.

    Regards,
    vikrant
     
    vikrant, May 17, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,601
    Victor Bazarov
    Feb 17, 2005
  2. Replies:
    3
    Views:
    13,385
  3. Eric Layman
    Replies:
    3
    Views:
    652
    Rad [Visual C# MVP]
    Apr 14, 2007
  4. Evan
    Replies:
    1
    Views:
    415
    Marc 'BlackJack' Rintsch
    Nov 4, 2008
  5. Evan
    Replies:
    3
    Views:
    546
    Marc 'BlackJack' Rintsch
    Nov 4, 2008
Loading...

Share This Page