Split the file and access that files through array and loop | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Split the file and access that files through array and loop

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-29-2013
kalidoss kalidoss is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 1 August 2013, 10:35 AM EDT
Posts: 10
Thanks: 4
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old 01-29-2013
vbe's Avatar
vbe vbe is online now Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 16 April 2014, 12:11 PM EDT
Location: Switzerland - GE
Posts: 5,489
Thanks: 148
Thanked 361 Times in 338 Posts
Yes?
And what have you done so far?
Sponsored Links
    #3  
Old 01-29-2013
kalidoss kalidoss is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 1 August 2013, 10:35 AM EDT
Posts: 10
Thanks: 4
Thanked 0 Times in 0 Posts
Hi Vbe,

I have done the below ...

Code:
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

Moderator's Comments:
Please use code tags next time for your code and data.

Last edited by vbe; 01-29-2013 at 08:46 AM..
    #4  
Old 01-29-2013
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 16 April 2014, 11:58 AM EDT
Location: Saskatchewan
Posts: 18,518
Thanks: 680
Thanked 3,034 Times in 2,854 Posts
Using an array for this is kind of overkill. If you just want to read lines one by one, read lines one by one.


Code:
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.
The Following User Says Thank You to Corona688 For This Useful Post:
kalidoss (01-31-2013)
Sponsored Links
    #5  
Old 01-30-2013
kalidoss kalidoss is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 1 August 2013, 10:35 AM EDT
Posts: 10
Thanks: 4
Thanked 0 Times in 0 Posts
Hi Corona,

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

syntax error at line 6 : `done' unexpected

regards,
Kalidoss
Sponsored Links
    #6  
Old 01-30-2013
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 16 April 2014, 10:25 AM EDT
Location: The Netherlands
Posts: 7,667
Thanks: 116
Thanked 525 Times in 496 Posts
The code seems to be fine. Have you saved the file with a windows editor?
The Following User Says Thank You to Franklin52 For This Useful Post:
kalidoss (01-31-2013)
Sponsored Links
    #7  
Old 01-30-2013
kalidoss kalidoss is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 1 August 2013, 10:35 AM EDT
Posts: 10
Thanks: 4
Thanked 0 Times in 0 Posts
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
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
problem access array outside of loop in bash adlmostwanted Shell Programming and Scripting 4 12-12-2011 11:18 AM
Push records to array during implicit loop and write to file jospan Shell Programming and Scripting 0 01-12-2010 08:06 AM
Not access variable outside loop when a reading a file ricardo.ludwig Shell Programming and Scripting 4 05-23-2009 11:14 PM
split and making an array inside another array dcfargo Shell Programming and Scripting 2 08-06-2008 10:07 AM
Access value outside awk or split value of array jason.bean UNIX for Dummies Questions & Answers 1 11-26-2007 03:33 PM



All times are GMT -4. The time now is 12:11 PM.