Make new files based on the number extracted


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Make new files based on the number extracted
# 1  
Old 07-18-2011
Make new files based on the number extracted

Hi All,

I have a big file which looks like this:

Code:
0 4.5 6.7
0 3.4 6
1 5 6
1 6 4
2 9 4.44

Above is just a miniature version of the file. In fact, considering the first column of the file that is 0 0 1 1 2, the numbers in actual go until 10442.

This means my actual file looks like this:

Code:
0 4.5 6.7
0 3.4 6
1 5 6
1 6 4
2 9 4.44
..
..
10442 5 4.333

What I want to do is to look in the first column, extract all the lines which have 0 in the beginning and store in the file 1.num

Referring to the above example, this is what I mean:

Code:
0 4.5 6.7
0 3.4 6

The above numbers go in file, 1.num


Code:
1 5 6
1 6 4

Lines which begin with 1, go in file named 2.num

Subsequently,

Code:
10442 5 4.333

goes in file 10443.num

The number of lines containing 0's or 1's or 10442's is not fixed. This means I have to look at the first number from each line and keep storing the lines with the same number in the same file.

I am not able to figure it out as to how to tackle this but I show as to what I have done:

Code:
awk '{print $1} {c++}{print > c."num"}' scores.sce

The above code has following problems:
1. I am not able to put in the searching part that is the part which searches for 0 or 1 or 2 etc in the beginning of every line.
2. It gives syntax error message.

I am using Linux with BASH.

Last edited by shoaibjameel123; 07-18-2011 at 05:32 AM.. Reason: Added extra information
# 2  
Old 07-18-2011
Code:
awk '{print > $1+1".num"}' inputfile

This User Gave Thanks to pravin27 For This Post:
# 3  
Old 07-18-2011
Code:
awk '{print > $1+1".num"}' file

This User Gave Thanks to bartus11 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Select and copy .csv files based on row and column number

Dear UNIX experts, I'm a command line novice working on a Macintosh computer (Bash shell) and have neither found advice that is pertinent to my problem on the internet nor in this forum. I have hundreds of .csv files in a directory. Now I would like to copy the subset of files that contains... (8 Replies)
Discussion started by: rcsapo
8 Replies

2. Shell Programming and Scripting

List files with number to select based on number

Hi experts, I am using KSH and I am need to display file with number in front of file names and user can select it by entering the number. I am trying to use following command to display list with numbers. but I do not know how to capture number and identify what file it is to be used for... (5 Replies)
Discussion started by: mysocks
5 Replies

3. Shell Programming and Scripting

Compare two unsorted unequal files extracted from xml

I have two files for comparison which are extracts from set of xml files. file1 has: Comparing File: BRCSH1to320140224CC3.xml :: TZZZ:BR :: TAZZ:OUT UIZZ:0 :: ERAZ:1.000000 UIZZ:0 :: CTZZ:B UIZZ:0 :: CCAZ:MYR Comparing File: BRMY20140224CC18REG013SPFNSY13.xml :: TZZZ:BR :: TAZZ:INB... (1 Reply)
Discussion started by: vamsi gunda
1 Replies

4. UNIX for Dummies Questions & Answers

Sort Files based on the number(s) on the file name

Experts I have a list of files in the directory mysample1 mysample2 mysample3 mysample4 mysample5 mysample6 mysample7 mysample8 mysample9 mysample10 mysample11 mysample12 mysample13 mysample14 mysample15 (4 Replies)
Discussion started by: dsedi
4 Replies

5. UNIX for Dummies Questions & Answers

Command to split the files based on the number of lines in it

Hello Friends, Can anyone help me for the below requirement. I am having a file called Input.txt. My requirement is first check the count that is wc -l input.txt If the result of the wc -l Input.txt is less than 10 then don't split the Input.txt file. Where as if Input.txt >= 10 the split... (12 Replies)
Discussion started by: malaya kumar
12 Replies

6. Shell Programming and Scripting

include file name to extracted files

I've written the script below to merge only .txt files that exist in one directory into one huge .txt file and ignore other files with other extensions. now the result is one huge .txt file with all the contents of other .txt files how can i add a File Name as a comment before each file? ... (12 Replies)
Discussion started by: miss_dodi
12 Replies

7. UNIX for Dummies Questions & Answers

Merging two files based on two columns to make a third file

Hi there, I'm trying to merge two files and make a third file. However, two of the columns need to match exactly in both files AND I want everything from both files in the output if the two columns match in that row. First file looks like this: chr1 10001980 T A Second... (12 Replies)
Discussion started by: infiniteabyss
12 Replies

8. Shell Programming and Scripting

Creating folders based on number of files

I have hundreds of files numbered in consecutive number in one single folder What I would like to do is to make as many subfolders as needed (dependeing on the number of individual files) and name them Folder01, Folder02, etc. Then, move file01 to folder01, file02 to folder02 so on and so... (3 Replies)
Discussion started by: Xterra
3 Replies

9. Shell Programming and Scripting

Script to split files based on number of lines

I am getting a few gzip files into a folder by doing ftp to another server. Once I get them I move them to another location .But before that I need to make sure each gzip is not more than 5000 lines and split it up . The files I get are anywhere from 500 lines to 10000 lines in them and is in gzip... (4 Replies)
Discussion started by: gubbu
4 Replies
Login or Register to Ask a Question