Results 1 to 6 of 6
  1. #1
    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.

  2. #2
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,015
    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

  3. #3
    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.

  4. #4
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,015
    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 13:25.

  5. #5
    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.

  6. #6
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,015
    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •