Shell scripting-I need a script which should watch a directory for a file with specific directory

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell scripting-I need a script which should watch a directory for a file with specific directory
# 1  
Old 08-12-2014
Shell scripting-I need a script which should watch a directory for a file with specific directory

I need a script which should watch a directory for a file with specific directory.
If it finds a file in directory, it should search for few specific keyword in the file. if the keyword exists, it should trim string from specific column.
The file should be moved to another directory and the a mail should be sent to list of email id's and email body should contain the strings which have been trimmed.

For example, I have a file GEFCO_INVRPTB9PDB_VIB1.351.egs. The script should look for lines starting with CSG2 and ARD1. If it starts with CSG2,Script should copy data between position 5-20 and put it into the body of email.
Also, the original file should be moved to another directory.

UNB10941A780843653                     0941A081195390TEST                 10000006      140603    2300    GEFCO-SALCEDA                      25843         
UNH1INVRPTSH            140603    2300    25843         20 
MID1VIB1540          20140603  2224                                                                                                              
NAD1SDT  0060183727804       28734G  16          VISTEON SISTEMAS INTERIORES        EL CERQUIDO S/N                    36472 SALCEDA DE CASELAS                                                                                                                              ES CARLOS MARQUES                     986343402                         986346252                                                            28734G                                                                                                                            10 
NAD1CDT  0941A780843654      VIGO                GEFCO CTL1                         CTL GEFCO ESPANA                   AVENIDA CITROEN, S/N               ZONA FRANCA                        36210 VIGO                                                                      ES JOSE BRION                         986214434                         986212363                                                                                                                                                                                              5  
CSG1006046200400300000                      PEUGEOT CITROEN AUTOMOBILES        AVENIDA CITROEN                    36210 VIGO                                                                                                                                            ES                                                                                                                                                                                                                                     
ARD196738649ES                         1                                  20140602    2255  18            PCE VIB1540                                                     00000000                      925064500ES                                          1  1  2     2X                   006046200400300000  PEUGEOT CITROEN AUTOMOBILES        AVENIDA CITROEN                    36210 VIGO                                                                                               ES 0060183727804       VISTEON SISTEMAS INTERIORES        EL CERQUIDO S/N                    36472 SALCEDA DE CASELAS                                                                                 ES                          0060183727804       VISTEON SISTEMAS INTERIORES        EL CERQUIDO S/N                    36472 SALCEDA DE CASELAS                                                                                 ES 0060183727804       VISTEON SISTEMAS INTERIORES        EL CERQUIDO S/N                    36472 SALCEDA DE CASELAS                                                                                 ES 20140602    2255  VIB1540                                                                                                   1           1  1       04760            04760                              1         PCE

Last edited by Franklin52; 08-12-2014 at 08:33 AM.. Reason: fixed code tags
# 2  
Old 08-12-2014
Welcome akashdeepak,

Well, that's quite a wide post, I must admit. Is that truely how the data looks?

Anyway, I have a few to questions pose in response first:-
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.

We're all here to learn and getting the relevant information will help us all.
# 3  
Old 08-13-2014
This is what I had been trying to get it done.
But problem comes when I use cut command to get the key. It cuts all the fields at once because of which I am not able to use it in if command.

# Function for file watching
  function mailMsg

    if [ $# -ne 3 ]
        return 1
    mail -s "$1" $2 <<ENDMAIL
echo `pwd`
cd /ediec/3271010/in/tempin 
echo `pwd`
#while true
    file_list=$( ls GEFCO_INVRPTB9PDB* 2>/dev/null )
    if [ -n "{$file_list}" ];  
       #echo "inside in"
	for file_name in ${file_list};
	  echo "file_name" $file_name
	   tt=`grep -i CSG2 ${file_name}` >> Dockcodes.txt
	   aa=`grep -i ARD1 ${file_name}` >> Dockcodes.txt

	   #echo "dock" $dock "part" $part
	  # while read aline
	#	  do
			# echo "ttemp2.txt"
			#cat temp2.txt
			key=`cut -c1-4 Dockcodes.txt`
			echo "key" $key
			if [ $key = 'CSG2' ]
				dock=`cut -c5-20 Dockcodes.txt`
				echo $dock
			if [ $key = 'ARD1' ]
				part=`cut -c5-40 Dockcodes.txt` 
				qty=`cut -c93-107 Dockcodes.txt`
                     echo "dock" $dock "part" $part "qty" $qty 
         #        done < Dockcodes.txt
           #echo "Dockcodes.txt"
	   #cat Dockcodes.txt
	   #rm Dockcodes.txt

  	   #echo "inside for ${file_name}"
	   #cat $file_name >> temp.txt
          # mailMsg "${file_name} has been moved to ${DEF_EDI_IN_DIR} directory" "${DEF_EDI_ADMIN_EMAIL}" ""
          # mv ${file_name} ${DEF_EDI_IN_DIR}
	  #echo "before done"

Last edited by rbatte1; 08-14-2014 at 07:12 AM..
# 4  
Old 08-13-2014
How about trying sth. like:
cut  -c1-4,5-20,21-40,93-107 --output-delimiter="|" file |
while IFS="|" read COD DOK PRN QTY 
        do case $COD in
                ARD1) echo "$DOK$PRN :  $QTY";;
                CSG2) echo "$DOK";;

