Solved

Complex UNIX Script question?

Posted on 2007-03-27
8
419 Views
Last Modified: 2013-12-26
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
Comment
Question by:budrito12
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 18804889
perl -e 'unlink grep !/^\d+\s/,<*.txt> or die $!'
0
 
LVL 48

Expert Comment

by:Tintin
ID: 18804900
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
 
LVL 58

Expert Comment

by:amit_g
ID: 18804903
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
 

Author Comment

by:budrito12
ID: 18833840
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 84

Expert Comment

by:ozo
ID: 18834616
find . -type f \! -name '[0-9][0-9][0-9][0-9][0-9][0-9] *' -print0 | xargs -0 rm
0
 
LVL 58

Expert Comment

by:amit_g
ID: 18838101
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
 

Author Comment

by:budrito12
ID: 18840781
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
 
LVL 58

Accepted Solution

by:
amit_g earned 125 total points
ID: 18841525
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now