我下面有这样一个句子
/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把目标存档。
发布于 2014-11-24 09:17:40
简单的字符串替换就可以了:
#!/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.chttps://stackoverflow.com/questions/27101042
复制相似问题