我得到了如下句子
/home/kernel/drivers/hal
/home/kernel/drivers/hal
/home/kernel/drivers/sdk
/home/kernel/drivers/sdk/systems/linux/kernel/common
/home/kernel/drivers/sdk/src
/home/kernel/drivers/sdk/build/linux/tar/soc/
src/soc/libsoc.c
/home/kernel/drivers/sdk/build/linux/src/soc/phy/
mt/soc/phy/x1.c
mt/soc/phy/x2.c
mt/soc/phy/x3.c
mt/soc/phy/x4.c
mt/soc/phy/x5.c
mt/soc/phy/x6.c
mt/soc/phy/x7.c
我的目标是在下面做这样的
/home/kernel/drivers/sdk/build/src/soc/libsoc.c
/home/kernel/drivers/sdk/build/mt/soc/phy/x1.c
/home/kernel/drivers/sdk/build/mt/soc/phy/x2.c
/home/kernel/drivers/sdk/build/mt/soc/phy/x3.c
/home/kernel/drivers/sdk/build/mt/soc/phy/x4.c
/home/kernel/drivers/sdk/build/mt/soc/phy/x5.c
/home/kernel/drivers/sdk/build/mt/soc/phy/x6.c
/home/kernel/drivers/sdk/build/mt/soc/phy/x7.c
归档目标。
我在互联网上阅读的文章很多,包括stackoverflow ...但未能得到它。
在Bash中
我尝试使用bash内置表达式
${parameter%word}
${parameter%%word}
Remove matching suffix pattern.
我得到了这个结果
/home/kernel/sdk/
在awk中
我并非100%确定此代码适用于我的意图
awk -F/ '{ for(i=1;i<=NF;i++)
{
room[i]=$i
};
i=1;
while(i<=NF)
{
if(room[i] == "sdk") {
j=i;
i=1;
while(i<=j)
{
print $i
i++
}
}
else {
j=i;
i=1;
while(i<=j)
{
print $i
i++
}
}
}
;getline;
}'
结果是
home
kernel
drivers ....
答案 0 :(得分:1)
简单字符串替换将执行:
#!/bin/bash
path=/home/test/kernel/drivers/middle/sdk/build/linux/src/soc/phy/test.c
ffname="${path##*/}" # filename component
echo "newpath: ${path//\/build*/}/${ffname}"
<强>输出:强>
newpath: /home/test/kernel/drivers/middle/sdk/test.c