简体   繁体   English

bash grep regex从字符串的开头到变量

[英]bash grep regex from start of the string to variable

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/ 例如,我需要分配变量:/ 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 ? 哪一个返回000000004802803如何将从根目录到文件夹的所有路径分配给另一个变量? I think it can be done by regex, from the start of the string to variable $zip_folder_name. 我认为可以通过正则表达式来完成,从字符串的开头到变量$ 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. UPD1只是忘了提一下,带数字的文件夹之后可以是另一个文件夹,例如/tmp/gfh/000000004802803/blablabla/EngID_Consolidation.zip而且我需要/ tmp / gfh / 000000004802803 /准确地放入变量。

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** 以下是一个脚本,它将输入参数输入$IN_STR并吐出您需要的字符串**

#!/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 grep -P :Perl表达式
-o : only matching -o :仅匹配
.* : Repeat everything .* :重复一切
(?=) : Query string (?=) :查询字符串
[0-9] : any number [0-9] :任意数字
{15} : 15 chars {15} :15个字符

**From comments, the requirement was like: Path up until the folder name containing exactly 15 digits, including the name of that folder itself. **从注释开始,要求是这样的:直到文件夹名称正好包含15个数字的路径,包括该文件夹本身的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM