Solved

Complex UNIX Script question?

Posted on 2007-03-27
8
432 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
[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
  • 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Understanding ping command in Ubuntu 5 68
debug scriptblock parameters in powershell script 2 48
Open new WPF windows - powershell 4 41
script to read text file 34 34
Many people tend to confuse the function of a virus with the one of adware, this misunderstanding of the basic of what each software is and how it operates causes users and organizations to take the wrong security measures that would protect them ag…
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…
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…
The viewer will learn how to dynamically set the form action using jQuery.

710 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