check number or character of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting check number or character of a file
# 1  
Old 11-08-2010
check number or character of a file

HTML Code:
Hi,

Please see the below information.

1. Read files one by one and run the script.
2. Check if the filename is CHARTER OR NUMBER 
3. Run the case statement.


I have files in the folder. i will get 300 files in a single day.
Code:
Abc_111111111111.csv
101010_kkk_bbbb.csv
101012_ddd_bbbb.csv
101013_nnn_bbbb.csv


Read the first file then check the filename having charter or number.
Code:
Source_file= source filename

If it is a CHARTER then 

fname=`echo $Source_file |cut -d '_' -f1`   # ex  input:Abc_111111111111.csv   output :Abc 
else NUMBER
fname=`echo $Source_file |cut -d '_' -f2`   # ex  input:101010_kkk_bbbb.csv  output :kkk
fi


case $fname in
        Abc) echo "Abc found";
     Mv $Source_file  taget_path;;
        kkkk) echo "CCCC found" ;
      Mv $Source_file  Backup directory;;
        *) echo "INVALID filename!" ;;
esac

Any help greatly appreciated.

Thanks
Suri
# 2  
Old 11-08-2010
Code:
cat chk_file.ksh
#!/bin/ksh

FNAME=$1

case ${FNAME} in
   +([0-9]) )
      echo "${FNAME} is NUMBER"
   ;;
  * )
      echo "${FNAME} is CHARACTER"
  ;;
esac


Code:
./chk_file.ksh 1234  
1234 is NUMBER

./chk_file.ksh abcd
abcd is CHARACTER

./chk_file.ksh abc4
abc4 is CHARACTER

# 3  
Old 11-08-2010
Hi,

I have written the script. It is working fine.
On demand, I will receive files in the source directory [Each day I will receive 300 files]. Using control-m scheduler I am scheduling the below shell script every five mins.

But I am facing lot of problems to atomize for the following things also include in the script.

1. Filename passing as a parameter. But I want to atomize to read all the files in the directory execute the shell script one by one.
2. Ex: source files:
All_101010101010.csv
101010_EMP_1200.csv
101011_SALES_3200.csv

First it will check the starting with charter file [Ex: All_101010101010.csv] then it will execute script for only that file. After the completion of the script automatically zip the file and moved to back up directory.

Then execute the script for starting with number files[Ex: 101010_EMP_1200.csv]. After the completion of the script automatically zip the file and moved to back up directory.
And so on....


Code:
#!/bin/sh

####################################################################
# Check the input parameters
####################################################################
if [ $# -ne 1 ]
then
	echo "parameter missing"
	enit 1
fi


Source_file=$1
fname=`echo $Source_file|cut -d '_' -f1`

####################################################################
# Check the source file is a charter or number
####################################################################
case $fname in
	[0-9]*) var=`echo $Source_file|cut -d '_' -f2` ;;
	[a-zA-Z]*) var=`echo $Source_file|cut -d '_' -f1` ;;
	*) echo "Invalid file name" ;;
esac

####################################################################
#Based on the type of file run the data stage job.
####################################################################
if [ $var = 'ALL']
then
	dsjob -server :$SERVER_PORTID 
	      -run 
	      -mode NORMAL 
	      -jobstatus 
	      -param INPUT_GCDB_DIR=$InputFilePath 
	      -param INTERIM_DIR=$SequentialFilePath 
	      -param SCRIPT_DIR=$ShellScriptPath 
	      -param CountryID=$CountryID 
	      -warn 0 $ProjectName $JobName1.$CountryID
elseif [ $var = 'EMP']
then
	dsjob -server :$SERVER_PORTID 
	      -run 
	      -mode NORMAL 
	      -jobstatus 
	      -param INPUT_GCDB_DIR=$InputFilePath 
	      -param INTERIM_DIR=$SequentialFilePath 
	      -param SCRIPT_DIR=$ShellScriptPath 
	      -param CountryID=$CountryID 
	      -warn 0 $ProjectName $JobName2.$CountryID
