输入:"/abcd/prod/Cid/1234/Did"
或"/abcd/prod/Cid/1234/Did/Pid/234"
输出:"/abcd/prod/Cid/1234/"
(仅到第 4 个单词,考虑到"/"是分隔符(
如何使用perl切片并超越
作为单行:
$ perl -F'/' -lane 'print join "/", @{F[0..4]}' <<< "/abcd/prod/Cid/1234/Did"
作为脚本:
while (<>) {
chomp $_;
my @F = split(m[/], $_, 0);
print join('/', @F[0..4]), "n";
}
输出:
/abcd/prod/Cid/1234