-2

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

1 Answer 1

3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.