0

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Devidas
  • 2,479
  • 9
  • 24
  • 1
    Don't combine them in the first place ?`for i in {1,2,3};do tail -n +1 "file${i}.txt" >"newfile.${1}";done`. – 123 Dec 05 '16 at 10:28
  • hi thanks for reply but the whole reason I am doing this as a replacement of tar. as tar combines files and extracts on another machine. I am doing it with out tar command as it is not available. – Devidas Dec 05 '16 at 10:34
  • Why don't you just transfer the files? There's literally no benefit in trying to emulate tar. – 123 Dec 05 '16 at 10:36

1 Answers1

0

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.

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
  • hi I tried for given dump but I have file name like /var/www/index.php so it didn't work but it did work for your dump can you suggest something generic – Devidas Dec 05 '16 at 11:02
  • @Devidas It should be possible to modify to your needs, i.e. by removing the dirname: `/^==> ([^\/]*\/)*([^\/]+) <==$/` and then use `res[2]`. – Andreas Louv Dec 05 '16 at 11:07
  • problem is the regular expression is looking for all word chars but it gets spacial chars like / so it did not match – Devidas Dec 05 '16 at 11:08
  • and you have used ".new" for what purpose? – Devidas Dec 05 '16 at 11:11
  • @Devidas Not for anyone, mostly just to give you a change to undo if you did `tail -n +1 a b c | awk ...` – Andreas Louv Dec 05 '16 at 11:12
  • check if you can improve answer with the thing in comment. So that it will be useful for someone who will be seeking for this. Just make his life easy. – Devidas Dec 05 '16 at 12:39
  • `awk 'match($0, /^==> ([^\/]*\/)*([^\/]+) <==$/, res) { file = res[2]; next } { print > file }' filename` – Devidas Apr 13 '17 at 11:31