If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Splitting one file into few others with column in the name

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
Join Date: Jan 2013
Posts: 3
Splitting one file into few others with column in the name

Hello everybody,

I faced an issue to solve.

Let's say that I have a file like this:

Big_file.txt

1|2|3|4|message1
2|3|4|5|message1
3|4|1|4|message2
3|4|2|2|message2
3|3|1|2|message2
4|1|0|2|message3
4|1|2|6|message1

I want to write a shell script that will split this file into few smallers like this:

File 1:

Name: File_message1.txt
1|2|3|4|message1
2|3|4|5|message1
4|1|2|6|message1

File 2:

Name: File_message2.txt
3|4|1|4|message2
3|4|2|2|message2
3|3|1|2|message2


File 3:

Name File_message3.txt
4|1|0|2|message3

Note: The column with messageX textis dynamic - always will be different text there.
Anyone knows how to solve it?
Thank you.
Reply With Quote
  #2 (permalink)  
Old
Programming since 1BC
 
Join Date: Sep 2009
Location: Ontario
Posts: 972
Code:
#!/bin/bash                   
while read line               
do                            
        i=`echo ${#line}`     
        i=`expr $i - 1`       
        x=`echo ${line:$i:1}` 
        file=message$x.txt    
        echo $line >>$file    
done
See: Bash String Manipulation Examples – Length, Substring, Find and Replace
Reply With Quote
  #3 (permalink)  
Old
Registered User
 
Join Date: Jan 2013
Posts: 3
Quote:
Originally Posted by kitaman View Post
Code:
#!/bin/bash                   
while read line               
do                            
        i=`echo ${#line}`     
        i=`expr $i - 1`       
        x=`echo ${line:$i:1}` 
        file=message$x.txt    
        echo $line >>$file    
done
See: Bash String Manipulation Examples Length, Substring, Find and Replace
I think it may be preety close. But first of all it does not work for me at all
I put my big_file as an argument (./scr.sh source.txt) while running the script but it does not work. It actually hangs.

Anyway, it seems to be good code (apart from not-working ) but my output files should be like: File_value_of_last_column.txt

Kitman, can you please tell me how to run this script and can you make it work like I wrote above?
Thank you.
Nodex.
Reply With Quote
  #4 (permalink)  
Old
Programming since 1BC
 
Join Date: Sep 2009
Location: Ontario
Posts: 972
Run it as
Code:
myscript <big_file.txt
not
Code:
myscript big_file.txt
Note that you must run it in as a bash shell.

To run it so that the input file is a command line parameter rather than redirected:
Code:
#!/bin/bash                   
while read line               
do                            
        i=`echo ${#line}`     
        i=`expr $i - 1`       
        x=`echo ${line:$i:1}` 
        file=message$x.txt    
        echo $line >>$file    
done<$1

Last edited by kitaman; 01-11-13 at 12:25.
Reply With Quote
  #5 (permalink)  
Old
Registered User
 
Join Date: Jan 2013
Posts: 3
Quote:
Originally Posted by kitaman View Post
Run it as
Code:
myscript <big_file.txt
not
Code:
myscript big_file.txt
Note that you must run it in as a bash shell.

To run it so that the input file is a command line parameter rather than redirected:
Code:
#!/bin/bash                   
while read line               
do                            
        i=`echo ${#line}`     
        i=`expr $i - 1`       
        x=`echo ${line:$i:1}` 
        file=message$x.txt    
        echo $line >>$file    
done<$1
I need to pass only 4 out of 5 columns into the file. So, my files should looks like this:

File 1:

Name: File_message1.txt
1|2|3|4
2|3|4|5
4|1|2|6

File 2:

Name: File_message2.txt
3|4|1|4
3|4|2|2
3|3|1|2

File 3:

Name File_message3.txt
4|1|0|2

Last column should not be passed. How to do that? I tried to put a cut expression into a variable but it did not work.
Reply With Quote
  #6 (permalink)  
Old
Programming since 1BC
 
Join Date: Sep 2009
Location: Ontario
Posts: 972
Code:
#!/bin/bash  
rm message?.txt
IFS="|"               
while read a b c d line               
do                            
        i=`echo ${#line}`     
        i=`expr $i - 1`       
        x=`echo ${line:$i:1}` 
        file=message$x.txt    
        echo $a\|$b\|$c\|$d >>$file    
done<$1
Reply With Quote
Reply

Tags
shell scripts, splitting file

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On