Search code examples
pythonlinuxbashsplittail

splitting files concatinated by tail


I am copying php files from remote web server from telnet. I used [this][1] command is

tail -n +1 file1.txt file2.txt file3.txt

for concatenating but now I want to split that file to separate files.

I did tried and could get to a point where I could get all line numbers of file name line and filename in another file using this commands respectively.

 grep -irn "==>" php.dump |cut -d: -f1

 grep -irn "==>" php.dump |cut -d' ' -f2 

now I want to use this info to split files.

this is a lengthy approach can you guys suggest any straight forward approach. or anything to do after this two commands

I am sure there has to be something to do this as it is used generally to take dump.

Note: I know this would have been easy with tar but sadly there is no tar command and did not have root privilege to that server.

cat multiple files but include filename as headers


Solution

  • Since you are using a poor mans tar -c here is a poor mans tar -x:

    $ awk 'match($0, /^==> (\w+) <==$/, res) { file = res[1]; next }
                                             { print > file".new" }' comb_file.txt
    

    With input like:

    ==> file1 <==
    hello1
    
    ==> file2 <==
    hello2
    
    ==> file3 <==
    hello3
    

    Three files will be created: file1, file2 and file3, each containing the lines between the ==> xxx <==.

    This means an trailing empty line will be added to file1 and file2. But it might get your started.