I'm using wget to download files and during the process, I save log messages (see below) for later use. The most important part is this line Saving to: '/path/somefile.gz'
.
I figured out, how I can extract this snipped using 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
:
$ 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 -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:
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 -Po "^Saving to: .\\K[^']*"
provided the Perl Regular Expressions are implemented in the grep
(not all grep
s implement those).
Of course, it can be used in a pipe also:
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. If this is really intended then just replace the [^']
with the [^']
in the pattern match expression above.
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.