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 ??
# 8  
Old 11-12-2010
Code:
nawk -vVAR="$v_projname" '{print > ("ABS\_"((/\|/)?"ADDR\_":"SLICE\_")VAR"\_NETWORKID.txt")}' spfile.txt

# 9  
Old 11-12-2010
MySQL

Thanks for the solution !!

Just minor correction (Typo - Missed space between -v and VAR)

nawk -v VAR="$v_projname" '{print > ("ABS\_"((/\|/)?"ADDR\_":"SLICE\_")VAR"\_NETWORKID.txt")}' spfile.txt

Regards
JC
# 10  
Old 11-12-2010
Some awk and/or nawk version support without space Smilie
This User Gave Thanks to ctsgnb For This Post:
# 11  
Old 11-17-2010
Hi
extending to my previous query ....

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

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