Divide the file into several Variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Divide the file into several Variable
# 1  
Old 04-15-2010
Divide the file into several Variable

I have a file, let's say X
Code:
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:
Code:
BTS 0          UNLOCKED         ENABLED         NONE
TRX 0          UNLOCKED         ENABLED         NONE
TRX 1          UNLOCKED         ENABLED         NONE

Variable TWO contains:
Code:
BTS 1          UNLOCKED         ENABLED         NONE
 TRX 0          UNLOCKED         ENABLED         NONE
 TRX 1          UNLOCKED         ENABLED         NONE

Variable THREE contains:
Code:
BTS 2          UNLOCKED         ENABLED         NONE
 TRX 0          UNLOCKED         ENABLED         NONE
 TRX 1          UNLOCKED         ENABLED         NONE

I need your help guys... Smilie

Last edited by zaxxon; 04-15-2010 at 07:54 AM.. Reason: use code tags please, ty
# 2  
Old 04-15-2010
you wanted to divide in 3 variables or 3 files?

---------- Post updated at 03:11 AM ---------- Previous update was at 03:01 AM ----------

Code:
awk '{if($0 !~ /BTS/){cur=cur " " $0;}else {printf("%s\n", cur);cur=$0}}END{print cur}'  file

# 3  
Old 04-15-2010
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:
Code:
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:
Code:
BTS 0          UNLOCKED         ENABLED         NONE
TRX 0          UNLOCKED         ENABLED         NONE
TRX 1          UNLOCKED         ENABLED         NONE

for variable number 2 (BTS1), contain the text:
Code:
BTS 1          UNLOCKED         ENABLED         NONE
TRX 0          UNLOCKED         ENABLED         NONE
TRX 1          UNLOCKED         ENABLED         NONE

for variable number 3 (BTS2), contain the text:
Code:
BTS 2          UNLOCKED         ENABLED         NONE
TRX 0          UNLOCKED         ENABLED         NONE
TRX 1          UNLOCKED         ENABLED         NONE


Last edited by zaxxon; 04-15-2010 at 07:55 AM.. Reason: use code tags please, ty
# 4  
Old 04-15-2010
Quote:
Originally Posted by amaulana
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:
Code:
awk '/BTS/{f=$2}{print > "BTS" f}'  file

# 5  
Old 04-16-2010
if your file is small, may use below similar perl script.

Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Divide the numbers in file

Dear ALL, I have sample file : tx_bytes: 2422, tx_packets: 13, uptime: 16119, tx_bytes: 2342, tx_packets: 14, uptime: 11009, tx_bytes: 252, tx_packets: 12, uptime: 3113, my formula : minutes=$(( uptime/60%60 )) hours=$(( uptime/60/60%24 )) (3 Replies)
Discussion started by: gnulyn
3 Replies

2. UNIX for Dummies Questions & Answers

To divide file

We have large log files, and we need to extract last four hours lines only, Please let us know the command. Thanks, Saurau. (1 Reply)
Discussion started by: saurau
1 Replies

3. UNIX for Dummies Questions & Answers

Divide a numerical data column by a variable

Hello, I have two files, f1 and f2. f1 has 5 columns like so: a b c d 154 e f g h 365 ..... f2 has two columns, the first column contains the name of the above file and second column contains a constant which is to be used for division. e.g. file1 56 I want to divide the 5th... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

4. Shell Programming and Scripting

Divide number of lines by the size of the same file. And create relational table.

I basically need to do what the title says. I have my text file. I'm still pretty new at this. At the moment I know that: 1. wc -l file.txt To get the number of lines. 2. ls -lh file.txt To get the file size. But I need to divide both numbers. Then I need to save the output in a... (7 Replies)
Discussion started by: PainMaker101
7 Replies

5. Shell Programming and Scripting

Divide two variable

F= `expr $E/$S` output test5.sh: line 45: 1296863.27001857757568359375/21997: No such file or directory can any one help me ,i just want to divide the $E/$S and Print o/p in F. (3 Replies)
Discussion started by: netdbaind
3 Replies

6. Shell Programming and Scripting

Divide variable

Hi All, here D Prints the bytes value .plz help to convert the variable D value to MB in new variable $E D=`more $C |awk '{print $6;}'` Thanks in Advance.:) (3 Replies)
Discussion started by: netdbaind
3 Replies

7. UNIX for Dummies Questions & Answers

divide the file into multiple files based on the city name

Hi, I have a file abc.dat. It contains the fileds of empid, empname, empcity. each city contains 10 records. i want to create the city file and pass the same city records into the file. I don't know the city names. In unix using awk command how can we do? abc.dat: 1 john delhi 2... (2 Replies)
Discussion started by: raghukreddy.ab
2 Replies

8. Shell Programming and Scripting

divide a single file with different Weboffercodes into different files with each of o

Here is the format of my file; I do not have the delimiter in the file for the data to be separated. Each line in the file is in the following format. File contains the data of different WebOfferCodes Item Code | Account Number | Card Number | Source code | WebOfferCode 12digits | 10... (4 Replies)
Discussion started by: enigma_83
4 Replies

9. Shell Programming and Scripting

how to divide single large log file into multiple files.

Can you please help me with writing script for following purpose. I have to divide single large web access log file into multiple log files based on dates inside the log file. For example: if data is logged in the access file for jan-10-08 , jan-11-08 , Jan-12-08 then make small log file... (1 Reply)
Discussion started by: kamleshm
1 Replies

10. Shell Programming and Scripting

script needs to divide by 2

i need to divide this count by 2, what variable can i use in my script? 26 hcscprod_cpus_totals /2 = 13 13 hcncprod_cpus_totals /2= 6.5 541 ktazp_cpus_totals /2= 270.5 346 ktazd_cpus_totals /2=173 110 ktazi_cpus_totals /2=55 10 ktazq_cpus_totals /2=5 (2 Replies)
Discussion started by: wereyou
2 Replies
Login or Register to Ask a Question