【问题标题】:Parsing a path in bash在bash中解析一条路
【发布时间】:2018-03-15 20:57:48
【问题描述】:

我正在尝试从“/home/user/directory/sub”之类的路径中仅选择受参数影响的部分。如果我将脚本称为 ./script 2,它应该返回“/home/user”。

这是我尝试过的:

argument=$1
P=$PWD

verif=`echo "$P" | grep -o "/" | wc -l`

nr=`expr $verif - $argument + 1|bc`  

prints=$(echo {1..$nr})

path=`echo $P | awk -F "/" -v f="$prints" '{print $f}'`
echo $path

我得到了 verif 和 nr 的正确结果,但打印和生成的路径不起作用。

提前致谢

【问题讨论】:

  • 您考虑过使用 AWK OFS 吗?
  • prints=$(echo {1..$nr}) 这条线永远行不通!大括号扩展发生在参数扩展之前。清楚地说明您的输入和预期输出
  • 我打算根据参数创建一个包含“$1$2$3”之类的变量,并将该变量的内容插入到 awk 中,以便仅选择我需要的内容。跨度>

标签: bash parsing awk


【解决方案1】:

如果您需要以脚本的形式使用它,那么以下内容可能会对您有所帮助。

cat script.ksh
var=$1
PWD=`pwd`
echo "$PWD" | awk -v VAR="$var" -F"/" '{for(i=2;i<=(NF-VAR);i++){if($i){printf("%s%s",i==2?"/"$i:$i,i==(NF-VAR)?RS:"/")}}}'

在此处也添加上述解决方案的可读性更好的形式。

cat script.ksh
var=$1
PWD=`pwd`
echo "$PWD" |
awk -v VAR="$var" -F"/" '{
 for(i=2;i<=(NF-VAR);i++){
   if($i){
     printf("%s%s",i==2?"/"$i:$i,i==(NF-VAR)?RS:"/")
}
}
}'

假设我们有以下路径/singh/is/king/test_1/test/test2。因此,当我们运行 script.ksh 时,将会输出以下内容。

./script.ksh 2
/singh/is/king/test_1

代码说明:

cat script.ksh
var=$1                    ##creating a variable named var here which will have very first argument while running the script in it.
PWD=`pwd`                 ##Storing the current pwd value into variable named PWD here.
echo "$PWD" |
awk -v VAR="$var" -F"/" '{##Printing the value of variable PWD and sending it as a standard input for awk command, in awk command creating variable VAR whose value is bash variable named var value. Then creating the field separator value as /
 for(i=2;i<=(NF-VAR);i++){##Now traversing through all the fields where values for it starts from 2 to till value of NF-VAR(where NF is total number of fields value and VAR is value of arguments passed by person to script), incrementing variable i each iteration of for loop.
   if($i){                ##Checking if a variable of $i is NOT NULL then perform following.
     printf("%s%s",i==2?"/"$i:$i,i==(NF-VAR)?RS:"/") ##Printing 2 types of string here with printf, 1st is value of fields(paths actually) where condition I am checking if i value is 2(means very first path) then print / ahead of it else simply print it, now second condition is if i==(NF-VAR) then print a new line(because it means loop is going to complete now) else print /(to make the path with slashes in them).
}
}
}'

【讨论】:

  • 脚本的参数不应该是要解析的路径,而是要返回的目录个数。在我的脚本中,“P”取当前路径的值。当我运行脚本时,如 ./script 2,我希望它从当前路径转到“/home/user/directory/sub”到“/home/user”。
  • 是的,这确实有效。现在我只需要了解您到底做了什么,因为我的 bash 技能还处于开发的早期阶段。谢谢!
  • @DragosCazangiu,您能否查看我的代码部分解释,如果您清楚,请告诉我。
  • 非常感谢您的解释。是的,很清楚。感谢您的努力!
  • @DragosCazangiu,不客气,继续学习,继续分享知识,干杯。
【解决方案2】:

在python中:

#!/usr/bin/env python3

import os
import sys
print('/'.join(os.getcwd().split('/')[:int(sys.argv[1])+1]))

【讨论】:

    猜你喜欢
    • 2017-04-09
    • 2023-04-10
    • 1970-01-01
    • 2011-02-22
    • 1970-01-01
    • 2013-02-20
    • 2012-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多