Split the file and access that files through array and loop

Hi All,

the below is my requirement..

i need to split the file based on line and put that files in a array and need to access that files through loop finally i should send the files through mail..

how can we achieve this ..I am new to shell script please guide me..

I am using KSH..

Thanks in advance..

Regards,
Kalidoss

Yes?
And what have you done so far?

Hi Vbe,

I have done the below ...

split -l 2 line_test.txt line_test.txt
set -A TEST_ARRAY line_test.txta*
a=0
for i in ${TEST_ARRAY[@]}
do
   echo " TEST_ARRAY[$a]  : $i"
   mv $i ./line_test_$a.txt
   (( a = a + 1 ))
done

it is working ...I have searched and tried this...

But I dont know that the above is correct. Is there some nice way to do the same ?

Regards,
Kalidoss

Using an array for this is kind of overkill. If you just want to read lines one by one, read lines one by one.

A=0
while read -r LINE
do
        echo mv "$LINE" "./line_test_${A}.txt"
        let A=A+1
done < inputfile

This ought to work in any Bourne shell, while that array code will only work in KSH.

1 Like

Hi Corona,

I have tried your code , i got the below error...

syntax error at line 6 : `done' unexpected

regards,
Kalidoss

The code seems to be fine. Have you saved the file with a windows editor?

1 Like

Hi Franklin,

Yes. I have saved the file with a windows editor.

it is working fine now..

This code reads the file line by line.. I want to split the file..

How can we achieve this ..

Please give me any idea..

Thanks in advance..

Regards,
Kalidoss

Can you explain exactly what you're trying to achieve with an example?

Hi Franklin,

Actually I have a text file ..I need to split that file and should mail that splitted files based on some conditions..

Regards,
Kalidoss

Show the input you want, the bits you want to mail, and explain what these conditions are.

Otherwise we're just guessing.

Hi Corona,

1|PAYER NAME|248277|248000000|SUPPLIER_01|IBEL||ICBC|||0|900|01|
2|PAYER NAME|248277|248000000|SUPPLIER_01|IBEL||ICBC|||0|100|01|
1|PAYER NAME|248277|248000000|SUPPLIER_01|IBEL||ICBC|||0|900|01|
2|PAYER NAME|248277|248000000|SUPPLIER_01|IBEL||ICBC|||0|100|01|
1|PAYER NAME|248277|248000000|SUPPLIER_01|IBEL||ICBC|||0|900|01|
2|PAYER NAME|248277|248000000|SUPPLIER_01|IBEL||ICBC|||0|100|01|
1|PAYER NAME|248277|248000000|SUPPLIER_01|IBEL||ICBC|||0|900|01|
2|PAYER NAME|248277|248000000|SUPPLIER_01|IBEL||ICBC|||0|100|01|
1|PAYER NAME|248277|248000000|SUPPLIER_01|IBEL||ICBC|||0|900|01|
2|PAYER NAME|248277|248000000|SUPPLIER_01|IBEL||ICBC|||0|100|01|
1|PAYER NAME|248277|248000000|SUPPLIER_01|IBEL||ICBC|||0|900|01|
2|PAYER NAME|248277|248000000|SUPPLIER_01|IBEL||ICBC|||0|100|01|
1|PAYER NAME|248277|248000000|SUPPLIER_01|IBEL||ICBC|||0|900|01|
2|PAYER NAME|248277|248000000|SUPPLIER_01|IBEL||ICBC|||0|100|01|
1|PAYER NAME|248277|248000000|SUPPLIER_01|IBEL||ICBC|||0|900|01|
2|PAYER NAME|248277|248000000|SUPPLIER_01|IBEL||ICBC|||0|100|01|
1|PAYER NAME|248277|248000000|SUPPLIER_01|IBEL||ICBC|||0|900|01|
2|PAYER NAME|248277|248000000|SUPPLIER_01|IBEL||ICBC|||0|100|01|
1|PAYER NAME|248277|248000000|SUPPLIER_01|IBEL||ICBC|||0|900|01|
2|PAYER NAME|248277|248000000|SUPPLIER_01|IBEL||ICBC|||0|100|01|

This is how my text file looks..

If the text file is more than 10 rows ,i need to divide this file and send the divided files through mail...

what i have done is the below

LINE_COUNT=`wc -l $APPLTMP/${TXT_FILE_NAME} | cut -d " " -f8`
echo "LINE_COUNT :$LINE_COUNT"
if [ LINE_COUNT -gt 10 ]
then
split -l 10 $APPLTMP/$TXT_FILE_NAME $APPLTMP/$TXT_FILE_NAME
cd $APPLTMP
set -A FILE_ARRAY ${TXT_FILE_NAME}a*
a=0
for i in ${FILE_ARRAY[@]}
do
echo " FILE_ARRAY[$a]  : $i"
#cp $i ./$APPLTMP/${TXT_FILE_NAME}_$a
(( a = a + 1 ))
done
set -A FILE_ARRAY ${TXT_FILE_NAME}a*
else
cd $APPLTMP
set -A FILE_ARRAY ${TXT_FILE_NAME}
fi
#
for i in ${FILE_ARRAY[@]}
do
#echo "inside for loop"
#
echo "  $i"
    if [ -f $APPLTMP/$i ] 
    then
(echo "Please find attached your requested report"
        uuencode ${i} ${i}) | mailx -s "${i}" ${P_EMAIL}
    fi
done

Thanks in advance..

Regards,
Kalidoss

split -d -l 10 filename filename_split_
tar -cf filename_archive.tar filename_split_*
uuencode filename_archive.tar filename_archive.tar | mailx -s "Subject" user@domain.com
1 Like