Solved

Complex UNIX Script question?

Posted on 2007-03-27
8
426 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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
 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

776 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