Splitting text file into 2 separate files ??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting text file into 2 separate files ??
# 1  
Old 11-12-2010
Java Splitting text file into 2 separate files ??

Hi All,
I am new to this forumn as well to the UNIX, I have basic knowledge of UNIX which I studied some years ago, now I have to do some shell scripting to load data into Oracle database using sqlldr utility, whcih I am able to do. I have a requirement where I need to do following operation.
I have one text file in which the data will be in following format for example
.................... FILE1.txt ....................
A1|1234561|010|065|
aaaaa
sssss
ddddd
fffff
A2|1234562|011|066|
qqqq
ww
eeeeeeee
r
A3|1234563|012|067|
ttttttttt
A4|1234564|013|068|
yyyyy
uuu
A5|1234565|014|069|
sdfsdfsd
werw345
feewwe
A6|1234566|015|060|
A7|1234567|016|061|
....................................................
Now from above file I have to split it into 2 different files for example
File_A.txt
....................................................
A1|1234561|010|065|
A2|1234562|011|066|
A3|1234563|012|067|
A4|1234564|013|068|
A5|1234565|014|069|
A6|1234566|015|060|
A7|1234567|016|061|
....................................................

File_B.txt
....................................................
aaaaa
sssss
ddddd
fffff
qqqq
ww
eeeeeeee
r
ttttttttt
yyyyy
uuu
sdfsdfsd
werw345
feewwe
....................................................
I am able to create the first file FILE_A.txt using awk command, but I am not able create the second FILE_B.txt
I just like to know is there any other quick way of creating 2 seperate files as mentioned above or I can use same awk command to create the second file FILA_B.txt
Any help is highly appreciated.
Regards
JC
# 2  
Old 11-12-2010
Code:
awk '{print > ("File"((/\|/)?"A":"B")".txt")}' File1.txt

This User Gave Thanks to danmero For This Post:
# 3  
Old 11-12-2010
Hi
I Tried with the given solution by specifying my file name in the command
i.e.
awk '{print > ("File"((/\|/)?"A":"B")".txt")}' spgen.txt

But I am getting following error, also any idea how to get second file i.e. FILE_B.txt from FILE1.txt

awk: syntax error near line 1
awk: illegal statement near line 1

I feel I am doing something wrong, please correct me...

I was able to create the first file by using following command
awk '/\|/' spgen.txt > FILE_A.txt

Regards
JC
# 4  
Old 11-12-2010
Use gawk, nawk, mawk OR /usr/xpg4/bin/awk on Solaris.
# 5  
Old 11-12-2010
Thanks Danmero,
That was really quick and apt solution, I used nawk and it worked out. But I would like to understand how it works, probably I will look into details of nawk man pages

Once again thanks for your help Smilie
regards
JC
# 6  
Old 11-12-2010
Quote:
Originally Posted by shekharjchandra
But I would like to understand how it works
Is simple, we just shift the output file name. ((/\|/)?"A":"B") if record match regular expression(pipe char) the value is A else value is B.
# 7  
Old 11-12-2010
Further adding to my previous query ....

I want to create the 2 files based on above nawk command. I will be dynamically passing the value of project number through runtime input variable

i.e by using read v_projname
if my variable is having v_projname=PROJECT1234
so I have to use it something like $v_projectname in my given nawk command, I tried in various combination but I was not able to get the substituted value of variable
I used in this way nawk '{print > ("ABS\_"((/\|/)?"ADDR\_":"SLICE\_")"$v_projname\_NETWORKID.txt")}' spfile.txt

My output of files shoule be something like
ABS_ADDR_PROJECT1234_NETWORKID.txt
ABS_SLICE_PROJECT1234_NETWORKID.txt

But I am getting
ABS_ADDR_$v_projname_NETWORKID.txt
ABS_SLICE_$v_projname_NETWORKID.txt
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Frustrating in splitting text files

Duplicate threads merged Dear all, I have been working with a very large text file manually. I'm ordering how to do this with a script. The gamma should be straightforward: I just want split the text into multiple files. The file name should be "CP1", "TS1 for the second step", "PR1 for... (3 Replies)
Discussion started by: liuzhencc
3 Replies

3. Shell Programming and Scripting

Separate columns into different text files

Hi I have large text file consisting of five columns. Sample of the file is give below: ed 2-4 12.0 commons that they depended on. मानवों नष्ट किया जिन पर वो आधारित थे। ed 3-1 12.0 Almost E, but would be over. रचना करीब करीब ई तक जाती है, मगर तब तो नाटक ख़त्म हो... (2 Replies)
Discussion started by: my_Perl
2 Replies

4. Shell Programming and Scripting

Splitting a column in two separate fields

for making a summary I have a CSV file which is transformed to .DAT. I have an AWK file which is supposing to do the mapping of the DAT file. The code from the AWK file is the one below. The content of the DAT file looks like this (tab separated): ODT AGE CDT CO SEX TIME VALUE COMMENT ... (1 Reply)
Discussion started by: grikoss
1 Replies

5. UNIX for Dummies Questions & Answers

Splitting up a text file into multiple files by columns

Hi, I have a space delimited text file with multiple columns 102 columns. I want to break it up into 100 files labelled 1.txt through 100.txt (n.txt). Each text file will contain the first two columns and in addition the nth column (that corresponds to n.txt). The third file will contain the... (1 Reply)
Discussion started by: evelibertine
1 Replies

6. Shell Programming and Scripting

Combine the lines from separate text files

Hi All, I have three separate text files which has only one line and i want to combine these lines in one text file which will have three lines. cat file1.txt abc cat file2.txt 1265 6589 1367 cat file3.txt 0.98 0.36 0.5 So, I want to see these three lines in the... (9 Replies)
Discussion started by: senayasma
9 Replies

7. Shell Programming and Scripting

Extract text and store in separate files

Hi, I have a file which looks like this: .I 1 some text .A this is the first line .I 2 some text again .B this is the second line .I 3 again some text .C this is the third line I want to have my output like this in separate files: (7 Replies)
Discussion started by: shoaibjameel123
7 Replies

8. UNIX for Dummies Questions & Answers

Separate text files in a folder by word count

Hi, been searching Google for like an hour and I doubt I got the right keywords so might as well ask here. What I need: Before: Article1.txt 564 Article2.txt 799 Article3.txt 349 Article4.txt 452 * Separate files over 400 wordcount * After: Article1.txt 564... (3 Replies)
Discussion started by: Richard2000
3 Replies

9. Shell Programming and Scripting

Splitting text file to several other files using sed.

I'm trying to figure out how to do this efficiently with as little execution time as possible and I'm pretty sure using sed is the best way. However I'm new to sed and all the reading and examples I've found don't seem to show a similar exercise: I have a long text file (i'll call it... (3 Replies)
Discussion started by: JeffV
3 Replies

10. Shell Programming and Scripting

splitting files based on text in the file

I need to split a file based on certain context inside the file. Is there a unix command that can do this? I have looked into split and csplit but it does not seem like those would work because I need to split this file based on certain text. The file has multiple records and I need to split this... (1 Reply)
Discussion started by: matrix1067
1 Replies
Login or Register to Ask a Question