Issue in splitting a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue in splitting a file
# 1  
Old 09-04-2012
Issue in splitting a file

Hi,

I have a file (named as values) which contains around 10 columns with an unique number in the first column. I wanted to segregate the file into 10 files based on the range of the Unique number. It varies from 0 to 10 Million. So, I need to split the file into 10 files with ranges from 0 to 1 million .... 9 million to 10 million.

I also have a file (named as headeronly) that contains the header which I should use to merge the splitted file with a common header . "awk and cat commands are used inside a for loop". The issue is the script is getting executed and when the splitted files checked only the header is printed but not the columns of the numerical values.

Code used:

Code:
for (i=0;i<=9;i=i+1)
 do
   awk'{if(($i*1000000<$1)&&($1<($i+1)*1000000)){print$0}}' values > $i
   cat headeronly $i > a$i
done

Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.


Kindly give suggestion.

Last edited by Corona688; 09-04-2012 at 06:40 PM..
# 2  
Old 09-04-2012
Variables do not work inside single quotes.

Code:
$ echo '$i'

$i

$

Instead you input variables into awk like
Code:
awk -v I="$i" '{ ... }'

Also, you can simplify your awk code a lot. A raw expression by itself prints lines whenever true, and doesn't whenever false.

So:

Code:
for i in 0 1 2 3 4 5 6 7 8 9
do
   awk -v I="$i" '(((I*1000000)<$1)&&($1<((I+1)*1000000)))' values > $i
   cat headeronly $i > a$i
done

# 3  
Old 09-04-2012
$i inside the awk script is not $i from the shell script (at least in single-quotes), and awk doesn't use a $ to access most variables ($i will evaluate to a field, 0 in this case).

EDIT: Also, I'm guessing you want the header first into a$i, and append the other values to the same file? What is the $i in the cat for though?

Try something like:
Code:
for (i=0;i<=9;i=i+1)
 do
   cat headeronly $i > a$i
   awk -vidx=$i '{if ((idx*1000000<$1)&&($1<(idx+1)*1000000)){print}}' values >> a$i
done

# 4  
Old 09-05-2012
can you provide sample data of input and output for your requirement
# 5  
Old 09-05-2012
@ raj_saini20

Say a file (named as values) has following data (i have given only 2 columns for simplication) with a header as :

The values of the simulation are as follows with the unique number and potential as

unique number potential

1 5.63
2 9.62
3 0.23
4 5.01
5 6.36
6 2.40
7 5.62
8 1.24
9 4.23
10 7.25
11 1.98
. .
. .
. .
. .
100 7.04

Now , i need to separate above file into 10 files based on unique number with range from 1 to 10 , 11 to 20 ..... 91 to 100 and include the common header to all these 10 files .
I should do this by using "awk and cat" command.

---------- Post updated at 01:09 AM ---------- Previous update was at 12:58 AM ----------

@CarloM : I am just trying to first split the files into 10 number based on their unique number. So , I am first using the "awk" command and put it into a new 10 files with names as 0 1 2 ...9. That's why is use $i in the last of the awk .

Code:
for (i=0;i<=9;i=i+1)
 do
   awk'{if(($i*1000000<$1)&&($1<($i+1)*1000000)){print$0}}' values > $i

Then, i wish to add header to all my newly created files (i.e) to files named 0 1 2 .....9 using the cat command. since , i have referred $i as the new file in the above awk command, i am doing same in the cat command also .

Code:
cat headeronly $i > a$i
done


Last edited by b@l@ji; 09-05-2012 at 03:35 AM..
# 6  
Old 09-05-2012
Bug

Try something like this...

Code:
for ((i=0;i<=9;i=i+1))
 do
   cat headeronly $i > a$i
   range_s="10" #Here you can give your range directly so no need to change in the code.. 10 is for 0-100 range..
   awk -v idx="$i" -v RG="$range_s" '{if ((idx*RG<$1)&&($1<=(idx+1)*RG)){print}}' test_file >> a$i   
done

