在BASH和AWK中将特定单词拆分成句子?

在BASH和AWK中将特定单词拆分成句子?,bash,awk,cut,out,Bash,Awk,Cut,Out,我下面有一个这样的句子 /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/drive

我下面有一个这样的句子

/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
我的目标是如下所示

/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;
     }'
    

  • 简单的字符串替换可以:

    #!/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
    

    很抱歉,我没有把它说清楚,但正如我所看到的那样name@heaven4us解释模式是什么,你在搜索sdk和之前的所有东西吗?@heaven4us一个例子是不够的。在预期输出的同时提供更多行数。好的,我将给出句子。这与您之前提出的问题非常相似,没有澄清就离开了: