Generate files from one file based on lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Generate files from one file based on lines
# 1  
Old 12-20-2012
Question Generate files from one file based on lines

Hi Friends,

I have a file1

file1.txt

Code:
 
1ABC 13478 aqjerh 473 343   
2hej 478 5775 24578 23892
3fhd fg 847 brjkb f99345 487
4eh ehjk 84 47589 8947 234
5784 487 738 52895 8975
6 57489 eghe9 4575 859479 
7fnbd 4y5 4iuy  458 h irh
8fjdg 74  7845 8475 5789 
94yr 48yr  4hr  erhj  reh
10fh g 874 fr b8457 hjk 
11hgf fej efjg j ghjegf 
12hj fjg jfeg feg gjgwe 
1DEF 136578 aqjrh 43 33   
2htr 478 5775 24578 2392
3fhd fg 847 brjkb f99345 487
4eh ehjk 84 47589 8947 234
5784 487 78 52895 8975
6 57489 eghe9 4575 859479 
7fnbd 4y5 4iuy  458 h irh
8fjdg 74  7845 8475 5789 
94yr 48yr  4hr  erhj  reh
10fh g 874 fr b8457 hjk 
11hgf fej efjg j ghjegf 
12hj fjg jfeg feg gjgwe

i want to generate 2 files where frist.txt will contain the 1-9 lines which are in red in color. and second.txt will contain 1 and 10 t0 12 lines.i have this sequence through out the file.i am newbie and need help.

expected output :

Code:
 
first.txt
 
ABC 13478 aqjerh 473 343   
hej 478 5775 24578 23892
fhd fg 847 brjkb f99345 487
eh ehjk 84 47589 8947 234
784 487 738 52895 8975
 57489 eghe9 4575 859479 
fnbd 4y5 4iuy  458 h irh
fjdg 74  7845 8475 5789 
4yr 48yr  4hr  erhj  reh
DEF 136578 aqjrh 43 33   
htr 478 5775 24578 2392
fhd fg 847 brjkb f99345 487
eh ehjk 84 47589 8947 234
784 487 78 52895 8975
 57489 eghe9 4575 859479 
fnbd 4y5 4iuy  458 h irh
fjdg 74  7845 8475 5789 
4yr 48yr  4hr  erhj  reh

Code:
 
second.txt
 
ABC 13478 aqjerh 473 343 
fh g 874 fr b8457 hjk 
hgf fej efjg j ghjegf 
hj fjg jfeg feg gjgwe 
DEF 136578 aqjrh 43 33
fh g 874 fr b8457 hjk 
hgf fej efjg j ghjegf 
hj fjg jfeg feg gjgwe

plz help
# 2  
Old 12-20-2012
The following seems to do what you want:
Code:
awk -v f1=first.txt -v f2=second.txt '
(NR - 1) % 12 <= 8 {print > f1}
(NR - 1) % 12 == 0 || (NR - 1) % 12 > 8 {print > f2}
' file1.txt

but the results don't quite match your sample files because you have different numbers of trailing spaces between lines 1 and 13 in file1.txt and what you said you want to see on lines 1 and 5 of the output to be placed in second.txt.
# 3  
Old 12-20-2012
In the below line, why it was not considered as 94 ?

Code:
 
94yr 48yr  4hr  erhj  reh

---------- Post updated at 11:34 AM ---------- Previous update was at 11:31 AM ----------

Code:
 
$ nawk '{a=substr($1,1,2)+0;sub("^[0-9]","",$0)}a<=9||a>12{print >> "first.txt"}a==1||a>=10&&a<=12{sub("^[0-9]","",$0);print >>"second.txt"}' a.txt
 
$ cat first.txt
ABC 13478 aqjerh 473 343   
hej 478 5775 24578 23892
fhd fg 847 brjkb f99345 487
eh ehjk 84 47589 8947 234
784 487 738 52895 8975
 57489 eghe9 4575 859479 
fnbd 4y5 4iuy  458 h irh
fjdg 74  7845 8475 5789 
4yr 48yr  4hr  erhj  reh
DEF 136578 aqjrh 43 33   
htr 478 5775 24578 2392
fhd fg 847 brjkb f99345 487
eh ehjk 84 47589 8947 234
784 487 78 52895 8975
 57489 eghe9 4575 859479 
fnbd 4y5 4iuy  458 h irh
fjdg 74  7845 8475 5789 
4yr 48yr  4hr  erhj  reh
 
$ cat second.txt 
ABC 13478 aqjerh 473 343   
fh g 874 fr b8457 hjk 
hgf fej efjg j ghjegf 
hj fjg jfeg feg gjgwe 
DEF 136578 aqjrh 43 33   
fh g 874 fr b8457 hjk 
hgf fej efjg j ghjegf 
hj fjg jfeg feg gjgwe

# 4  
Old 12-20-2012
Quote:
Originally Posted by itkamaraj
In the below line, why it was not considered as 94 ?

Code:
 
94yr 48yr  4hr  erhj  reh

---------- Post updated at 11:34 AM ---------- Previous update was at 11:31 AM ----------

Code:
 
$ nawk '{a=substr($1,1,2)+0;sub("^[0-9]","",$0)}a<=9||a>12{print >> "first.txt"}a==1||a>=10&&a<=12{sub("^[0-9]","",$0);print >>"second.txt"}' a.txt
 