# 7  
Old 09-05-2012
check this if it is working for splitting into 10 files.
if this code is working i will further modify it for insertion of header in all the ten files.
One more thing in how many lines your header including column name is coming
Code:
awk '{for(i=1;i<=10;i++){if(($1>=(i-1)*1000000)&&($1<i*1000000)){print > i".txt"}}}' values.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Splitting the file based on two fields - Fixed length file

Hi , I am having a scenario where I need to split the file based on two field values. The file is a fixed length file. ex: AA0998703000000000000190510095350019500010005101980301 K 0998703000000000000190510095351019500020005101480 ... (4 Replies)
Discussion started by: saj
4 Replies

2. Shell Programming and Scripting

Splitting a text file into smaller files with awk, how to create a different name for each new file

Hello, I have some large text files that look like, putrescine Mrv1583 01041713302D 6 5 0 0 0 0 999 V2000 2.0928 -0.2063 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 5.6650 0.2063 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 3.5217 ... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

3. Shell Programming and Scripting

awk issue splitting a fixed-width file containing line feed in data

Hi Forum. I have the following script that splits a large fixed-width file into smaller multiple fixed-width files based on input segment type. The main command in the script is: awk -v search_col_pos=$search_col_pos -v search_str_len=$search_str_len -v segment_type="$segment_type"... (8 Replies)
Discussion started by: pchang
8 Replies

4. Shell Programming and Scripting

Issue splitting file based on XML tags

more a-d.txt1 <a-dets> <a-serv> <aserv>mymac14,mymac15:MYAPP:mydom:/web/domain/mydom/config <NMGR>:MYAPP:/web/bea_apps/perf/NMGR/NMGR1034 <a-rep-string> 11.12.10.01=192.10.00.26 10.20.18.10=192.10.00.27 </a-rep-string> </a-serv> <w-serv>... (2 Replies)
Discussion started by: mohtashims
2 Replies

5. Shell Programming and Scripting

Execution of loop :Splitting a single file into multiple .dat file

hdr=$(cut -c1 $path$file|head -1)#extract header”H” trl=$(cut -c|path$file|tail -1)#extract trailer “T” SplitFile=$(cut -c 50-250 $path 1$newfile |sed'$/ *$//' head -1')# to trim white space and extract table name If; then # start loop if it is a header While read I #read file Do... (4 Replies)
Discussion started by: SwagatikaP1
4 Replies

6. Shell Programming and Scripting

Splitting XML file on basis of line number into multiple file

Hi All, I have more than half million lines of XML file , wanted to split in four files in a such a way that top 7 lines should be present in each file on top and bottom line of should be present in each file at bottom. from the 8th line actual record starts and each record contains 15 lines... (14 Replies)
Discussion started by: ajju
14 Replies

7. Shell Programming and Scripting

Splitting a file in to multiple files and passing each individual file to a command

I have an input file with contents like: MainFile.dat: 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 ... (4 Replies)
Discussion started by: rkrish
4 Replies

8. Shell Programming and Scripting

Issue while splitting a row of record

Hi, I have one file with the following details, ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Account_Id Date Id Balance 44 9 1000.00 30 15-10-2173 10 1000.00 42 15-10-2173 10 1200.00 53 01-01-2008 10 1200.00 I need to split up the values in to the respective fields as follows, ... (7 Replies)
Discussion started by: Kattoor
7 Replies

9. UNIX for Dummies Questions & Answers

Splitting a file based on record sin another file

All, We receive a file with a large no of records (records can vary) and we have to split it into two files based on another file. e.g. File1: UHDR 2008112 "25187","00000022","00",21-APR-1991,"" ,"D",-000000519,+0000000000,"C", ,+000000000,+000000000,000000000,"2","" ,21-APR-1991... (7 Replies)
Discussion started by: er_ashu
7 Replies

10. UNIX for Advanced & Expert Users

Issue with Splitting of file

HI! All Iam running this script ro split the file ,attact a timesatmp to it and then conver it to .tmp extension cd /home/staff/thussain Prefix=Z_PRICE_NEW_`date "+%Y%m%d%H%M%S"` split -3000 -a 5 /home/staff/thussain/Z_PRICE_NEW.txt $Prefix find . -name "$Prefix*" -print | { while read... (6 Replies)
Discussion started by: mohdtausifsh
6 Replies
Login or Register to Ask a Question