Split file into multiple files using delimiter

Hi,

I have a file which has many URLs delimited by space. Now i want them to move to separate files each one holding 10 URLs per file.

http://3276.e-printphoto.co.uk/guardian http://abdera.apache.org/ http://abdera.apache.org/docs/api/index.html

I have used the below code to arrange each URLs in a line, but unable to write the output to a file(still URLs exist in the same line)

tr -s '[[:space:]]' '\n' < urls.txt

urls.txt - Source file which has multiple URLs

O/P
http://3276.e-printphoto.co.uk/guardian
http://abdera.apache.org/
http://abdera.apache.org/docs/api/index.html
echo `tr -s '[[:space:]]' '\n' < urls.txt` >> newline.txt
##This fails to write the each URL in a single line

Please help.

Regards,
Shunmugavel K

You can achieve your objective if you pipe the output of tr into split .

Regards,
Alister

I have done it, but same output. It doesn't work

tr -s '[[:space:]]' '\n' < urls.txt | split -l 1 urls.txt new

You did it incorrectly. split needs to read from standard input (the pipe), so that it can see the result of tr 's work, one url per line. Instead of the name of the file with the original data, you need to tell split to read - .

Regards,
Alister

No, it does not. Your (unnecessary!) echo transforms the painfully achieved newlines to spaces. Why at all do you use echo?

That's true: it does not work. It can't! Remove the urls.txt new from the end, and split will happily create 3 files with one url in each. Set the l option to 10 for larger files.

Here is an awk program that might also help you:

awk ' BEGIN {
        T = 1
        n = 1
        F = "URL" n
} {
        for ( i = 1; i <= NF; i++ )
        {
                if ( ++c > T )
                {
                        close(F)
                        ++n
                        F = "URL" n
                        c = 1
                }
                print $i > F
        }
} ' urls.txt

Note: Change variable T value as per your requirement depending on how many lines you want to split.

Thanks alister and RodiC. It works as per the changes you have suggested.

---------- Post updated at 03:43 AM ---------- Previous update was at 03:41 AM ----------

Thanks for the program. I don't have more knowledge on awk, but your program is simple for me to understand. I would love to learn it.