?
Solved

Complex UNIX Script question?

Posted on 2007-03-27
8
Medium Priority
?
441 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
Google Certified Professional - Cloud Architect

This course (1 of 3) is designed to help students who are interested in Google Cloud Platform (GCP) to become familiar with the platform, navigate the console and learn its capabilities. It will also prepare students for the Google Cloud Architect certification exam.

 

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

AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

Question has a verified solution.

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

In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

719 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