atorex
asked on
help pulling data string using Awk
I'm working on a script to locate newly changed files and send out an email, I need help pulling just the file name from the resulting find command.
my find command is-
find /mnt/ABC_POOL/ABC_BUILD/ -type f -name .zip -prune -o -mmin -5 -type f -print
the result from the above would be something like this
/mnt/ABC_POOL/ABC_BUILD/XS TORE/ELC/A BC_xyz_7.1 .4.17_1.12 .3-install x.zip
I would like to use just the zip file name as my final result by using awk or sed to remove the path and leave just the file name, keeping in mind that the full path is not always the same.
I'm hoping there is a way to remove everything from start of string leaving the last part after the last / that would be the file name.
Any idea what would be the best way, I attempted awk -F"\t" but did not work.
my find command is-
find /mnt/ABC_POOL/ABC_BUILD/ -type f -name .zip -prune -o -mmin -5 -type f -print
the result from the above would be something like this
/mnt/ABC_POOL/ABC_BUILD/XS
I would like to use just the zip file name as my final result by using awk or sed to remove the path and leave just the file name, keeping in mind that the full path is not always the same.
I'm hoping there is a way to remove everything from start of string leaving the last part after the last / that would be the file name.
Any idea what would be the best way, I attempted awk -F"\t" but did not work.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome, that works perfectly.
much appreciated.
Regards,
Atorex
much appreciated.
Regards,
Atorex
You can of course use "awk", because "awk" can do just everything:
find /mnt/ABC_POOL/ABC_BUILD/ -type f -name .zip -prune -o -mmin -5 -type f -print | awk '{F=split($0,A,"/"); print A[F]}'
ASKER
I have completed my script, thanks to you woolmilkporc, I have one issue I just noticed, the output is listing all files in sequence with a space separating each file, is there a way to have the output of the command I'm using to list it all like ls -l would in a column?
here is my command as a variable, I use the output of the variable to email the list of files.
FILE=$(basename $(find /mnt/ABC_POOL/ABC_BUILD/ -type f -newerct '5 minute ago' -print))
Regards,
Atorex
here is my command as a variable, I use the output of the variable to email the list of files.
FILE=$(basename $(find /mnt/ABC_POOL/ABC_BUILD/ -type f -newerct '5 minute ago' -print))
Regards,
Atorex
How do you use the variable?
If it's something like
echo $FILE | mailx ... ...
you can try
echo $FILE | tr " " "\n" | mailx... ...
If you're using a different approach please let me know .
If it's something like
echo $FILE | mailx ... ...
you can try
echo $FILE | tr " " "\n" | mailx... ...
If you're using a different approach please let me know .
ASKER
yes I'm using echo $FILE in to a temp file to be used for the body of the email like.
echo $FILE > /mnt/ABC_POOL/ABC_BUILD/ne wfile
so the below should work?
echo $FILE | tr " " "\n" > /mnt/ABC_POOL/ABC_BUILD/ne wfile
echo $FILE > /mnt/ABC_POOL/ABC_BUILD/ne
so the below should work?
echo $FILE | tr " " "\n" > /mnt/ABC_POOL/ABC_BUILD/ne
Yes, but if you want the output to go to a file you can simply do this:
find /mnt/ABC_POOL/ABC_BUILD/ -type f -newerct '5 minute ago' -print | xargs -n1 basename > /mnt/ABC_POOL/ABC_BUILD/newfile
ASKER
Ok, i'm using the variable to validate if a file is found in an if statement, the validation I'm using is
if [ -s $FILE ] and has been working so far.
Thanks for your help, trying to better my limited bash scripting capabilities.
Regards,
Atorex
if [ -s $FILE ] and has been working so far.
Thanks for your help, trying to better my limited bash scripting capabilities.
Regards,
Atorex
Open in new window
If the result of the find command is always just one filename you could put it like this:
Open in new window