Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pass array variable to close() as filename 2

Status
Not open for further replies.

huangzihyun

Programmer
Jul 8, 2009
1
TW
I have pattern file like below
123456
234567
789012
345678
456789
...
...
...

and another data_file repeat as below(about 500 lines)
#aaaaaaaaaa
bbbbbbbbbbb
ccccccccccc
ddddddddddd
#aaaaaaaaaa
bbbbbbbbbbb
ccccccccccc
ddddddddddd
...
...
...
...
...
#aaaaaaaaaa
bbbbbbbbbbb
ccccccccccc
ddddddddddd

now I need to split data file from #aaaaaaaaaa to ddddddddddd as a block, one block for one split file,
and every split file's filename must sequentially read from pattern file.

such as:
123456 (filename1)
#aaaaaaaaaa
bbbbbbbbbbb
ccccccccccc
ddddddddddd

234567 (filename2)
#aaaaaaaaaa
bbbbbbbbbbb
ccccccccccc
ddddddddddd

789012 (filename3)
#aaaaaaaaaa
bbbbbbbbbbb
ccccccccccc
ddddddddddd

345678 (filename4)
#aaaaaaaaaa
bbbbbbbbbbb
ccccccccccc
ddddddddddd

(filename5)

(filename6)

How to code this?

there is a code
gawk -v n=1 '/ddddddddddd/{close("splitfile"n);n++;next} {print > "splitfile"n}' data_file

could easyily split file,
but how to put pattern into close() as filename?
Thanks

 
Hi

Code:
[gray]# my style[/gray]
gawk '/^#/{if(n)close(n);getline n<"name.txt"}{print>n}' content.txt

[gray]# PHV's style[/gray]
gawk 'FNR==NR{n[NR]=$0;next}/^#/{if(i++)close(n[i])}{print>n[i]}' name.txt content.txt

Feherke.
 
:-D
In fact, # my style
Code:
gawk 'FNR==NR{n[NR]=$0;next}/^#/{if(FNR>1)close(n[i]);++i}{print>n[i]}' name.txt content.txt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top