Dear All,
I was wondering in how split one file in multiple file in a conditional way.
Briefly, I have a file like this:
>Id1
textA
>Id2
textB
and my outputs file (2 in this case) shoul be:
Id1
>Id1
textA
Id2
>Id2
textB
hope you may help me.
Best
G
Hello giuliangiuseppe,
Could you please try following and let me know if this helps.
awk '/^>/{++i}{print >> "file" i}' Input_file
Output will be as follows.
cat file1
>Id1
textA
cat file2
>Id2
textB
Thanks,
R. Singh
It works prefectly.
The only things is that should be nice that file name is the first line of each file (without
>
)
cat Id1
>Id1
textA
cat Id2
>id2
textB
Best
Giuliano
RudiC
November 26, 2015, 7:40am
4
Try like
awk '/^>/{FN=$0; sub (">", "", FN)}{print > FN}' file
Id1:
>Id1
textA
Id2:
>Id2
textB
1 Like
Hello Giuliano,
Could you please try following then, it may help you in same.
awk '/^>/{sub(/>/,X,$0);i=$0} {print >> i}' Input_file
Output will be as follows.
cat Id1
Id1
textA
cat Id2
Id2
textB
Thanks,
R. Singh
1 Like
If your input file is to be split into several files, you may have to close files when you're done writing to them. Even if you just have a few file to be written, it is a good idea to close files when you're done with them...
If you run into problems with the other suggestions you've been given, you could try:
awk '/^>/{close(f);f=substr($0,2)}{print > f}' file
As always, if you want to try any of these awk
script on a Solaris/SunOS system, change awk
to /usr/xpg4/bin/awk
or nawk
.