Find the answer to your Linux question:
Results 1 to 6 of 6
I'm trying to add a substitute operator to change the value STRING: "Sat May 18 13:58:34 2013" into Sat May 18 13:58:34 2013 (keep in mind, this date isn't static. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2013
    Posts
    17

    Post Having Trouble displaying the output to the file


    I'm trying to add a substitute operator to change the value STRING: "Sat May 18 13:58:34 2013" into Sat May 18 13:58:34 2013 (keep in mind, this date isn't static. But changes frequently) using the substitute operator in Perl.

    Here is the command to summon the String value from the server: -
    Code:
    snmpget -c ICT338 -v2c -Ov  tcp:115.146.85.10:1161 iso.3.6.1.4.1.2021.100.4.0
    Output: -
    Code:
    STRING: "Sat May 18 13:58:34 2013"
    I want to change this active Server Date String to this: -

    Code:
    Sat May 18 13:58:34 2013
    OR
    Sat May 18 2013 13:58:34
    Perl Script: -
    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    my($FILE_N, $Substitute);
    
    $FILE_N = "Test";
    open (FILE, ">>$FILE_N\.log") || die"\n Unable to create the file\.log";
    
    $Substitute = `snmpget -c ICT338 -v2c -Ov  tcp:115.146.85.10:1161 iso.3.6.1.4.1.2021.100.4.0`;
    
    $Substitute =~ s/\w\w\w\w\w\w\:\s\"\w\w\w\s\w\w\w\s\d\d\s\d\d\:\d\d\:\d\d\s\d\d\d\d\"/\w\w\w\s\w\w\w\s\d\d\s\d\d\:\d\d\:\d\d\s\d\d\d\d\ /;
    
    print FILE "$Substitute\n";
    
    close FILE;
    But it gives me this output instead: -
    Code:
    Unrecognized escape \w passed through at ./Test1.pl line 12.
    Unrecognized escape \w passed through at ./Test1.pl line 12.
    Unrecognized escape \w passed through at ./Test1.pl line 12.
    Unrecognized escape \s passed through at ./Test1.pl line 12.
    Unrecognized escape \w passed through at ./Test1.pl line 12.
    Unrecognized escape \w passed through at ./Test1.pl line 12.
    Unrecognized escape \w passed through at ./Test1.pl line 12.
    Unrecognized escape \s passed through at ./Test1.pl line 12.
    Unrecognized escape \d passed through at ./Test1.pl line 12.
    Unrecognized escape \d passed through at ./Test1.pl line 12.
    Unrecognized escape \s passed through at ./Test1.pl line 12.
    Unrecognized escape \d passed through at ./Test1.pl line 12.
    Unrecognized escape \d passed through at ./Test1.pl line 12.
    Unrecognized escape \d passed through at ./Test1.pl line 12.
    Unrecognized escape \d passed through at ./Test1.pl line 12.
    Unrecognized escape \d passed through at ./Test1.pl line 12.
    Unrecognized escape \d passed through at ./Test1.pl line 12.
    Unrecognized escape \s passed through at ./Test1.pl line 12.
    Unrecognized escape \d passed through at ./Test1.pl line 12.
    Unrecognized escape \d passed through at ./Test1.pl line 12.
    Unrecognized escape \d passed through at ./Test1.pl line 12.
    Unrecognized escape \d passed through at ./Test1.pl line 12.
    Any ideas would be appreciated. Thank you .
    Last edited by Khrz; 05-18-2013 at 06:42 AM.

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    478
    saving the snmpget output in a file T say,
    then something like this might work:

    cut -b10-33 <T
    the sun is new every day (heraclitus)

  3. #3
    Just Joined!
    Join Date
    May 2013
    Posts
    17
    Quote Originally Posted by tpl View Post
    saving the snmpget output in a file T say,
    then something like this might work:

    cut -b10-33 <T
    Thanks a lot man. I really appreciated the effort you've placed on helping me .
    But just to be sure that it works on the other values that does the same on what I've just mentioned to see whether it works or not. Then I'll consider this topic solved .

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    May 2013
    Posts
    17
    Quote Originally Posted by tpl View Post
    saving the snmpget output in a file T say,
    then something like this might work:

    cut -b10-33 <T
    For that part. I'm able to cut through the text anywhere from Left to right. But i have another question, Am i able to cut the text from Right to left as well?

  6. #5
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,294
    Hi.

    For the file data1:
    Code:
    STRING: "Sat May 18 13:58:34 2013"
    here's a short perl solution.
    Code:
    #!/usr/bin/env perl
    
    # @(#) p1	Demonstrate use of parts of a split string.
    
    use strict;
    use warnings;
    
    #0       1    2   3  4        5
    #STRING: "Sat May 18 13:58:34 2013";
    my($t1) = <>;
    chomp($t1);;
    $t1 =~ s/["]//g;
    my(@a) = (split(/ /,$t1))[5,1,2,3,4];
    
    print "@a\n";
    
    exit(0);
    producing:
    Code:
    % ./p1 data1
    2013 Sat May 18 13:58:34
    I am not sure why you want right-to-left, but here's an awk solution:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate field splitting with [g]awk.
    
    pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
    db() { : ; }
    C=$HOME/bin/context && [ -f $C ] && $C awk
    
    FILE=${1-data1}
    pl " Input data file $FILE:"
    head $FILE
    
    pe
    awk -F'[" ]' '
    	{ print " Whole line |" $0 "|" }
    	{ print " Number of fields:", NF }
    	{ print " First  field: " $1 }
    	{ print " Second field: " $2 }
    	{ print " Third  field: " $3 }
    	{ print " last-1 field: " $(NF-1) }
    	{ print $(NF-1),$3,$4,$5,$6 }
    	{ print $(NF-1),$(NF-5),$(NF-4),$(NF-3),$(NF-2) }
    ' $FILE
    
    exit 0
    producing:
    Code:
    % ./s1
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
    bash GNU bash 3.2.39
    awk GNU Awk 3.1.5
    
    -----
     Input data file data1:
    STRING: "Sat May 18 13:58:34 2013"
    
     Whole line |STRING: "Sat May 18 13:58:34 2013"|
     Number of fields: 8
     First  field: STRING:
     Second field: 
     Third  field: Sat
     last-1 field: 2013
    2013 Sat May 18 13:58:34
    2013 Sat May 18 13:58:34
    Experiment with printing the fields in any order you wish. See man pages for details.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  7. #6
    Just Joined!
    Join Date
    May 2013
    Posts
    17
    Quote Originally Posted by drl View Post
    Hi.

    For the file data1:
    Code:
    STRING: "Sat May 18 13:58:34 2013"
    here's a short perl solution.
    Code:
    #!/usr/bin/env perl
    
    # @(#) p1	Demonstrate use of parts of a split string.
    
    use strict;
    use warnings;
    
    #0       1    2   3  4        5
    #STRING: "Sat May 18 13:58:34 2013";
    my($t1) = <>;
    chomp($t1);;
    $t1 =~ s/["]//g;
    my(@a) = (split(/ /,$t1))[5,1,2,3,4];
    
    print "@a\n";
    
    exit(0);
    producing:
    Code:
    % ./p1 data1
    2013 Sat May 18 13:58:34
    I am not sure why you want right-to-left, but here's an awk solution:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate field splitting with [g]awk.
    
    pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
    db() { : ; }
    C=$HOME/bin/context && [ -f $C ] && $C awk
    
    FILE=${1-data1}
    pl " Input data file $FILE:"
    head $FILE
    
    pe
    awk -F'[" ]' '
    	{ print " Whole line |" $0 "|" }
    	{ print " Number of fields:", NF }
    	{ print " First  field: " $1 }
    	{ print " Second field: " $2 }
    	{ print " Third  field: " $3 }
    	{ print " last-1 field: " $(NF-1) }
    	{ print $(NF-1),$3,$4,$5,$6 }
    	{ print $(NF-1),$(NF-5),$(NF-4),$(NF-3),$(NF-2) }
    ' $FILE
    
    exit 0
    producing:
    Code:
    % ./s1
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
    bash GNU bash 3.2.39
    awk GNU Awk 3.1.5
    
    -----
     Input data file data1:
    STRING: "Sat May 18 13:58:34 2013"
    
     Whole line |STRING: "Sat May 18 13:58:34 2013"|
     Number of fields: 8
     First  field: STRING:
     Second field: 
     Third  field: Sat
     last-1 field: 2013
    2013 Sat May 18 13:58:34
    2013 Sat May 18 13:58:34
    Experiment with printing the fields in any order you wish. See man pages for details.

    Best wishes ... cheers, drl
    No worries , I'll get to test that right away. I'll let you know if I've stumbled up with more obstacles on my way(I might need error handling for this script later on as well)

Posting Permissions

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