[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 446
  • Last Modified:

Complex UNIX Script question?

Hi,
I have written a UNIX script (KSH) that ftp's filenames to a remote server and calculates what numeric directory the file needs to be transfered to via an algorithm. The filename iteself has 7 fields
(i.e. '123456 G462 NOTE 0002598503 V01 DT 20060725121249.txt') - note 7 fields in filename.

The first field of the filename should ALWAYS be numeric (i.e. 123456), sometimes what happens is that the users generate an invalid filename which also includes soemething else other then numeric character in the first field.
(e.g. "A23456 G462 NOTE 0002598503 ..." OR " _MISC G462 NOTE 0002598503 ..." )

Can someone please advsie the best possible way to put in a check to delete any filesnames that isn't numeric in the first field of the filename? In other words the first field of the file name must always be numeric, anything other then that should be delete.

Note that the directory may contain more then a 1000+ files at any given time.

Thanks
Bud.
0
budrito12
Asked:
budrito12
  • 3
  • 2
  • 2
  • +1
1 Solution
 
ozoCommented:
perl -e 'unlink grep !/^\d+\s/,<*.txt> or die $!'
0
 
TintinCommented:
Do the filenames have spaces in them?  If so, then:

#!/bin/ksh

file="ABC23456 G462 NOTE 0002598503 V01 DT 20060725121249.txt"

if [ -n "`echo "$file" | awk '$1 !~ /^[0-9]*$/ {print $1}'`" ]
then
   echo "$file has invalid first field"
fi


0
 
amit_gCommented:
Do you want to check first 6 chars only?

cd TheDir
find . | egrep -v '^\.\/[0-9]{6}'

If that returns only files that should be deleted, remove them by

find . | egrep -v '^\.\/[0-9]{6}' | xargs rm

If you want to check the first field as upto the blank char, you can use

find . | egrep -v '^\.\/[0-9]+ '
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
budrito12Author Commented:
Thanks for your efforts guys. Amit_g if I wanted to move the files to another directory upon detection using your approach say to "error" please you advise of the syntax. By the way your rm command also doesn;t work because the filename has space thus is cannot find the file. Its only picking up the first field.

Ozo dont have perl installed but thanks for your help and Tintin your stuff also works.

thx,
Budrito
0
 
ozoCommented:
find . -type f \! -name '[0-9][0-9][0-9][0-9][0-9][0-9] *' -print0 | xargs -0 rm
0
 
amit_gCommented:
If you want to move the file somewhere else instead of deleting, you can use

find . | egrep -v '^\.\/[0-9]{6}' | xargs -i mv {} TheDirectoryForTheMovedFiles

0
 
budrito12Author Commented:
Ozo, your syntax errors out with the following msg:
find: bad option -print0.....so I removed the print0 option. The rm doesn't work as well with the following error: xargs: unknown option: -0. I'm using HPUX 11i. Might have to explore the man pages.

Amit_g:
Your commands interesting, although it moves the files to the error directory I still get an error after the execution(see below):
find . | egrep -v '^\.\/[0-9]{6}' | xargs -i mv {} /interface/error
mv: .: rename: Invalid argument

Can you please explain why I get this.

Thanks
Bud




0
 
amit_gCommented:
It is trying to move the . (the current diretory). Use -type option

find . -type f | egrep -v '^\.\/[0-9]{6}' | xargs -i mv {} /interface/error
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now