1

I am trying to select from a path like "/home/user/directory/sub" only a part that is influenced by an argument. If I was to call the script as ./script 2 it should return "/home/user".

Here is what I tried:

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

I get the right results for verif and nr but prints and the path resulted do not work.

Thanks in advance

3
  • Have you considered using AWK OFS? Commented Oct 4, 2017 at 12:50
  • The line prints=$(echo {1..$nr}) would never work! Brace expansion happens before parameter expansion. State your input and expected output clearly Commented Oct 4, 2017 at 12:51
  • What I intended was to create a variable containing something like "$1$2$3", depending on the argument and to insert the content of that variable in awk in order to select only what I needed. Commented Oct 4, 2017 at 12:54

2 Answers 2

1

In case you need to have this in form of script then following may help you in same.

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:"/")}}}'

Adding a better readable form of above solution too here.

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:"/")
}
}
}'

Let's say we have following path /singh/is/king/test_1/test/test2. So when we run the script.ksh following will be the output then.

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

Explanation of code:

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).
}
}
}'
Sign up to request clarification or add additional context in comments.

6 Comments

The argument of the script shouldn't be the path to be parsed, but the number of directories to go back. In my script, "P" takes the value of the current path. When I run the script, as ./script 2, I want it to go from the current path, say "/home/user/directory/sub" to "/home/user".
Yes, this does indeed work. Now I just have to understand what exactly you did, as my bash skills are in the early stages of development. Thank you!
@DragosCazangiu, could you please check my section explanation of code and let me know if this is clear to you.
Thank you very much for that explanation. Yes, it is clear. Thank you for the effort!
@DragosCazangiu, you are welcome, keep learning and keep sharing knowledge, cheers.
|
-1

In python:

#!/usr/bin/env python3

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

2 Comments

The question explicitly asks for a solution in bash. And if someone was looking for a solution in python3, they would not find your answer. Please move it to an appropriate discussion.
@jeannej - The accepted answer uses awk, not bash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.