Home > Back-end >  linux command to fetch path of S3
linux command to fetch path of S3

Time:08-19

I have S3 bucket path like

s3://my-dev-s3/apyong/output/public/file3.gz.tar

I want to fetch all characters after first "/" (not //) and before last "/" . So here output should be

apyong/output/public

I tried awk -F'/' '{print $NF}' .But its not producing correct results.

CodePudding user response:

Here is sed solution:

s='s3://my-dev-s3/apyong/output/public/file3.gz.tar'
sed -E 's~.*//[^/]*/|/[^/]*$~~g' <<< "$s"

apyong/output/public

Pure bash solution:

s='s3://my-dev-s3/apyong/output/public/file3.gz.tar'
r="${s#*//*/}"
echo "${r%/*}"

apyong/output/public

CodePudding user response:

1st solution: With GNU grep you could use following solution. Using -oP options with GNU grep will make sure it prints only matched values and enables PCRE regex respectively. Then in main grep code using regex ^.*?\/\/.*?\/\K(.*)(?=/)(explained following) to fetch the desired outcome.

var="s3://my-dev-s3/apyong/output/public/file3.gz.tar"
grep -oP '^.*?\/\/.*?\/\K(.*)(?=/)' <<<"$var"

apyong/output/public

Explanation: Adding detailed explanation for used regex.

^.*?\/\/  ##From starting of value performing a lazy match till // here.
.*?\/     ##again performing lazy match till single / here.
\K        ##\K will make sure previous values are forgotten in order to print only needed values.
(.*)(?=/) ##This is greedy match to get everything else as per requirement.


2nd solution: Using GNU awk please try following code. Using capturing group capability of GNU awk here to store values captured by regex in array arr to be used later on in program.

awk 'match($0,/^[^:]*:\/\/[^/]*\/(.*)\//,arr){print arr[1]}' <<<"$var"


3rd solution: awk's match match solution here to get the required output.

awk 'match($0,/^[^:]*:\/\/[^/]*\//) && match(val=substr($0,RSTART RLENGTH),/^.*\//){
  print substr(val,RSTART,RLENGTH-1)
}
' <<<"$var"

CodePudding user response:

Using awk:

awk -F "/" '{ for(i=4;i<=NF;i  ) { if (i==4) { printf "%s",$i } else { printf "/%s",$i } }}' <<< "s3://my-dev-s3/apyong/output/public/file3.gz.tar"

Set the field delimiter to "/" and the look through the 4th to the last field. For the fourth field print the field and for all other field print "/" then the field.

CodePudding user response:

Use coreutils cut:

<<<"s3://my-dev-s3/apyong/output/public/file3.gz.tar" \
cut -d/ -f4-6

Output:

apyong/output/public
  • Related