Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
-rw-rw---- 1 test test 2809 Feb 11 19:05 /abc/xyz/test_sys/bin/sys.d/ tc1_0.sh I want only /abc/xyz/test_sys/bin/sys.d/tc1_0.sh How can do that ???...
  1. #1
    Linux Newbie
    Join Date
    Jan 2008
    Posts
    114

    pattern searching in perl ?

    -rw-rw---- 1 test test 2809 Feb 11 19:05 /abc/xyz/test_sys/bin/sys.d/tc1_0.sh


    I want only /abc/xyz/test_sys/bin/sys.d/tc1_0.sh How can do that ???

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Assuming none of your file names contain spaces, do this:
    Code:
    $string=~s/^.* //;
    Otherwise, you can go a bit more complex:
    Code:
    $string=~s/^.*?\:\d\d //;
    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Linux Newbie
    Join Date
    Jan 2008
    Posts
    114
    its working fine for all except for below mentioned line it does not work

    -rw-rw---- 1 test test 1246 Feb 6 19:05 /abc/xyz/pt_100.sh


    Can you suggest me some doc for patten searching.

    thanks

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    its working fine for all except for below mentioned line it does not work

    -rw-rw---- 1 test test 1246 Feb 6 19:05 /abc/xyz/pt_100.sh
    It worked for me. What happens when you run this script? (You may have to change the first line if Perl on your system is elsewhere.)
    Code:
    #!/usr/bin/perl
    
    $string1=<DATA>;
    chomp($string1);
    $string2=$string1;
    $string1=~s/^.* //;
    $string2=~s/^.*?\:\d\d //;
    
    print("$string1\n$string2\n");
    
    __END__
    -rw-rw---- 1 test test 1246 Feb 6 19:05 /abc/xyz/pt_100.sh
    When I run it, I get as output
    Code:
    /abc/xyz/pt_100.sh
    /abc/xyz/pt_100.sh
    Can you suggest me some doc for patten searching.
    perlrequick - perldoc.perl.org
    perlretut - perldoc.perl.org
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    For what it's worth, this doesn't even require a regular expression. This can be easily done with the split operator:
    Code:
    alex@danu ~/test/perl $ cat split_ls 
    #!/usr/bin/perl -w
    
    # if we use split's handy implicit variable...
    $_ = "-rw-rw---- 1 test test 1246 Feb 6 19:05 /abc/xyz/pt_100.sh";
    $name = (split)[-1];
    
    print "$name\n";
    
    # otherwise...
    $line = "-rw-rw---- 1 test test 1246 Feb 6 19:05 /abc/xyz/pt_100.sh";
    $name = (split / /, $line)[-1];
    
    print "$name\n";
    alex@danu ~/test/perl $ ./split_ls 
    /abc/xyz/pt_100.sh
    /abc/xyz/pt_100.sh
    split produces an array, and I use the index "-1" to get the last element in that array. Note that this does not work if there is a space in the filename.
    DISTRO=Arch
    Registered Linux User #388732

  6. #6
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Code:
    $s="-rw-rw---- 1 test test 2809 Feb 11 19:05 /abc/xyz/test_sys/bin/sys.d/tc1_0.sh";
    print substr($s,index($s,"/"));

  7. #7
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Quote Originally Posted by ghostdog74 View Post
    Code:
    $s="-rw-rw---- 1 test test 2809 Feb 11 19:05 /abc/xyz/test_sys/bin/sys.d/tc1_0.sh";
    print substr($s,index($s,"/"));
    That is beautiful. I never even considered doing that.
    DISTRO=Arch
    Registered Linux User #388732

  8. #8
    Linux Newbie
    Join Date
    Jan 2008
    Posts
    114
    Great help, thanks all

  9. #9
    Linux Newbie
    Join Date
    Jan 2008
    Posts
    114
    is there any way to get the data between two words e.g.

    REV: THIS IS LIux~#X VERSION: 4.0

    not I just want "THIS IS LIux~#X"

    and "4.0"
    separately.
    Switched to Scripting

  10. #10
    Linux Newbie
    Join Date
    Jan 2008
    Posts
    114
    s there any way to get the data between two words e.g.

    REV: THIS IS LIux~#X VERSION: 4.0

    not I just want "THIS IS LIux~#X"

    and "4.0"
    Switched to Scripting

Page 1 of 2 1 2 LastLast

Posting Permissions

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