I have a file, let's say X
BTS 0 UNLOCKED ENABLED NONE
TRX 0 UNLOCKED ENABLED NONE
TRX 1 UNLOCKED ENABLED NONE
BTS 1 UNLOCKED ENABLED NONE
TRX 0 UNLOCKED ENABLED NONE
TRX 1 UNLOCKED ENABLED NONE
BTS 2 UNLOCKED ENABLED NONE
TRX 0 UNLOCKED ENABLED NONE
TRX 1 UNLOCKED ENABLED NONE
I want to devide the file into several variables based on the number of BTS, there's 3 BTSes here, so i need three variable.
Variable ONE contains:
BTS 0 UNLOCKED ENABLED NONE
TRX 0 UNLOCKED ENABLED NONE
TRX 1 UNLOCKED ENABLED NONE
Variable TWO contains:
BTS 1 UNLOCKED ENABLED NONE
TRX 0 UNLOCKED ENABLED NONE
TRX 1 UNLOCKED ENABLED NONE
Variable THREE contains:
BTS 2 UNLOCKED ENABLED NONE
TRX 0 UNLOCKED ENABLED NONE
TRX 1 UNLOCKED ENABLED NONE
I need your help guys...
you wanted to divide in 3 variables or 3 files?
---------- Post updated at 03:11 AM ---------- Previous update was at 03:01 AM ----------
awk '{if($0 !~ /BTS/){cur=cur " " $0;}else {printf("%s\n", cur);cur=$0}}END{print cur}' file
thanks bro..
devide to 3 variables (but 3 files are also ok), your script tested ini solaris and this is the result:
the file still in 1 file and this is the result:
BTS 0 UNLOCKED ENABLED NONE TRX 0 UNLOCKED ENABLED NONE TRX 1 UNLOCKED ENABLED NONE
BTS 1 UNLOCKED ENABLED NONE TRX 0 UNLOCKED ENABLED NONE TRX 1 UNLOCKED ENABLED NONE
BTS 2 UNLOCKED ENABLED NONE TRX 0 UNLOCKED ENABLED NONE TRX 1 UNLOCKED ENABLED NONE
we can use BTS number as the name of its variable or file, BTS0 for variable number 1, BTS1 for variable number 2 and so on. So the result wanted are:
for variable number 1 (BTS0), contain the text:
BTS 0 UNLOCKED ENABLED NONE
TRX 0 UNLOCKED ENABLED NONE
TRX 1 UNLOCKED ENABLED NONE
for variable number 2 (BTS1), contain the text:
BTS 1 UNLOCKED ENABLED NONE
TRX 0 UNLOCKED ENABLED NONE
TRX 1 UNLOCKED ENABLED NONE
for variable number 3 (BTS2), contain the text:
BTS 2 UNLOCKED ENABLED NONE
TRX 0 UNLOCKED ENABLED NONE
TRX 1 UNLOCKED ENABLED NONE
Try this:
awk '/BTS/{f=$2}{print > "BTS" f}' file
if your file is small, may use below similar perl script.
local $/;
my $str=<DATA>;
my @arr = split(/^(?=BTS)/m,$str);
for(my $i=0;$i<=$#arr;$i++){
*{"var".$i}=\$arr[$i];
}
print $var2;
__DATA__
BTS 0 UNLOCKED ENABLED NONE
TRX 0 UNLOCKED ENABLED NONE
TRX 1 UNLOCKED ENABLED NONE
BTS 1 UNLOCKED ENABLED NONE
TRX 0 UNLOCKED ENABLED NONE
TRX 1 UNLOCKED ENABLED NONE
BTS 2 UNLOCKED ENABLED NONE
TRX 0 UNLOCKED ENABLED NONE
TRX 1 UNLOCKED ENABLED NONE