[英]Spliting specific word out in sentence in BASH and AWK?
我下面有一個這樣的句子
/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內置表達式
${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 ....
簡單的字符串替換即可:
#!/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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.