Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

help pulling data string using Awk

Posted on 2016-09-12
9
Medium Priority
?
117 Views
Last Modified: 2016-09-13
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/XSTORE/ELC/ABC_xyz_7.1.4.17_1.12.3-installx.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.
0
Comment
Question by:atorex
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 41794877
You could try "basename", a utility exactly meant for that purpose:

find /mnt/ABC_POOL/ABC_BUILD/ -type f -name .zip -prune -o -mmin -5 -type f -print | while read file
  do
    echo $(basename $file)
  done

Open in new window


If the result of the find command is always just one filename you could put it like this:

FILE=$(basename $(find /mnt/ABC_POOL/ABC_BUILD/ -type f -name .zip -prune -o -mmin -5 -type f -print))

Open in new window

0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 41794886
Or simpler with xargs:

find /mnt/ABC_POOL/ABC_BUILD/ -type f -name .zip -prune -o -mmin -5 -type f -print |xargs -i{} basename {}

Open in new window

0
 

Author Comment

by:atorex
ID: 41794891
Awesome, that works perfectly.

much appreciated.

Regards,
Atorex
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 41794896
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]}'

Open in new window

0
 

Author Comment

by:atorex
ID: 41796594
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
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 41796664
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 .
0
 

Author Comment

by:atorex
ID: 41796676
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/newfile

so the below should work?

echo $FILE | tr " " "\n" > /mnt/ABC_POOL/ABC_BUILD/newfile
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 41796684
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

Open in new window

0
 

Author Comment

by:atorex
ID: 41796701
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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question