Help please!Split files according to index


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help please!Split files according to index
# 1  
Old 05-11-2007
Question Help please!Split files according to index

Suppose I have a csv file like this:
27, california , A3
27, vermont, B6
27, ontario, B7
34, kansas, C9
34, washington, A2

How do I split this file into two files according to field1.
file1:
27, california , A3
27, vermont, B6
27, ontario, B7
file2:
34, kansas, C9
34, washington, A2

Thanks a lot!!
# 2  
Old 05-11-2007
Code:
awk -F", " '{ print > $1 }' filename


files 24 and 37 would be created as expected
# 3  
Old 05-11-2007
Code:
awk -F"," ' { print > "file"$1 } ' filename

# 4  
Old 05-11-2007
thanks a lot, but why does it work?
print > $1? print into field 1?
suppose I want to have a .csv extention, I tried
awk -F"," '{print $1.csv }' databasefile.name didn't work

Last edited by onthetopo; 05-11-2007 at 07:48 AM..
# 5  
Old 05-11-2007
Quote:
Originally Posted by onthetopo
thanks a lot, but why does it work?
print > $1? print into field 1?
Because it has been instructed.

Print the record to the file named $1 which is 24 (or) 37 in your examples which is as per your req Smilie
# 6  
Old 05-11-2007
Quote:
Originally Posted by onthetopo
thanks a lot, but why does it work?
print > $1? print into field 1?
Code:
print > "filename"

This is the syntax for sending strings to file inside awk. So whatever string followed by > is taken as filename and here $1 is taken as filename.
# 7  
Old 05-11-2007
Thanks a lot guys
suppose I want to have a .csv extention, I tried
awk -F"," '{print $1.csv }' databasefile.name didn't work
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Automate splitting of files , scp files as each split completes and combine files on target server

i use the split command to split a one terabyte backup file into 10 chunks of 100 GB each. The files are split one after the other. While the files is being split, I will like to scp the files one after the other as soon as the previous one completes, from server A to Server B. Then on server B ,... (2 Replies)
Discussion started by: malaika
2 Replies

2. UNIX for Beginners Questions & Answers

Split and Rename Split Files

Hello, I need to split a file by number of records and rename each split file with actual filename pre-pended with 3 digit split number. What I have tried is the below command with 2 digit numeric value split -l 3 -d abc.txt F (# Will Produce split Files as F00 F01 F02) How to produce... (19 Replies)
Discussion started by: techedipro
19 Replies

3. Shell Programming and Scripting

Backup of index.* files

Sir, My server is hacked. Server administrator is saying that hacker used 0day CentOS kernal exploit to hack it. Now administrator updated the kernal and rebooted the server. But he is unable to cath the hacker, or how he initiated the process. So therefore i need your help in following... (5 Replies)
Discussion started by: SimonFrance
5 Replies

4. Shell Programming and Scripting

script to merge two files on an index

I have a need to merge two files on the value of an index column. input file 1 id filePath MDL_NUMBER 1 MFCD00008104.mol MFCD00008104 2 MFCD00012849.mol MFCD00012849 3 MFCD00037597.mol MFCD00037597 4 MFCD00064558.mol MFCD00064558 5 MFCD00064559.mol MFCD00064559 input file 2 ... (9 Replies)
Discussion started by: LMHmedchem
9 Replies

5. UNIX for Dummies Questions & Answers

create table file from different files with index

Hi, I've several files with two collumns, where first collumn can be used as index. filename1 and filename2 how to create a file I should start with cat all files and extract first collumn to create an index? (4 Replies)
Discussion started by: sargotrons
4 Replies

6. UNIX for Dummies Questions & Answers

join files with numbered index

Hi all I´m a newbie so maybe this question will make someone mad. I am trying this command; join -a1 -11 file1 file2 > file3 file1 looks like: 1 2 3 4 5 6 7 8 9 10 11 file2: (4 Replies)
Discussion started by: awe1
4 Replies

7. Shell Programming and Scripting

rm -i and deleting files from an index table

Hi, I am trying to make a command to delete my files out the trash can, but one at a time. I am currently using rm - i to do this, but the original file locations for restoring my files are heard on a .txt file which I am using as an index table. How would I manage to make it so that if I... (21 Replies)
Discussion started by: E-WAN
21 Replies

8. Shell Programming and Scripting

Sort from start index and end index in line

Hi All, I have a file (FileNames.txt) which contains the following data in it. $ cat FileNames.txt MYFILE17XXX208Sep191307.csv MYFILE19XXX208Sep192124.csv MYFILE20XXX208Sep192418.csv MYFILE22XXX208Sep193234.csv MYFILE21XXX208Sep193018.csv MYFILE24XXX208Sep194053.csv... (5 Replies)
Discussion started by: krish_indus
5 Replies

9. UNIX for Dummies Questions & Answers

wh inode index starts from 1 unlike array index (0)

brothers why inode index starts from 1 unlike array inex which starts from 0 its a question from the design of unix operating system of maurice j.bach i need to know the answer urgently...someone help please (1 Reply)
Discussion started by: sairamdevotee
1 Replies

10. Filesystems, Disks and Memory

why the inode index of file system starts from 1 unlike array index(0)

why do inode indices starts from 1 unlike array indexes which starts from 0 its a question from "the design of unix operating system" of maurice j bach id be glad if i get to know the answer quickly :) (0 Replies)
Discussion started by: sairamdevotee
0 Replies
Login or Register to Ask a Question