Reputation: 2559
Good day,
I was wondering how to cut a file and save each part in a different file. Where the delimiter is ]
example:
TOfile1 ] TOfile2
Thanks in advance
Upvotes: 0
Views: 562
Reputation: 15996
This appears to be precisely the standard use-case for the cut
utility. Without further ado:
cut --delimiter=']' --field=1 input.txt > TOFile1.txt
cut --delimiter=']' --field=2 input.txt > TOFile2.txt
I'm using the long option names here for readability. The short versions are -d
and -f
respectively.
Upvotes: 1
Reputation: 77085
Using awk
:
$ awk -F' []] ' '{for(i=1;i<=NF;i++) print $i > "file"i}' input
$ head file*
==> file1 <==
TOfile1
==> file2 <==
TOfile2
[]]
, since ]
is a special character we put it inside character class to consider it literal. Hence, if your input is something like the following:
$ cat input
TOfile1 ] TOfile2 ] Tofile3 ] Tofile4
TOfile1 ] TOfile2
$ awk -F' []] ' '{for(i=1;i<=NF;i++) print $i > "file"i}' input
$ head file*
==> file1 <==
TOfile1
TOfile1
==> file2 <==
TOfile2
TOfile2
==> file3 <==
Tofile3
==> file4 <==
Tofile4
Upvotes: 3
Reputation: 17455
sed -e 's/.*]//' myfile > TOFile2
sed -e 's/].*//' myfile > TOFile1
Upvotes: 1