elseif [ $var = 'SALES']
then
	dsjob -server :$SERVER_PORTID 
	      -run 
	      -mode NORMAL 
	      -jobstatus 
	      -param INPUT_GCDB_DIR=$InputFilePath 
	      -param INTERIM_DIR=$SequentialFilePath 
	      -param SCRIPT_DIR=$ShellScriptPath 
	      -param CountryID=$CountryID 
	      -warn 0 $ProjectName $JobName3.$CountryID
else 
	echo " not valid"
fi

Any help greatly appreciated

Thanks
suri
# 4  
Old 11-08-2010
Quote:
Originally Posted by onesuri
...
1. Filename passing as a parameter. But I want to atomize to read all the files in the directory execute the shell script one by one.
2. Ex: source files:
All_101010101010.csv
101010_EMP_1200.csv
101011_SALES_3200.csv

First it will check the starting with charter file [Ex: All_101010101010.csv] then it will execute script for only that file. After the completion of the script automatically zip the file and moved to back up directory.

Then execute the script for starting with number files[Ex: 101010_EMP_1200.csv]. After the completion of the script automatically zip the file and moved to back up directory.
And so on....
...

You could do something like this -

Code:
#!/usr/bin/sh
for x in *.csv
do
  x=`echo $x | tr [a-z] [A-Z]`
  case $x in
        ALL*) ## execute your job for "ALL" here
              ;;
       *EMP*) ## execute your job for "EMP" here
              ;;
     *SALES*) ## execute your job for "SALES" here
              ;;
           *) echo "Invalid file name"
              ;;
  esac
done
$
$

# 5  
Old 11-09-2010
Hi,
When i run the below script i am getting the below error.

