- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Scripting issue
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-08-2009 07:31 AM - last edited on тАО11-26-2012 06:55 PM by Maiko-I
тАО01-08-2009 07:31 AM - last edited on тАО11-26-2012 06:55 PM by Maiko-I
Hi All
I am attempting to create a quick and simple skulker script. This is what I intended.
A text file containing some thing similar to
cat HOUSEKEEP_FILES
-------------------------------------------------------------
FileSys Strin ACTION DAYS OLD
-------------------------------------------------------------
/tmp/martin/1 file1001 REMOVE 2
/tmp/martin/1 file1002 REMOVE 2
/tmp/martin/1 file1003 REMOVE 2
/tmp/martin/2 file1004 REMOVE 3
/tmp/martin/2 file1005 REMOVE 3
/tmp/martin/2 file1006 REMOVE 3
I wanted to use a sed and awk command, lets say to fin files older than 2 or 3 ($4) in the directory ($1) the actual file ($2). I dont think I'm to far away bu reading that but the command blurps. By the way I am just trying to list initially. The command I am using is,
sed -n '/REMOVE/p' HOUSEKEEP_FILES | awk '{system(find $1, " " -name $2," " -print)}'
is there any awk gurus who can simplify this?
thanks very much
Martin
P.S. this thread has been moved from HP-UX > System Administration to HP-UX > languages - HP Forums Moderator
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-08-2009 07:48 AM
тАО01-08-2009 07:48 AM
Re: Scripting issue
egrep "REMOVE" HOUSEKEEP_FILES | while read DIR FILE ACTION DAYS_OLD ; do
[[ $DAYS_OLD -gt 2 ]] && find $DIR -name "$FILE" -print
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-08-2009 08:17 AM
тАО01-08-2009 08:17 AM
Re: Scripting issue
For example, when receiving the first non-header line of HOUSEKEEP_FILES, your script would not remove only:
/tmp/martin/1/file1001
but also anything matching:
/tmp/martin/1/*/file1001
/tmp/martin/1/*/*/file1001
/tmp/martin/1/*/*/*/file1001
...
(When creating automated file removers, you *must* be as specific and predictable as possible. The hard way of learning this lesson involves restoring files from backups and restarting long-running processing jobs while thinking about how to explain this to an angry customer :-)
If the directory structure is predictable, it would be much safer to directly assemble the name of the file to be removed instead of fishing around with the find command.
If the file paths are of the form "/FileSys/Strin", then you could do something like this:
#!/bin/sh
grep '^/.*REMOVE' HOUSEKEEP_FILES | \
while read FileSys Strin junk; do
echo "rm \"$FileSys/$Strin\""
# rm "$FileSys/$Strin" # uncomment after testing
done
The first grep command passes through only the lines beginning with a '/' character, so we get rid of all the header lines. After that it's just a matter of reading each line, collecting the columns to variables and assembling the necessary command line.
Of course, the above approach is slow and causes excessive forking of new processes.
So, here's version 2:
#!/bin/sh
grep '^/.*REMOVE' HOUSEKEEP_FILES | \
awk '{ print $1 "/" $2 };' | \
xargs echo rm
# Replace "xargs echo rm" with "xargs rm" if OK
Note that there is no loop at all, just a single pipeline. You could even write it as a one-liner, but I think it's easier to understand when written on multiple lines.
MK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-08-2009 08:27 AM
тАО01-08-2009 08:27 AM
Re: Scripting issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-08-2009 08:31 AM
тАО01-08-2009 08:31 AM
SolutionHmm, lot of possibilities.
I was even thinking about generalizing the
script. What if HOUSEKEEP_FILES could contain
different keywords in the third column (not only "REMOVE"). For example:
-------------------------------------------------------------
FileSys Strin ACTION DAYS OLD
-------------------------------------------------------------
/tmp/martin/1 file1001 REMOVE 2
/tmp/martin/1 file1002 REMOVE 2
/tmp/martin/1 file1003 REMOVE 2
/tmp/martin/2 file1004 REMOVE 3
/tmp/martin/2 file1005 KEEP 3
/tmp/martin/2 file1006 REMOVE 3
Here is my little script:
#!/bin/sh
awk '! /---|DAYS OLD/ {print}' HOUSEKEEP_FILES | while read SEARCHDIR FILE ACTION HOWOLD
do
case $ACTION in
REMOVE|remove) ACTION="rm -f" ;;
*) ACTION="ll" ;;
esac
find $SEARCHDIR -name $FILE -type f -mtime $HOWOLD -exec $ACTION {} \;
done
And here is what it would run:
find /tmp/martin/1 -name file1001 -type f -mtime 2 -exec rm -f {} \;
find /tmp/martin/1 -name file1002 -type f -mtime 2 -exec rm -f {} \;
find /tmp/martin/1 -name file1003 -type f -mtime 2 -exec rm -f {} \;
find /tmp/martin/2 -name file1004 -type f -mtime 3 -exec rm -f {} \;
find /tmp/martin/2 -name file1005 -type f -mtime 3 -exec ll {} \;
find /tmp/martin/2 -name file1006 -type f -mtime 3 -exec rm -f {} \;
I am sure there are many more ways to
do it :)
Cheers,
VK2COT
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-08-2009 08:36 AM
тАО01-08-2009 08:36 AM
Re: Scripting issue
it is not clear, in which way you want to use the file name.
I suggest, that if there is no value of 'fil' found in awk, use any filespec found in the list. Else use only the specified filespec.
Wrap my awk in a script of your choice;
start with something like this in the commandline to test your expectations:
#filespec=file1002 # may be set
awk -v age=3 -v dir=/tmp/martin/1 -v fil="$filespec" 'NR<4 {next}
($3 == "REMOVE") && ($4 >= age) && ($1 == dir) { if(fil && $2 != fil) next
sys_str="find "dir" -type f -name "$2" -mtime +"age" -print"
print sys_str
system(sys_str)}' HOUSEKEEP_FILES
You could implement the handling of directory names the same way as I did with the filespec.
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-08-2009 11:38 PM
тАО01-08-2009 11:38 PM
Re: Scripting issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-09-2009 03:12 AM
тАО01-09-2009 03:12 AM
Re: Scripting issue
nice, that we could help you!
Because you are new to this forum - Welcome! - I want to put yout attention to its unique point system:
http://forums11.itrc.hp.com/service/forums/helptips.do?#33
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-09-2009 03:41 AM
тАО01-09-2009 03:41 AM
Re: Scripting issue
We use ZIP as an option too. So 2 day old logs get zipped, then deleted after 7 days.
So you would have:
/tmp/martin/1 file1001 ZIP 2
/tmp/martin/1 file1001 REMOVE 7