I have been using the following snippet pulled from a similar post - https://unix.stackexchange.com/a/101273/212793 - to get a filename from a tar.gz
file:
tar tzf "archive.tar.gz" | awk -F/ '{ if($NF != "") print $NF }'
For my case, I only need one specific file, so I use something like:
tar tzf "archive.tar.gz" | awk -F/ '{ if($NF != "") print $NF }' | grep -e "^..*my-file-name\\.ext$"
The key part is that, my .tar.gz
is very big, and contains a lot of files. However, each one has a similar "hash" appended to the beginning (hence the ^..*
part of my grep'd regex).
So the files might look like:
- 4b77e4e1_file-a.ext
- 4b77e4e1_file-b.ext
- 4b77e4e1_file-c.ext
# ect.
I noticed the command to get all the files (tar tzf "archive.tar.gz" | awk -F/ '{ if($NF != "") print $NF }'
) streams the output.
My thought is, if I could "break" the stream, then extract that first hash part, I could build my filename that I'd eventually need without having to loop through the entire contents of the .tar.gz
file.
So my question is, how can I "break" .awk
on its first output, as opposed to waiting for the whole command to finish (which takes several minutes) and grepping the result to get the filename I eventually want
EDIT: Looks like I actually want to break tar
, as simply exit
ing after the first result doesn't change the execution time.
expect
to solve your problem : let it spawn the tar and ask it to print the relevant information and close the process once such information is found. – Aaron Feb 22 '17 at 17:23