How to find all the multi line pattern and redirecting it to a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find all the multi line pattern and redirecting it to a file?
# 1  
Old 03-17-2017
How to find all the multi line pattern and redirecting it to a file?

I've a file like this

Code:
{multi line
     .......
     .......
  pattern}
{
some other stuff
     .........
}
{multi line
     .......
     .......
  pattern}
{
some other stuff
     .........
}

and so on

I want to redirect every multiline pattern in a new file using awk

---------- Post updated at 07:10 PM ---------- Previous update was at 07:05 PM ----------

I'm using the following command

awk '/start pattern/,/end pattern/' file_to_search > file_to_redirect
it's working but it is only extracting first pattern not the every multiline pattern

Last edited by vgersh99; 03-17-2017 at 10:57 AM.. Reason: code tags, please
# 2  
Old 03-17-2017
Quote:
Originally Posted by aamir_raihan
I've a file like this

Code:
{multi line
     .......
     .......
  pattern}
{
some other stuff
     .........
}
{multi line
     .......
     .......
  pattern}
{
some other stuff
     .........
}

and so on

I want to redirect every multiline pattern in a new file using awk

---------- Post updated at 07:10 PM ---------- Previous update was at 07:05 PM ----------

I'm using the following command

awk '/start pattern/,/end pattern/' file_to_search > file_to_redirect
it's working but it is only extracting first pattern not the every multiline pattern
I'd say your startPat/endPat pai is good ONLY for the first 'block'.
Any leading/trailing spaces/tabs/etc...
Make sure it works for the others....

A sample would help as well....
# 3  
Old 03-17-2017
I would suggest using csplit instead. It allows you to split a file based on a condition.

Make sure that the target directory for the files is empty before you start to make working through them/cleaning up easier later.


Does this help?
Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

2. Shell Programming and Scripting

sed multiple multi line blocks of text containing pattern

Hi, I have a log file which has sessionids in it, each block in the log starts with a date entry, a block may be a single line or multiple lines. I need to sed (or awk) out the lines/blocks with that start with a date and include the session id. The files are large at several Gb. My... (3 Replies)
Discussion started by: andyatit
3 Replies

3. UNIX for Dummies Questions & Answers

Find next line based on pattern, if it is similar pattern skip it

Hi, I am able to get next line if it is matching a particular pattern. But i need a way to skip if next line also matches same pattern.. For example: No Records No Records Records found got it Records found Now i want to find 'Records found' after 'No Records' pattern matches.. ... (5 Replies)
Discussion started by: nagpa531
5 Replies

4. Shell Programming and Scripting

Multi-line filtering based on multi-line pattern in a file

I have a file with data records separated by multiple equals signs, as below. ========== RECORD 1 ========== RECORD 2 DATA LINE ========== RECORD 3 ========== RECORD 4 DATA LINE ========== RECORD 5 DATA LINE ========== I need to filter out all data from this file where the... (2 Replies)
Discussion started by: Finja
2 Replies

5. UNIX for Dummies Questions & Answers

find/xargs/*grep: find multi-line empty "try-catch" blocks - eg, missing ; not in a commented block

How can I recursively find all files in a directory and print out the file and first line number of any text blocks that match the below cases? This would seem to involve find, xargs, *grep, regex, etc. In summary, I want to find so-called empty "try-catch blocks" that do not contain code... (0 Replies)
Discussion started by: lifechamp
0 Replies

6. Shell Programming and Scripting

Print between multi line pattern

Hi, I have a file with text like this .SET WIDTH 10000 .SET MAXERROR 1 insert into new_db SELECT * FROM some_db ; +---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+- .SET WIDTH 10000... (3 Replies)
Discussion started by: sol_nov
3 Replies

7. Shell Programming and Scripting

Find the position of a pattern on a line from a csv file

hello I'm doing a unix program and i'm using many file csv.in each csv file the colums are separated by ";" I would like to know the position of a pattern. For example for a line yyyy, bbbb, cccc; ddddd;eeee. I will like for example by finding the position of the pattern "cccc" and the response is... (6 Replies)
Discussion started by: papis
6 Replies

8. Shell Programming and Scripting

multi line multirecord find and replace

Hello I am looking to have a script that performs some tasks for find and replace and inserts a line as well. I have done some programming 10 years ago, so it is causing me a little grief. File consists of 2500 records. I will show you a sample consisting of two records below and what needs... (3 Replies)
Discussion started by: cdc01
3 Replies

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

10. UNIX for Dummies Questions & Answers

find pattern delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: W/D FRM CHK 00 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (1 Reply)
Discussion started by: nickg
1 Replies
Login or Register to Ask a Question