I have a WINDOWS text file that contains the following in its own directory: It is called "test.txt"
Example Source File – test.txt:
hxxp://url.xxx/XX/file 1.txt
hxxp://url.xxx/XX/file 2.avi
hxxp://url.xxx/XX/file 3.mpg
The content of the first file would be:
hxxp://url.xxx/file 1.txt
The name of this file would be:
file 1.txt
My goal is to create separate text files containing each URL and named after the "file" part of the URL. So the first one would be "file 1.txt.txt" then "file 2.avi.avi" and so on. Split does this perfectly EXCEPT for naming the actual files.
Best Answer
This is easily done in
awk
. You just need to set the field separator to/
and then print each line into a file whose name is the last field. Since the number of fields inawk
is saved in theNF
variable, you can get the last fields with$NF
. Then, if you want all of them to have the txt extension, all you need is:If, as seems to be the case based on your comments, your input file has Windows line endings, you need to remove the
\r
first: