简体   繁体   中英

How to split/cut till specific path (directory path )of string in perl

Input : "/abcd/prod/Cid/1234/Did" or "/abcd/prod/Cid/1234/Did/Pid/234"

Output: "/abcd/prod/Cid/1234/" (Only till 4th word, considering '/' is the delimiter)

How to slice and get above using perl

As one-liner :

$ perl -F'/' -lane 'print join "/", @{F[0..4]}' <<< "/abcd/prod/Cid/1234/Did"

As a script :

while (<>) {
    chomp $_;
    my @F = split(m[/], $_, 0);
    print join('/', @F[0..4]), "\n";
}

Output :

/abcd/prod/Cid/1234

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM