Splitting file into 2 files ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting file into 2 files ?
# 1  
Old 11-19-2010
Java 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 like invar2 to be passed into nawk. This I want to use in the place of NETWORKID
i.e. somthing similar to

nawk -v invar1="$aa" invar2="$bb" '{print > ("ABS\_"((/\|/)?"A\_":"B\_")invar1"\_"invar2".txt")}' spfile.txt

Above syntax is obviously wrong, I just like to know is there a way I can pass two different variables in nawk as above or other alternate way
- - - -
Also can I use Substring in nawk, because I need to find the '|' character in certain position in every line
i.e. I need to pick only those lines whose fifth character is '|' and then put that entire line in first file or else in second file
Eg:-
if
aa=xxxx
bb=yyyyy

and spfile.txt is

aaa1|bbb1|ccc1
11|21|31|
12|22|32|
13|23|33|
14|24|34|
aaa2|bbb2|ccc2
31|51|71|
32|52|72|
33|53|73|
34|54|74|
aaa3|bbb3|ccc3
41|61|81|
42|62|82|
43|63|93|
44|64|94|
aaa4|bbb4|ccc4

THEN

First file output should be (File ABS_A_xxxx_yyyy.txt)
aaa1|bbb1|ccc1
aaa2|bbb2|ccc2
aaa3|bbb3|ccc3
aaa4|bbb4|ccc4

Second file output should be (File ABS_B_xxxx_yyyy.txt)
11|21|31|
12|22|32|
13|23|33|
14|24|34|
31|51|71|
32|52|72|
33|53|73|
34|54|74|
41|61|81|
42|62|82|
43|63|93|
44|64|94|

Many many thanks in advance
regards
jc
# 2  
Old 11-19-2010
Code:
... -v invar1="$aa" -v invar2="$bb" ...

as many -v as necessary
# 3  
Old 11-19-2010
Hi
Thanks for your quick reply !!

Any clue on my second point i.e. using substring to find the fifth position character as "|" and then placing that entire line in first file
or else in second file

regards
JC
# 4  
Old 11-19-2010
Code:
grep ^[a-z] infile >>output1
grep ^[0-9] infile >>output2

By the way, you should NOT manage spfile manually (should be managed using SQL statements) you should manage the initSID.ora !

you might only use spfile to see what is in it :
Code:
strings spfile

but you should NOT modify it manually

(It should be regenerated from the initSID.ora)

What you should do before modif is :

1) keep a copie of the spfile and the initSID.ora before any modif
2) shutdown the database
3) rename/move the spfile (to *.old or whatever)
4) update your initSID.ora (set up new parameters...)
5) start your database (so it will take into account the initSID.ora)
6) generate the new spfile from your initSID.ora
Code:
SQL> create spfile from pfile


Last edited by ctsgnb; 11-19-2010 at 06:27 AM..
# 5  
Old 11-19-2010
Quote:
Originally Posted by shekharjchandra
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 like invar2 to be passed into nawk. This I want to use in the place of NETWORKID
i.e. somthing similar to

nawk -v invar1="$aa" invar2="$bb" '{print > ("ABS\_"((/\|/)?"A\_":"B\_")invar1"\_"invar2".txt")}' spfile.txt

Above syntax is obviously wrong, I just like to know is there a way I can pass two different variables in nawk as above or other alternate way
- - - -
Also can I use Substring in nawk, because I need to find the '|' character in certain position in every line
i.e. I need to pick only those lines whose fifth character is '|' and then put that entire line in first file or else in second file
Eg:-
if
aa=xxxx
bb=yyyyy

and spfile.txt is

aaa1|bbb1|ccc1
11|21|31|
12|22|32|
13|23|33|
14|24|34|
aaa2|bbb2|ccc2
31|51|71|
32|52|72|
33|53|73|
34|54|74|
aaa3|bbb3|ccc3
41|61|81|
42|62|82|
43|63|93|
44|64|94|
aaa4|bbb4|ccc4

THEN

First file output should be (File ABS_A_xxxx_yyyy.txt)
aaa1|bbb1|ccc1
aaa2|bbb2|ccc2
aaa3|bbb3|ccc3
aaa4|bbb4|ccc4

Second file output should be (File ABS_B_xxxx_yyyy.txt)
11|21|31|
12|22|32|
13|23|33|
14|24|34|
31|51|71|
32|52|72|
33|53|73|
34|54|74|
41|61|81|
42|62|82|
43|63|93|
44|64|94|

Many many thanks in advance
regards
jc
Try this:
Code:
awk -F"|" -v aa="xxxx" -v bb="yyyy" ' {
  fn=length($1)==4?"A":"B"
  print > "ABS_" fn "_" aa "_" bb ".txt"
}' file

This User Gave Thanks to Franklin52 For This Post:
# 6  
Old 11-19-2010
Hi

Thanks Franklin for your reply !!!

ctsgnb -> Sorry to confuse you !

Actually here by spfile I mean one of the spool file created through one of the standalone PL/SQL block in my earlier steps of shell script

Later this spfile.txt need to be splited into 2 different files by using the nawk as discussed, for which you gave the solution.

spfile.txt containing the lines having 5th character as "|" to be moved into one file and the other lines into another file.

Brief example:-

spfile.txt is

aaa1|bbb1|ccc1
11|21|31|
12|22|32|
13|23|33|
14|24|34|
aaa2|bbb2|ccc2
31|51|71|
32|52|72|
33|53|73|
34|54|74|
aaa3|bbb3|ccc3
41|61|81|
42|62|82|
43|63|93|
44|64|94|
aaa4|bbb4|ccc4

After split

First file output should be (File ABS_A_xxxx_yyyy.txt)

aaa1|bbb1|ccc1
aaa2|bbb2|ccc2
aaa3|bbb3|ccc3
aaa4|bbb4|ccc4

Second file output should be (File ABS_B_xxxx_yyyy.txt)
11|21|31|
12|22|32|
13|23|33|
14|24|34|
31|51|71|
32|52|72|
33|53|73|
34|54|74|
41|61|81|
42|62|82|
43|63|93|
44|64|94|

I really appreciate your detailed explaination of any query posted.

Hats off !!

Regards
JC
# 7  
Old 11-19-2010
Lol ... Ok Smilie
This User Gave Thanks to ctsgnb For This Post:
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

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

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

4. Shell Programming and Scripting

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... (7 Replies)
Discussion started by: kedrick
7 Replies

5. Shell Programming and Scripting

Splitting a file in to multiple files and passing each individual file to a command

I have an input file with contents like: MainFile.dat: 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 ... (4 Replies)
Discussion started by: rkrish
4 Replies

6. Shell Programming and Scripting

Splitting single file into n files

Hi all, I am new to scripting and I have a requirement we have source file as HEADER 01.10.2010 14:32:37 NAYA TA0022 TA0000 20000001;20060612;99991231;K4;02;3 20000008;20080624;99991231;K4;02;3 20000026;19840724;99991231;KK;01;3 20000027;19840724;99991231;KK;01;3... (6 Replies)
Discussion started by: srk409
6 Replies

7. UNIX for Advanced & Expert Users

Splitting a file into small files

Hi Folks, Please help me in solving the problem. I want to write script in order to split a file into small pieces and send it automatically through mail. Ex. The file name is CALM*.txt . It is around 50 MB. I want to split the file into 20 MB 2-3 smaller files and send (like uuencode) it... (6 Replies)
Discussion started by: piyushbhashkar
6 Replies

8. Shell Programming and Scripting

Data Splitting into two files from one file

I have a file as: I/P File: 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): Ground Car (3 Replies)
Discussion started by: karumudi7
3 Replies

9. Shell Programming and Scripting

Splitting the Huge file into several files...

Hi I have to write a script to split the huge file into several pieces. The file columns is | pipe delimited. The data sample is as: 6625060|1420215|07308806|N|20100120|5572477081|+0002.79|+0000.00|0004|0001|......... (3 Replies)
Discussion started by: lakteja
3 Replies

10. 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
Login or Register to Ask a Question