$ cat first.txt
ABC 13478 aqjerh 473 343   
hej 478 5775 24578 23892
fhd fg 847 brjkb f99345 487
eh ehjk 84 47589 8947 234
784 487 738 52895 8975
 57489 eghe9 4575 859479 
fnbd 4y5 4iuy  458 h irh
fjdg 74  7845 8475 5789 
4yr 48yr  4hr  erhj  reh
DEF 136578 aqjrh 43 33   
htr 478 5775 24578 2392
fhd fg 847 brjkb f99345 487
eh ehjk 84 47589 8947 234
784 487 78 52895 8975
 57489 eghe9 4575 859479 
fnbd 4y5 4iuy  458 h irh
fjdg 74  7845 8475 5789 
4yr 48yr  4hr  erhj  reh
 
$ cat second.txt 
ABC 13478 aqjerh 473 343   
fh g 874 fr b8457 hjk 
hgf fej efjg j ghjegf 
hj fjg jfeg feg gjgwe 
DEF 136578 aqjrh 43 33   
fh g 874 fr b8457 hjk 
hgf fej efjg j ghjegf 
hj fjg jfeg feg gjgwe

Hi itkamaraj,
If you look back at message #1 in this thread, you'll see that the line you asked about is shown as:
Code:
94yr 48yr  4hr  erhj  reh

rather than
Code:
94yr 48yr  4hr  erhj  reh

(the 9 is in red, but the 4 is in black).

I assumed that the intent was that the numbers in red are line numbers (in repeating sets numbered from 1 through 12) used for discussion, but not actually present in the input file. Your code sample assumes that these numbers in red actually appear in the file.

i150371485 needs to clarify the intent for us to find out which assumption was correct.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In PErl script: need to read the data one file and generate multiple files based on the data

We have the data looks like below in a log file. I want to generat files based on the string between two hash(#) symbol like below Source: #ext1#test1.tale2 drop #ext1#test11.tale21 drop #ext1#test123.tale21 drop #ext2#test1.tale21 drop #ext2#test12.tale21 drop #ext3#test11.tale21 drop... (5 Replies)
Discussion started by: Sanjeev G
5 Replies

2. UNIX for Beginners Questions & Answers

Split file into multiple files based on empty lines

I am using below code to split files based on blank lines but it does not work. awk 'BEGIN{i=0}{RS="";}{x="F"++i;}{print > x;}' Your help would be highly appreciated find attachment of sample.txt file (2 Replies)
Discussion started by: imranrasheedamu
2 Replies

3. Shell Programming and Scripting

Delete specific lines from files based on another file

I have some text files in a folder named ff as follows. I need to delete the lines (in-place editing)in these files based on another file aa.txt. 32bm.txt: 249 253 A P - 0 0 8 0, 0.0 6,-1.4 0, 0.0 2,-0.4 -0.287 25.6-102.0 -74.4 161.1 37.1 13.3 10.9 250... (2 Replies)
Discussion started by: aden
2 Replies

4. Shell Programming and Scripting

Extracting lines from text files in folder based on the numbers in another file

Hello, I have a file ff.txt that looks as follows *ABNA.txt 356 24 36 112 *AC24.txt 457 458 321 2 ABNA.txt and AC24.txt are the files in the folder named foo1. Based on the numbers in the ff.txt file, I want to extract the lines from the corresponding files in the foo1 folder and... (2 Replies)
Discussion started by: mohamad
2 Replies

5. Shell Programming and Scripting

compare 2 files and return unique lines in each file (based on condition)

hi my problem is little complicated one. i have 2 files which appear like this file 1 abbsss:aa:22:34:as akl abc 1234 mkilll:as:ss:23:qs asc abc 0987 mlopii:cd:wq:24:as asd abc 7866 file2 lkoaa:as:24:32:sa alk abc 3245 lkmo:as:34:43:qs qsa abc 0987 kloia:ds:45:56:sa acq abc 7805 i... (5 Replies)
Discussion started by: anurupa777
5 Replies

6. Shell Programming and Scripting

Generate zone files based off subnet

Hi, I'm attempting to generate zone files based off the subnet address of a reverse zone. For example... My text file contains the following.. 5.4.7.0/24 5.4.7.0/24 68.74.23.0/24 68.74.24.0/24 I want to create a separate file for each of the listed blocks. Each file should contain... (1 Reply)
Discussion started by: spartan22
1 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. Shell Programming and Scripting

generate report based on data in files.

Hi All, I need to develop a shell script which does sanity check of a data file, as below. 1. For DATE columns, it should check if date is given in proper format or not? For example, if format of date is expected as DD-MON-YYYY HH24:MI:SS and we received the date in formation like DDMMYYYY HH24,... (1 Reply)
Discussion started by: ace_friends22
1 Replies

9. Shell Programming and Scripting

Adding lines to files based on file extension

I have posted this before but did not get many replies, so here it goes again. I have several files name like this If the file extension is 1a, I woould like to add at the beggining of the file the following sequence If the file extension is 1b, thn the entry that should be added is the next... (2 Replies)
Discussion started by: Xterra
2 Replies

10. UNIX for Dummies Questions & Answers

deleting lines from a delimited files based on a 2nd file

I need a little help... I have a file with 3 fields, Time/Date, IP address, UniqueID I have a 2nd file of UniqueIDs. I want to either (which ever is easier): 1. delete entries in file 1 that have a UniqueID in file 2 2. create a new file with the fields from File 1, excluding the... (4 Replies)
Discussion started by: goldie363
4 Replies
Login or Register to Ask a Question