0

I have a text file that has the contents of the example below, and I would like to split the file to multiple files.

[TXT]   /path/to/[TXT]
[BAT]   /path/to/[BAT]
[TXT]   /path/to/blah/[TXT]
[BAT]   /path/to/blah/[BAT]

So I have figured out I can use csplit to at least partially do what I wanted to achieve.

csplit -f 'paths-' -b '%04d.txt' 'path/to/filelist.txt' '/^\[(.*)]\t/' '{*}'

However this splits to paths-0000.txt.

I was hoping for something more like paths-txt.txt and paths-bat.txt.

Is there anyway I can get the regex match into the prefix match at all?

I did try things like -f 'paths-$1.txt' and -f 'paths-\1.txt'.

But neither of those did what I was hoping for them to do.

1 Answer 1

0

Using csplit is the wrong tool for this job.

I used awk instead to achieved what I was trying to do.

awk -F"\t" '{print > $1".txt"}' /path/to/filelist.txt

You can if you wish limit it so only certain parts of the file are kept after the split, but I needed/wanted everything.

awk -F"\t" '{print $2 > $1".txt"}' /path/to/filelist.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .