Data Splitting into two files from one file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Data Splitting into two files from one file
# 1  
Old 09-13-2011
Question Data Splitting into two files from one file

I have a file as:
I/P File:
Code:
Ground Car 2009
Lib 2008
Lib 2003
Ground Car 2009
Ground Car 2003
Car 2005
Car 2003
Car 2005
Sita 2900 2006
Car 2007

I have to split the file into two: - one for names and second for years.

O/p1 (Names):
Code:
Ground Car
Lib
Lib
Ground Car
Ground Car 
Car
Car
Car
Sita 2900
Car

O/p 2 (Years):
Code:
2009
2008
2003
2009
2003
2005
2003
2005
2006
2007


Thanks!!!
# 2  
Old 09-13-2011
Code:
# Print the last field into file2:   print $NF >"file2"
# Remove the last field:             $NF=""
# Print everything else into file1:  print >"file1"
awk '{ print $NF >"file2" ; $NF="" ; print >"file1" }' < input

# 3  
Old 09-13-2011
Code:
$
$ ls
data.txt
$
$ cat data.txt
Ground Car 2009
Lib 2008
Lib 2003
Ground Car 2009
Ground Car 2003
Car 2005
Car 2003
Car 2005
Sita 2900 2006
Car 2007
$
$
$ perl -lne 'BEGIN {open(F1, ">f1"); open(F2, ">f2")} m/^(.*?) (\d+)$/; print F1 $1; print F2 $2; END {close(F1); close(F2)}' data.txt
$
$
$ ls
data.txt  f1  f2
$
$
$ cat f1
Ground Car
Lib
Lib
Ground Car
Ground Car
Car
Car
Car
Sita 2900
Car
$
$ cat f2
2009
2008
2003
2009
2003
2005
2003
2005
2006
2007
$
$

tyler_durden
# 4  
Old 09-14-2011
MySQL

Quote:
Originally Posted by Corona688
Code:
# Print the last field into file2:   print $NF >"file2"
# Remove the last field:             $NF=""
# Print everything else into file1:  print >"file1"
awk '{ print $NF >"file2" ; $NF="" ; print >"file1" }' < input

Your idea was good! I have to test it.
Will post the result, after I test it!!!

Thanks.

---------- Post updated at 10:58 AM ---------- Previous update was at 10:55 AM ----------

Quote:
Originally Posted by durden_tyler
tyler_durden
Thanks for ur solution! But I don't have Idea on Perl .

---------- Post updated at 02:31 PM ---------- Previous update was at 10:58 AM ----------

Thank U, its worked for me!!!

---------- Post updated at 02:31 PM ---------- Previous update was at 02:31 PM ----------

Thank U, its worked for me!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting the XML file into three different files

Hello Shell Guru's I have a requirement to split the source xml file into three different text file. And i need your valuable suggestion to finish this. Here is my source xml snippet, here i am using only one entry of <jms-system-resource>. There may be multiple entries in the source file. ... (5 Replies)
Discussion started by: Siv51427882
5 Replies

2. 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

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

Splitting a file into 4 files containing the same name pattern

Hello, I have one file which is in size around 20 MB , wanted to split up into four files of each size of 5 MB. ABCD_XYZ_20130302223203.xml. Requirement is that to write script which should do as : first three file should be of size 5 MB each, the fourth one content should be in the last... (8 Replies)
Discussion started by: ajju
8 Replies

5. UNIX for Dummies Questions & Answers

Extracting data from one file, based on another file (splitting)

Dear All, I have two files but want to extract data from one based on another... can you please help me file 1 David Tom Ellen and file 2 David|0010|testnamez|resultsz David|0004|testnamex|resultsx Tom|0010|testnamez|resultsz Tom|0004|testnamex|resultsx Ellen|0010|testnamez|resultsz... (12 Replies)
Discussion started by: A-V
12 Replies

6. Shell Programming and Scripting

Help me pls : splitting single file in unix into different files based on data

I have a file in unix with sample data as follows : -------------------------------------------------------------- -------------------------------------------------------------- {30001002|XXparameter|Layout|$ I want this file to be splitted into different files and corresponding to the sample... (54 Replies)
Discussion started by: Ravindra Swan
54 Replies

7. UNIX for Dummies Questions & Answers

Splitting Data in File

I have a file with the below Data 1,nj@ny@pa@caa 2,ct 3,ca@vaa@txI want the output to be 1,nj 1,ny 1,pa 1,caa 2,ct 3,ca 3,vaa 3,tx I need to split the second column based on @ as delimiter The number of delimiters is unknown (4 Replies)
Discussion started by: traininfa
4 Replies

8. Shell Programming and Scripting

Splitting file into 2 files ?

Hi extending to one of my previous posted query .... I am using nawk -v invar1="$aa" '{print > ("ABS\_"((/\|/)?"A\_":"B\_")invar1"\_NETWORKID.txt")}' spfile.txt to get 2 different files based on split condition i.e. "|" Similar to invar1 variable in nawk I also need one more variable... (18 Replies)
Discussion started by: shekharjchandra
18 Replies

9. Shell Programming and Scripting

Splitting files from one file

Hi, I have an input file like: 111 abcdefgh asdfghjk dfghjkl 222 aaaaaaa bbbbbb 333 djfhfgjktitjhgfkg 444 djdhfjkhfjkghjkfg hsbfjksdbhjkgherjklg fjkhfjklsahjgh fkrjkgnj I want to read this input file and make separate output files with the header as numric value like "111"... (9 Replies)
Discussion started by: saltysumi
9 Replies

10. Shell Programming and Scripting

Splitting data file

Hello, I'm trying to split a file by lines. I know that I can use the split command to do this, but the one problem I'm having is, each file created, the first line needs to be a header. I can use the split command the create another file with the header, then append the new split file to... (4 Replies)
Discussion started by: ctcuser
4 Replies
Login or Register to Ask a Question