2014-11-24 29 views
0

我下面在BASH和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 

要存檔的目標。

  1. 我閱讀互聯網上的文章很多包括stackoverflow ...但未能得到它。

  2. 在bash

    我試圖使用bash內置的表達

    ${parameter%word} 
    ${parameter%%word} 
    Remove matching suffix pattern. 
    

    我得到這個結果

    /home/kernel/sdk/ 
    
  3. 在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

我很抱歉沒有清楚地說清楚,但正如我所看到的只是目錄名 – heaven4us 2014-11-24 09:05:40

+0

@ heaven4us解釋了模式是什麼,你在尋找sdk和它之前的一切嗎? – 2014-11-24 09:17:23

+0

@ heaven4us一個例子是不夠的。提供更多的行數以及預期的輸出。 – 2014-11-24 09:17:49

回答

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 
相關問題