Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Separating concatenated files

  1. #1
    Join Date
    Apr 2009
    Location
    Toronto, Canada
    Beans
    93
    Distro
    Kubuntu 9.04 Jaunty Jackalope

    Separating concatenated files

    I have put two binary files into one single file with the cat command as follows:
    Code:
    cat file1 file2 > bothfiles
    I do not know how to retrieve the files separately afterwards though...

    I came up with two possible solutions:
    1)
    Code:
    echo " separationMarker " > tmp
    cat file1 tmp file2 > bothfiles
    How would you then use separationMarker to send part of bothfiles to one output location and send the other part to another output location?
    2)
    Record the size of both files before packaging them.
    But how would one instruct the shell to output the first x bytes to one location and the next x bytes to another location.

    Any help with the implementation of any of these two solutions or with a completely different approach to separating the concatenated files is appreciated.
    Please do not suggest the use of tar instead of cat; I recognise its relevance to this question, but am searching specifically for a way to separate concatenated files.
    L'idée de communauté consiste d'offrande constante sans s'attendre à quoi que ce soit d'elle.
    The idea of community consists of constant giving without expectation of return.

  2. #2
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Separating concatenated files

    1. Something like the following would work.
    Code:
    flag=0; cat file | while read line; do
      if [ flag -eq 0 ];then
        if [ "$line" != " separationMarker " ]; then
          echo $line >> file1
        else
          flag=1; echo $line >> file2; 
        fi
      else
        echo $line >> file2
      fi
    done
    2. look into the size command.
    Last edited by slavik; December 5th, 2009 at 06:58 PM.
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  3. #3
    Join Date
    Apr 2009
    Location
    Toronto, Canada
    Beans
    93
    Distro
    Kubuntu 9.04 Jaunty Jackalope

    Re: Separating concatenated files

    Ah! That cat bothfiles | while read line is brilliant. Much more likely to work than my previous for piece in `cat bothfiles` attempt.
    However, when I try your code it does not seem to ever find the separationMarker. It creates the first file (which, in the end, is too small) and never makes the second...
    I was also wondering if you really meant to put
    Code:
    flag=1; echo $line >> file2
    Wouldn't that put separationMaker at the beginning of the second file?
    Thanks!
    Last edited by silentrebel; December 5th, 2009 at 07:58 PM.
    L'idée de communauté consiste d'offrande constante sans s'attendre à quoi que ce soit d'elle.
    The idea of community consists of constant giving without expectation of return.

  4. #4
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Separating concatenated files

    good point, I also haven't really tested the code.
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  5. #5
    Join Date
    Jan 2007
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Separating concatenated files

    don't you need to put the separtionMarker in one line?
    Code:
    echo -e "\n separationMarker " > tmp

  6. #6
    Join Date
    Apr 2009
    Location
    Toronto, Canada
    Beans
    93
    Distro
    Kubuntu 9.04 Jaunty Jackalope

    Re: Separating concatenated files

    I thought cat started every new file on a new line...
    Bad assumption, maybe...
    --------------
    This code creates both files, but they are way too small!!! Can anyone justify this?
    Code:
    cat bothfiles | while read line
    do 
         if [ $flag -eq 0 ]
         then 
              if [ -z "`echo "$line" | grep "separation"`" ]
              then 
                   echo "$line" >> file1
              else 
                   flag=1
              fi
         else 
              echo "$line" >> file2
         fi 
    done
    Remember,
    bothfiles is the result of
    Code:
    cat file1 tmp file2 > bothfiles
    and tmp is now the result of
    Code:
    echo -e "\n separationMarker " > tmp
    Last edited by silentrebel; December 5th, 2009 at 07:58 PM.
    L'idée de communauté consiste d'offrande constante sans s'attendre à quoi que ce soit d'elle.
    The idea of community consists of constant giving without expectation of return.

  7. #7
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Separating concatenated files

    cat + while read = UUOC.
    a question, why do you want to combine 2 binaries together? does it work properly when the application calls it?

    Code:
    awk 'FNR==1{print "marker" > "output"}{print $0 > "output"} ' binary1 binary2

  8. #8
    Join Date
    Jun 2009
    Location
    Alabama
    Beans
    2,232

    Lightbulb Re: Separating concatenated files

    I have always split files using the csplit command, but I don't know if that will meet your needs. I do recommend looking at it to see, though.

    Tim

  9. #9
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Separating concatenated files

    yes you can use csplit, provided it knows what to look for as "marker" to split

  10. #10
    Join Date
    Apr 2009
    Location
    Toronto, Canada
    Beans
    93
    Distro
    Kubuntu 9.04 Jaunty Jackalope

    Re: Separating concatenated files

    Quote Originally Posted by ghostdog74 View Post
    Code:
    awk 'FNR==1{print "marker" > "output"}{print $0 > "output"} ' binary1 binary2
    This code returns the following error when executed "awk: (FILENAME=binary1 FNR=1) fatal: cannot open file `binary1' for reading (No such file or directory)." I created empty files named binary1 and binary2, but then, though it did not complain, it did not do anything. I also tried piping bothfiles into the awk command to no avail.
    I'm not entirely familiar with all the logic of this awk command, though. Perhaps if you explained it, I could fix it...

    Quote Originally Posted by ghostdog74 View Post
    yes you can use csplit, provided it knows what to look for as "marker" to split
    I'm not able to get csplit look for the marker, but not copy it to either of the output files. One of the files always ends up bigger than it was originally, because, the marker is included? How would one go about fixing this?
    So far I have :
    Code:
    csplit bothfiles '/marker/'
    Thanks for your help...
    silentrebel
    P.S. I'm trying to make a bidirectional-patch wrapper for rdiff (http://ubuntuforums.org/showthread.php?t=1344415). The finished product should consist of a script, which depends on rdiff, and which uses it to create a single patch-file that offers a bidirectional patch. The same script could then be used to patch or reverse-patch depending on the file on which the patch is applied. I'll post the finished product on the thread cited above.
    Last edited by silentrebel; December 6th, 2009 at 07:22 PM.
    L'idée de communauté consiste d'offrande constante sans s'attendre à quoi que ce soit d'elle.
    The idea of community consists of constant giving without expectation of return.

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

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