Extract pattern before two newlines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract pattern before two newlines
# 1  
Old 09-13-2011
Extract pattern before two newlines

Hi All,

My file looks like this:

Code:
1 2 3
3 4 5
6 7 8
8 7 6


3 4 5
3 6 7
3 4 5


1 2 4
3 4 6
2 4 6

As you can see there are two newlines after the next pattern of numbers begin.

I want to extract the patterns and put them in separate files like:
1.dat

Code:
1 2 3
3 4 5
6 7 8
8 7 6

2.dat

Code:
3 4 5
3 6 7
3 4 5

3.dat
Code:
1 2 4
3 4 6
2 4 6

and so on until the end of the file. I am using Linux with BASH.

I tried with this but it did not work, moreover it cannot create multiple DAT files:

Code:
sed 's/ //g' INPUTFILE

# 2  
Old 09-13-2011
First, can you tell, what is the below code is doing. and why you issued the below sed command for your work?

Code:
sed 's/ //g' INPUTFILE

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 09-13-2011
ok. I was issuing the wrong command. Since, I was trying with various commands. I realize that my command was utter nonsense.

After a bit of searching, I could come up with this:
Code:
awk '/\n\n/{c++}{print > c ".dat"}' INPUTFILE

I am not that expert in this but reading in the file until two newlines should help me get the patterns which I want. The variable c is a counter which creates files from 1 until N
# 4  
Old 09-13-2011
Code:
awk '{print > ++c ".dat"}' RS= file

This User Gave Thanks to Franklin52 For This Post:
# 5  
Old 09-13-2011
Code:
 
$ nawk 'BEGIN{RS="";FS="\n\n"} {c++;f=c".dat"; printf("%s\n",$0) >> f}' test  
 
$ cat 1.dat 
1 2 3
3 4 5
6 7 8
8 7 6

$ cat 2.dat 
3 4 5
3 6 7
3 4 5

$ cat 3.dat 
1 2 4
3 4 6
2 4 6

This User Gave Thanks to itkamaraj 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

Extract whole word preceding a specific character pattern with first occurence of the pattern

Hello. Here is a file contents : declare -Ax NEW_FORCE_IGNORE_ARRAY=(="§" ="§" ="§" ="§" ="§" .................. ="§"Here is a pattern =I want to extract 'NEW_FORCE_IGNORE_ARRAY' which is the whole word before the first occurrence of pattern '=' Is there a better solution than mine :... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

sed a multiple line pattern that has newlines followed by text and r

Here is the text that I was to run sed on. In this text I want to insert a semi colon ';' before 'select a13.STORE_TYPE STORE_TYPE,' and after 'from ZZMR00 pa11' Input text: insert into ZZMQ01 select pa11.STATE_NBR STATE_NBR, pa11.STORE_TYPE STORE_TYPE, ... (9 Replies)
Discussion started by: v_vineeta11
9 Replies

3. Shell Programming and Scripting

Extract pattern from file

hi, file is having entries like below NRTMRC1=some value NRTSMO1=some value NRTMAA1=some value NRTSCO1=some value NRTSSMA1=some value NRTMRC11=some value NRTSMO11=some value NRTMAA11=some value NRTSCO11=some value NRTSSMA11=some alue NRTSSMMAA11=some value MNRMRC1=some value... (4 Replies)
Discussion started by: deepakiniimt
4 Replies

4. Shell Programming and Scripting

Need pattern Extract

2014-05-31-18.22.18.500158-240 E11115478A502 LEVEL: Info PID : 25100668 TID : 73282 PROC : db2sysc 0 INSTANCE: udbin001 NODE : 000 DB : APPHDL : 0-18345 APPID: *LOCAL.udbin001.140531200018 AUTHID : udbin001 EDUID : 73282 ... (4 Replies)
Discussion started by: ilugopal
4 Replies

5. Shell Programming and Scripting

Search for a pattern,extract value(s) from next line, extract lines having those extracted value(s)

I have hundreds of files to process. In each file I need to look for a pattern then extract value(s) from next line and then search for value(s) selected from point (2) in the same file at a specific position. HEADER ELECTRON TRANSPORT 18-MAR-98 1A7V TITLE CYTOCHROME... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

6. Shell Programming and Scripting

How to extract the number from the pattern

Hi I have a file with the following entries: cm2363db cm2445db cm2618db Is there a awk or sed command which would allow me to extract only the numbers from this file as output, so the output would look like this: 2363 2445 2618 Thanks a lot -A (4 Replies)
Discussion started by: aoussenko
4 Replies

7. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

8. Shell Programming and Scripting

extract based on pattern

I have a mail log file and I want to extract some lines belonging to one domain. For example Input File: Dec 12 03:15:28 postfix/smtpd: 3F481EB0295: client=unknown, sasl_method=PLAIN, sasl_username=abcd@xyz.com Dec 12 03:22:08 postfix/smtpd: 60B56EE001D: client=5ad9b9ba.com,... (7 Replies)
Discussion started by: Bijayant Kumar
7 Replies

9. Shell Programming and Scripting

pattern extract

Hi I have a pattern like : SYSTEM_NAME-232-S7-200810060949.LOG Here I need to extract system name and the timestamp and also the numeric number after "-S" i.e 7 here . I am not very sure of whether I should use sed / awk for this ?:confused: Thanks, Priya. (6 Replies)
Discussion started by: priyam
6 Replies

10. UNIX for Dummies Questions & Answers

Extract the Pattern

Hello All, can anyone help me out in extracting the pattern from a file... The Input file is: NFS B.11.11 ONC/NFS; Network-FileSystem,InformationServices,Utilities|123 NParProvider B.11.11.01.04.01.01 nPartition Provider|456 NPartition A.01.02 Enhanced NPartition Commands/789... (6 Replies)
Discussion started by: aajan
6 Replies
Login or Register to Ask a Question