简体   繁体   English

使用 grep 提取单引号之间的路径

[英]extract path between single quotes using grep

I'm using wget to download files and during the process, I save log messages (see below) for later use.我正在使用 wget 下载文件,在此过程中,我会保存日志消息(见下文)以供以后使用。 The most important part is this line Saving to: '/path/somefile.gz' .最重要的部分是这一行Saving to: '/path/somefile.gz'

I figured out, how I can extract this snipped using grep Saving .我想通了,如何使用grep Saving提取此片段。
Now, my question is: how can I extract just the path between the single quotes?现在,我的问题是:如何仅提取单引号之间的路径? '/path/somefile.gz' => /path/somefile.gz

HTTP request sent, awaiting response... 200 OK
Length: 15391 (15K) [application/octet-stream]
Saving to: ‘/path/somefile.gz’

     0K .......... .....                                      100% 79,7M=0s

2020-07-06  - ‘/path/somefile.gz’ saved [15391/15391]


Total wall clock time: 0,1s
Downloaded: 1 files, 15K in 0s (79,7 MB/s)

EDIT编辑

Is there any way to process it already in this form?有没有办法以这种形式处理它?

wget -m --no-parent -nd https://someurl/somefile.gz -P ~/src/  2>&1 |
grep Saving |
tee ~/src/log.txt 

Thank you in advance!先感谢您!

Sample output from wget :来自wget的示例 output :

$ cat wget.out
HTTP request sent, awaiting response... 200 OK
Length: 15391 (15K) [application/octet-stream]
Saving to: '/path/somefile.gz'

     0K .......... .....                                      100% 79,7M=0s

2020-07-06  - 'path/somefile.gz' saved [15391/15391]


Total wall clock time: 0,1s
Downloaded: 1 files, 15K in 0s (79,7 MB/s)

One awk solution to extract the desired path/file:一种用于提取所需路径/文件的awk解决方案:

$ awk -F"'" '                        # define input delimiter as single quote
/Saving to:/   { print $2 }          # if line contains string "Saving to:" then print 2nd input field
' wget.out                           # our input
/path/somefile.gz                    # our output

To save the above to a variable:要将以上内容保存到变量中:

$ wget_path=$(awk -F"'" '/Saving to:/ {print $2}' wget.out)
$ echo "${wget_path}"
/path/somefile.gz

Following up on OP's edit to the question... piping the output of wget into the awk solution:跟进 OP 对问题的编辑...将wget的 output 输送到awk解决方案中:

wget -m --no-parent -nd https://someurl/somefile.gz -P ~/src/ 2>&1 | awk -F"'" '/Saving to:/ {print $2}' | tee ~/src/log.txt 

Since the question asks for a solution in grep , a single GNU grep command to extract the specified path could be:由于问题要求grep中的解决方案,因此提取指定路径的单个 GNU grep命令可能是:

grep -Po "^Saving to: .\\K[^']*"

provided the Perl Regular Expressions are implemented in the grep (not all grep s implement those).前提是 Perl 正则表达式在grep中实现(并非所有grep都实现了这些)。

Of course, it can be used in a pipe also:当然,它也可以在 pipe 中使用:

wget_command | grep -Po "^Saving to: .\\K[^']*" | tee log.txt

Note that I used a single quote ( ' ) character to anchor the end of path in the pattern match expression, but in the question, Unicode Character Left Single Quotation Mark (U+2018) ( ' ) and Unicode Character Right Single Quotation Mark (U+2019) ( ' ) are used in the sample input.请注意,我使用单引号 ( ' ) 字符来锚定模式匹配表达式中的路径末尾,但在问题中,Unicode 字符左单引号 (U+2018) ( ' ) 和 Unicode 字符右单引号 ( U+2019) ( ' ) 用于样本输入。 If this is really intended then just replace the [^'] with the [^'] in the pattern match expression above.如果这确实是有意的,那么只需在上面的模式匹配表达式中将[^'] [^']

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

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