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
Eg 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, eg /tmp/gfh/000000004802803/blablabla/EngID_Consolidation.zip And I need exactly /tmp/gfh/000000004802803/ into variable.
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/
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.