Use the resultant variables to taste ... Result of above applied to your sample file is
96738649ES                         1 :  18            P

( I think the quantity field extends up to 109? Which would make it 18 PCE)

Last edited by RudiC; 08-14-2014 at 01:34 PM.. Reason: typo
These 2 Users Gave Thanks to RudiC For This Post:
# 5  
Old 08-26-2014
I tried the code but it throws error.

--output-delimiter is not supported.

# 6  
Old 08-26-2014
So - what is the delimiter in cut's output? Find out and adapt the script to your needs...
# 7  
Old 08-26-2014
Manual help page does not have any flag for output-delimiter.
Can you help me with some another command to do the same.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell scripting for moving folder specific files into target directory of that country folder.

I need help to write shell script to copy files from one server to another server. Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Discussion started by: naresh2389
3 Replies

2. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies

3. Shell Programming and Scripting

Script to Tar file in a specific Directory

I'm trying to write a Unix script that will go to a specific directory (/tmp/Sanbox/logs) and tar.gz all the log files in that directory and delete the original files that are older than 2 days. So far I have this but it doesn't work. Any help would be appreciated. #!/bin/bash ... (7 Replies)
Discussion started by: Loc
7 Replies

4. UNIX for Beginners Questions & Answers

How to zip csv files having specific pattern in a directory using UNIX shell script?

I have files in a Linux directory . Some of the file is listed below -rw-rw-r--. 1 roots roots 0 Dec 23 02:17 zzz_123_00000_A_1.csv -rw-rw-r--. 1 roots roots 0 Dec 23 02:18 zzz_121_00000_A_2.csv -rw-rw-r--. 1 roots roots 0 Dec 23 02:18 zzz_124_00000_A_3.csv drwxrwxr-x. 2 roots roots 6 Dec 23... (4 Replies)
Discussion started by: Balraj
4 Replies

5. Shell Programming and Scripting

Moving Files one directory to another directory shell script

Hi, Could you please assist how to move the gz files which are older than the 90 days from one folder to another folder ,before that it need to check the file system named "nfs" if size is less than 90 or not. If size is above 90 then it shouldn't perform file move and exit the script throwing... (4 Replies)
Discussion started by: venkat918
4 Replies

6. UNIX for Dummies Questions & Answers

Extract directory name from the full directory path in UNIX using shell scripting

My input is as below : /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/loyal/IFIND.HELLO.WROC.txt /splunk/scrubbed/triumph/ifind.triumph.txt From the above input I want to extract the file names only . Basically I want to... (5 Replies)
Discussion started by: IshuGupta
5 Replies

7. Web Development

Directory index forbidden by Options directive error on specific directory with indexing disabled

I am seeing the following error appear numerous times in my Apache error log: I have my Apache config configured as below, so I would expect indexing not to occur on this directory as it falls under the parent /web directory. Strangely all the IP address, including this example, all... (5 Replies)
Discussion started by: crmpicco
5 Replies

8. UNIX for Advanced & Expert Users

Watch directory and move specific file extensions

Hi all, This is actually more for my lazyness then anything else, but I think others might find it useful to use as well. Basically this is what I am trying to achieve... In my ubuntu home dir under Downloads is where firefox saves everything by default, now I know that you can manually... (3 Replies)
Discussion started by: STOIE
3 Replies

9. Shell Programming and Scripting

shell scripting with directory

I have a directory /ndata/nmk I want to have 4 copies of my daily database backups like /ndata/nmk/copy1 /ndata/nmk/copy2 till copy4 . where copy1-copy4 are directories having my db backups . Once my db backups reach 4 directories like copy4 i want to again write from copy1 to copy4 . ... (3 Replies)
Discussion started by: suku123
3 Replies

10. UNIX for Dummies Questions & Answers

script to watch changes on a directory

Hi, everybody I want to know hot to watch changes on a dir, for example if someone changes a file inside it, with an script. I've tried using md5sum and then diff, sadly with no success. I use md5sum for single files, but doesn't work for directories. The idea is take a snapshot with md5sum,... (4 Replies)
Discussion started by: piltrafa
4 Replies
Login or Register to Ask a Question

Featured Tech Videos