1

I have a file with following strings and need to assign path to folder with digits into variable:

/tmp/gfh/000000004802803/blablabla/EngID_Consolidation.zip
/tmp/vcbn/000000005395825/blablabla/172_6578-DUMP_NOMServer.zip
/tmp/one3/435876dfhg/000000004017051/5.zip
/tmp/one3/dsfkgjh/dsjfhgfd/000000004617319/Sybase.zip

E.g. I need to assign in variable: /tmp/gfh/000000004802803/

Digits and path always different. As a first step I've assigned into variable folder with digits using regex:

zip_folder_name0=$(grep -E -o "/([0]{5}[0-9]{10})/" <<< $zip_path)
zip_folder_name=${zip_folder_name0#"/"}
zip_folder_name=${zip_folder_name%"/"}
echo $zip_folder_name

Which return 000000004802803 How to assign into another variable all path to folder from the root ? I think it can be done by regex, from the start of the string to variable $zip_folder_name. Is this possile? Or maybe there is another way?

UPD1 Just forgot to mention, that after folder with digits can be another folder, e.g. /tmp/gfh/000000004802803/blablabla/EngID_Consolidation.zip And I need exactly /tmp/gfh/000000004802803/ into variable.

3
  • Update question with more details Commented Nov 3, 2016 at 11:49
  • are the digits always the 3rd field ? can there be other folders in the path with only digits ? (i.e. can you have /tmp/gfh/ijk/012345/myzip.zip or /tmp/gfh/0123/4567/myzip.zip ?) Commented Nov 3, 2016 at 12:01
  • There is always folder with 15 digits, but before and after can be folders with mixed digits and letters. No, there is no another folder with STRICTLY 15 digits in name, but can be folders with more or less then 15 digits. E.g. /tmp/gfh/000000004802803/123456/EngID_Consolidation.zip. Commented Nov 3, 2016 at 12:12

2 Answers 2

2

Following is a script that will take an input parameter into $IN_STR and will spit out the string you require**

#!/bin/bash

BASE_REGEX="[0-9]{15}"
IN_STR=$1

FIFTEEN_DIGIT_DIR_NAME=`echo $IN_STR | grep -E -o "$BASE_REGEX"`
FIFTEEN_DIGIT_DIR_CONTAINER=`echo $IN_STR | grep -Po ".*(?=$BASE_REGEX)"`

echo $FIFTEEN_DIGIT_DIR_CONTAINER$FIFTEEN_DIGIT_DIR_NAME

Outputs:

/tmp/gfh/00000000480280

Explanation:
grep -P : Perl expression
-o : only matching
.* : Repeat everything
(?=): Query string
[0-9]: any number
{15} : 15 chars

**From comments, the requirement was like: Path up until the folder name containing exactly 15 digits, including the name of that folder itself.

Sign up to request clarification or add additional context in comments.

6 Comments

Can you modify regex to amend variant with /tmp/gfh/000000004802803/12345 ? (nested folder can contain only digits, but it will not contain strictly 15 digits as parent folder).
Sorry I didn't understand, you don't want the last 12345 to be included in the variable?
Yes, I want to cut off 12345, to have only /tmp/gfh/000000004802803/
oh, ok.. will change the answer, won't be an amendment though, sorry!
Excellent! Thanks for solution and sorry for my bad English :(
|
2
while read line; do        
   dir=$(grep -oE .*/[0-9]+/ <<< $line|tr -d / )
   echo dir=$dir num=$num 
done < zips.txt 

> dir=/tmp/gfh/000000004802803/
> dir=/tmp/vcbn/000000005395825/
> dir=/tmp/one3/435876dfhg/000000004017051/
> dir=/tmp/one3/dsfkgjh/dsjfhgfd/000000004617319/

3 Comments

Might want read -r. And remember quotes around parameter expansions.
Just forgot to mention, that after folder with digits can be another folder, e.g. /tmp/gfh/000000004802803/blablabla/EngID_Consolidation.zip AndI need exactly /tmp/gfh/000000004802803/ into variable. Unfortunately, it's not possible with your answer.
When I have a nested folder after folder with 15 digits it returns dir=/tmp/gfh/000000004802803/blablabla/

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.