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

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.

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 ]
    then
        echo "ERROR: Usage is $0 MAIL_SUBJECT MAIL_RECIPIENT MAIL_MESSAGE"
        return 1
    fi
    mail -s "$1" $2 <<ENDMAIL
    $3
 
ENDMAIL
}


nDEF_EDI_ADMIN_EMAIL=adeepak@visteon.com,cgd@qad.com
DEF_EDI_LOG=/ediec/log/"${CURR_PRG_NAME}".log
DEF_EDI_TEMP_DIR=/ediec/3271010/in/tempin
DEF_EDI_IN_DIR=/ediec/3271010/in/
echo `pwd`
cd /ediec/3271010/in/tempin 
echo `pwd`
CSG2="CSG2"
ARD1="ARD1"
Dockcodes="Dockcodes.txt"
#while true
#do
    file_list=$( ls GEFCO_INVRPTB9PDB* 2>/dev/null )
    if [ -n "{$file_list}" ];  
    then
       #echo "inside in"
	for file_name in ${file_list};
	 do
	  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' ]
			then
				dock=`cut -c5-20 Dockcodes.txt`
				echo $dock
                        fi
			if [ $key = 'ARD1' ]
			then
				part=`cut -c5-40 Dockcodes.txt` 
				qty=`cut -c93-107 Dockcodes.txt`
			fi
                     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"
	 done 
    fi

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";;
           esac
        done 

Use the resultant variables to taste ... Result of above applied to your sample file is

FV6VFF          
96738649ES                         1 :  18            P

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

2 Likes

I tried the code but it throws error.

--output-delimiter is not supported.

So - what is the delimiter in cut 's output? Find out and adapt the script to your needs...

Manual help page does not have any flag for output-delimiter.
Can you help me with some another command to do the same.

You could use bash 's Parameter Substring Expansion:

while read line
  do COD=${line:0:4}; DOK=${line:4:16}; PRN=${line:20:20}; QTY=${line:92:15}
     echo "$COD", "$DOK", "$PRN", "$QTY"
     done < file
CSG2, FV6VFF, , 
ARD1, 96738649ES      ,                    1, 18            P

You could also try sed:

OIFS=$IFS
IFS=$'\n'
set `sed -nE \
  -e '/^CSG2/s:^.{4}(.{15}).*:\1:p' \
  -e '/^ARD1/s:^.{4}(.{35}).{53}(.{14}).*:\1\n\2:p' infile`
echo "dock \"$1\" part \"$2\" qty \"$3\""
IFS=$OIFS

Or like this:

IFS=$'\n' V=(`sed -nE \
  -e '/^CSG2/s:^.{4}(.{15}).*:\1:p' \
  -e '/^ARD1/s:^.{4}(.{35}).{53}(.{14}).*:\1\n\2:p' infile`)
echo "dock \"${V[0]}\" part \"${V[1]}\" qty \"${V[2]}\""
dock "FV6VFF         " part "96738649ES                         " qty "18            "