Splitting input CSV file into 3 files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting input CSV file into 3 files
# 1  
Old 10-03-2012
Splitting input CSV file into 3 files

Hi ,

I am receiving a CSV file that can vary in number of rows each time.
I am supposed to split this file into 3 separate files like this:
1. create a file named 'File1.csv' that will contain first 3 rows of the input file
2. create file named 'File2.csv' that will contain last 3 rows of the input file
3. create a file named 'File3.csv' that will contain all rows from the input file, except for the first 3 and last 3 rows

Can you please help me with this?
Thanks!
# 2  
Old 10-04-2012
This sure sounds like a homework assignment to me...
# 3  
Old 10-04-2012
Homework has special rules.
Moderator's Comments:
Mod Comment
Please repost this in the homework forums. Thank you.

Closed.


Thread reopened.

Last edited by jim mcnamara; 10-04-2012 at 07:32 AM..
# 4  
Old 10-04-2012
Code:
awk 'FNR==NR{lc1=FNR;next}
FNR<=3{print > "file1.csv";next}
FNR>(lc1-3){print > "file3.csv";next}
{print > "file2.csv"}' file file


Last edited by elixir_sinari; 10-04-2012 at 08:10 AM..
# 5  
Old 10-04-2012
Code:
wc -l infile | read l m

awk -v l=$l '
NR <=3 {print $0 > "File1.csv"; next;}
NR >=l-2 {print $0 > "File3.csv"; next;}
{print $0 > "File2.csv";}
' infile

# 6  
Old 10-04-2012
Quote:
Originally Posted by elixir_sinari
Code:
awk 'FNR==NR{lc1=FNR;next}
FNR<=3{print > "file1.csv";next}
FNR>=(lc1-3){print > "file3.csv";next}
{print > "file2.csv"}' file file

I think this puts the last 4 lines of file in file3.csv; not the last 3 lines. I believe the line writing file3.csv needs to be one of the following:
Code:
FNR>(lc1-3){print > "file3.csv";next}
        or
FNR>=(lc1-2){print > "file3.csv";next}

# 7  
Old 10-04-2012
Quote:
Originally Posted by Don Cragun
I think this puts the last 4 lines of file in file3.csv; not the last 3 lines. I believe the line writing file3.csv needs to be one of the following:
Code:
FNR>(lc1-3){print > "file3.csv";next}
        or
FNR>=(lc1-2){print > "file3.csv";next}

Oops...typo....corrected.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting csv into 3 tables in html file

I have the data in csv in 3 tables. how can I output the same into 3 tables in html.also how can I set the width. tried multiple options . attached is the format. #!/bin/ksh awk 'BEGIN{ FS="," print "<HTML><BODY><TABLE border = '1' cellpadding=10 width=100>" print... (7 Replies)
Discussion started by: archana25
7 Replies

2. Shell Programming and Scripting

Splitting CSV into variables then to XML file

I have a text file that looks like this: FIELD1, FIELD2, THIS IS FIELD3, FIELD4 FIELD1, FIELD2, THIS IS FIELD3, FIELD4 FIELD1, FIELD2, THIS IS FIELD3, FIELD4 I need it to turn it into an XML file to run against a custom application. My ultimate goal is for it to look like... (15 Replies)
Discussion started by: jeffs42885
15 Replies

3. Open Source

Splitting files using awk and reading filename value from input data

I have a process that requires me to read data from huge log files and find the most recent entry on a per-user basis. The number of users may fluctuate wildly month to month, so I can't code for it with names or a set number of variables to capture the data, and the files are large so I don't... (7 Replies)
Discussion started by: rbatte1
7 Replies

4. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

5. Shell Programming and Scripting

Match columns from two csv files and update field in one of the csv file

Hi, I have a file of csv data, which looks like this: file1: 1AA,LGV_PONCEY_LES_ATHEE,1,\N,1,00020460E1,0,\N,\N,\N,\N,2,00.22335321,0.00466628 2BB,LES_POUGES_ASF,\N,200,200,00006298G1,0,\N,\N,\N,\N,1,00.30887539,0.00050312... (10 Replies)
Discussion started by: djoseph
10 Replies

6. Shell Programming and Scripting

Compare 2 csv files in ksh and o/p the difference in a new csv file

(say) I have 2 csv files - file1.csv & file2.csv as mentioned below: file1.csv ID,version,cost 1000,1,30 2000,2,40 3000,3,50 4000,4,60 file2.csv ID,version,cost 1000,1,30 2000,2,45 3000,4,55 6000,5,70 The... (7 Replies)
Discussion started by: Naresh101
7 Replies

7. Shell Programming and Scripting

How to generate a csv files by separating the values from the input file based on position?

Hi All, I need help for doing the following. I have a input file like: aaaaaaaaaabbbbbbbbbbbbbbbbbbbb cccbbbbbaaaaaadddddaaaabbbbbbb now I am trying to generate a output csv file where i will have for e.g. 0-3 chars of each line as the first column in the csv, 4-10 chars of the line as... (3 Replies)
Discussion started by: babom
3 Replies

8. UNIX for Advanced & Expert Users

Splitting the single csv file

Hi, I have a requiement where in i will get a single file but there will be mutiple headers. Suppose say for eg: Header1 Data... Data... Header2 Data.. Data.. Header3 Data.. Data.. I want to split each with the corresponding data into a single file. Please let me know how... (1 Reply)
Discussion started by: weknowd
1 Replies

9. Shell Programming and Scripting

how to give multiple csv files as input in awk

Hi All, I am new to shell scripting..My problem is i want to give multiple csv files as input to awk script and process the data into one file.. My input file is File1 File2 File3 Product Location Period SalesPrice A x 8/11/2010 ... (7 Replies)
Discussion started by: kvth
7 Replies

10. Shell Programming and Scripting

Splitting input files into multiple files through AWK command

Hi, I needs to split *.txt files from single directory depends on the some mutltiple input values. i have wrote the code like below for file in *.txt do grep -i -h "value1|value2" $file > $file; done. My requirment is more input values needs to be given in grep; let us say 50... (3 Replies)
Discussion started by: arund_01
3 Replies
Login or Register to Ask a Question