data2.sh[15]: 0403-057 Syntax error at line 23 : `;' is not expected.


HTML Code:
#!/usr/bin/sh

#####################################
# Execute only for ALL filename
#####################################


for x in `ls /home_dir/ | egrep '^[a-zA-Z].*(csv$|cma$)'`
do
echo $x
y=`echo $x |cut -d '_' -f1`



  case $y in
        ALL ) echo "ALL found"
              ;;
        *) echo "No map file found "
              ;;
  esac
done

#####################################
# Execute only for CCCC filename
#####################################

for x in `ls /home_dir/ | egrep '^[0-9].*(csv$|cma$)'`
do
echo $x
y=`echo $x |cut -d '_' -f2`
  case $y in
        CCCC ) echo "Data file found" # adding some logic here.
               z=$?
               if [ $z -eq 0] then `gzip $x ; mv $x.gz /home_dir/bkp/abc.gz` else Exit 1 fi
              ;;
        *) echo "No Data file found "
              ;;
  esac
done
Any help greatly appricated.
thanks
suri
# 6  
Old 11-09-2010
Guess there are couple of semicolon miss in the if statement and there is no need for the back-ticks. Try if the below one helps
Code:
if [ $z -eq 0]; then gzip $x ; mv $x.gz /home_dir/bkp/abc.gz ; else Exit 1; fi

# 7  
Old 11-11-2010
Hi,
I am using control-m to trigger the shell script every 5 mins.

time slot to trigger shell script
example:

10.00 - automatically trigger the shell script -in my source directory i have 2 files[101111_EMP_3333.csv,101011_SALES_2345.csv]. those two files are processed. this looks fine.


10.05 - automatically trigger the shell script - no files present in the source directory- it wont execute the script. this looks file.

10.10 - automatically trigger the script-i have 2 files in the source directory-[101010_EMP_2222.csv,101111_SALES_6789.csv]. started processing for the first file- it takes 6 mins to complete then next file completed 3 mins.- total execution time is 9 mins.

10.13. i have one more file[101015_EMP_6666.csv] in the source file directory. this file will not execute here until the above two files should complete.what is the condition i have to place in this situation.

10.15

Code:
#!/usr/bin/sh

if [ $# -ne 1 ]
then
	exho "paramter missing"
	exit 1
fi

Indir=$1

new()
{
for x in `ls $Indir | egrep '[0-9].*_'$flag'_.*(csv$|cma$)'`
do
y=`echo $x|cut -d '_' -f2`

case $y in
	$y ) echo " souce file found" ; wc -l $x;mv $x /home/bkp/        # doing many calaculations here based on the file. 
	;;
	* ) echo "source data file not found"
	;;
esac
done
}

flag=EMP
new $Indir $flag &

flag=SALES
new $Indir $flag &

wait
echo "completed"

Any help greatly appriciated.


thanks
Suri
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to check number of group of file.?

Hi Gurus, I need check existing number of file based on the list in file list. for example: in my file list. I have below: abc, file1.txt abc, file2.txt abc, file3.txt abc, file4.txt cde, filea1.txt cde, filea2.txt cde, filea3.txt ... in my current file direcotry, I have file:... (0 Replies)
Discussion started by: ken6503
0 Replies

2. Shell Programming and Scripting

To check for a number inside the file in UNIX

Hi Gurus I am a newbie to Unix programming and I am having a difficulty in finding out a number which is present in a file and storing it in a variable so that i can use it in my shell script. The content of the file "count" is: Count of the files=11 I need to just store the value 11 in... (8 Replies)
Discussion started by: vikramgk9
8 Replies

3. Shell Programming and Scripting

Check Junk character in sql file

Hello, I have two .sql files which I transferred from Windows to Unix (Linux Enterprise Linux Server release 5.3).I want to ensure that these two files have no junk characters in them.How do I do it in the simplest possible way? Many thanks DJ (1 Reply)
Discussion started by: Digjoy83
1 Replies

4. Shell Programming and Scripting

How to check newline character in file?

Hi All, I have file with only one record,always be only one record. as like below. if that line contains newline end of the line..no need to add, if not just add the new line character. END OF FILE. ROW COUNT: 7 Please help me.. Thanks, (9 Replies)
Discussion started by: bmk
9 Replies

5. Shell Programming and Scripting

How to check for a character at last line of the file?

this is the csv file. i want to check the last line contains the character N. Record Type#Batch Job ID#Batch Number#FileCreation Date#FileCreation Time#Production/Test Fileindicator#File Character H#0002#0002#20100218#17.25#P#barani Record Type#A#B#C#D#E#F#G#H#J#K#L... (5 Replies)
Discussion started by: barani75
5 Replies

6. Shell Programming and Scripting

check number of character

hi, I would like to calculate number of character for a number, for exemple : 1200 --> there are 4 characters , 120001 -> 5 characters (4 Replies)
Discussion started by: francis_tom
4 Replies

7. Shell Programming and Scripting

To check if the first character is a alphabet or number

Hi, I need to find whether the first character in a line is a alphabet or a number. If its a number i should sort it numerically. If its a alphabet i should sort it based on the ASCII value.And if it is something other than alphabet or number then sort it based on ASCII value. The code i used... (2 Replies)
Discussion started by: ragavhere
2 Replies

8. AIX

check for a particular character inside a file and substitute with a given character?

i am a newbie to shell script,so i want a kshell script in which i need to check for a particular character inside a file through conditional looping(like if ,case,while)and if that character exists ,then substitute a given character to that character. consider a file test.txt,inside the file... (1 Reply)
Discussion started by: karthikprasathk
1 Replies

9. Shell Programming and Scripting

how to search string and number in one file and check in the other file

Hi, Can anyone help in the below problem. file1 has the below contents fileset 999 primary-ilist inode 37020 has invalid dotdot (426094) -> Not exist fileset 999 primary-ilist inode 115016 dup block ->... (9 Replies)
Discussion started by: knshree
9 Replies

10. Shell Programming and Scripting

hw can i count the number of character in a file by perl

i want to count the number of character contained in afile using perl cript help me out (1 Reply)
Discussion started by: trupti_rinku
1 Replies
Login or Register to Ask a Question