Fetch lines between 1st and 4th similar pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fetch lines between 1st and 4th similar pattern
# 1  
Old 05-24-2017
Fetch lines between 1st and 4th similar pattern

Hi Folks,

I have a big file that looks like below

Code:
==========
kjhjl
kjlklkkhcgflj
fgf
==========
xsww23edc
ccdde3rfv
ceerfcc
vff4
==========
zaq12wsx
xsw23edc
==========

==========
zxcvb
asdfg
qwert
==========
1qaz2wsx
2wsx3edc
==========
4rfv5tgb
6yn7ujm
==========

Now I need to fetch the lines between the first "==" to fourth "==" in a loop and put only this contents between the 1st and 4th "==" in the separate file. Now I should have 2 files as seen below

Code:
File 1 :

==========
kjhjl
kjlklkkhcgflj
fgf
==========
xsww23edc
ccdde3rfv
ceerfcc
vff4
==========
zaq12wsx
xsw23edc
==========

Code:
File 2:

==========
zxcvb
asdfg
qwert
==========
1qaz2wsx
2wsx3edc
==========
4rfv5tgb
6yn7ujm
==========

Please help me with this
# 2  
Old 05-24-2017
Hello jayadanabalan,

Could you please try following and let me know if you have any queries.
Code:
awk -v B=1 '/^==/{A++}  {print > "File "B} A==4{B++;A=""}'   Input_file

Thanks,
R. Singh
# 3  
Old 05-24-2017
works like a charm !!!!

I got another issue actually.

Say my bulk file is like seen below

Code:
==============
= Fruit      : lkjlkjkl
= Veggie   : rdhghjk
==============
zaq12wsx
xsw23edc
cde34rfv
==============
= Done     :  May 05
= Time     :  07:00 AM
==============

==============
= Fruit      : gfdgfhf
= Veggie   : mjgwecj
==============
zaq12wsx
xsw23edc
cde34rfv

==============
= Fruit      : fgkjkl
= Veggie   : xfdxgfc
==============
zaq12wsx
xsw23edc
cde34rfv
==============
= Done     : May 01
= Time     : 05:00 PM
==============

If you see the second lot, there is no Done and the log is incomplete. So the 3rd and 4th "==" is missing here.

How do I separate this alone ?
# 4  
Old 05-24-2017
Moderator's Comments:
Mod Comment Your original problem was solved and your new problem depends on data that was not present in your original post. Please start a new thread for a new problem.

And, note that unless you provide more details such that the start of a transaction can be reliably identified, there might not be any way to guess at what you really want to happen. Although the second line of each transaction in your second sample input file, you have not stated that that will be true in all correctly formatted transactions. Trying to guess at requirements from a two and a half transaction sample is dangerous.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. Shell Programming and Scripting

How to dynamically fetch lines after a match?

Hi Friends, How to fetch current hour data from a log file, given below? I want all lines after the match "Wed Aug 13 16:" I have tried below command, but not working. If I put exact string, then it is working. cat /iscp/user/monitor/ORA_errors |awk '/`date +%h" "%d" "%h`/,printed==999 {... (7 Replies)
Discussion started by: suresh3566
7 Replies

3. Shell Programming and Scripting

Fetch the lines which contains special characters

Hi All, My source file contains special characters(Latin characters).I need to fetch only the lines which contains the special characters. The problem is i don't know which all latin/special characters can come in the source. Is there anyway to extract the lines which contain letters other... (3 Replies)
Discussion started by: joe!!
3 Replies

4. Shell Programming and Scripting

Fetch entries with specific pattern

Hi all, I have following sample input file which is a part of big file: ID AINX_HUMAN Reviewed; 499 AA. AC Q16352; B1AQK0; Q9BRC5; DT 30-MAY-2000, integrated into UniProtKB/Swiss-Prot. DT 23-JAN-2002, sequence version 2. DT 28-NOV-2012, entry version 123.... (2 Replies)
Discussion started by: kareena
2 Replies

5. Shell Programming and Scripting

Join all the lines matching similar pattern

I am trying to Join all the lines matching similar pattern. Example ; I wanted to join all the lines which has sam to a single line. In next line, i wanted to have all the lines with jones to a single line....etc > cat sample.txt sam 2012/11/23 sam 2012/12/5 sam 2012/12/5 jones... (2 Replies)
Discussion started by: evrurs
2 Replies

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

7. Shell Programming and Scripting

match sentence and word adn fetch similar words in alist

Hi all, I have ot match sentence list and word list anf fetch similar words in a separate file second file with 2 columns So I want the output shuld be 2 columns like this (3 Replies)
Discussion started by: manigrover
3 Replies

8. UNIX for Dummies Questions & Answers

merge lines within a file that start with a similar pattern

Hello! i have a text file.. which contains the data as follows i want to merge the declarations lines pertaining to one datatype in to a single line as follows i've searched the forum for help.. but couldn't find much help.. how can i do this?? (1 Reply)
Discussion started by: a_ba
1 Replies

9. Shell Programming and Scripting

Grep and fetch subsequent lines also

Hi, I need to grep a pattern and fetch subsequent lines till end of the data-set. E.g., i have a file like: AA 1111 23 34 BB 45 56 78 CC 22 44 AA 2222 78 34 56 BB 22 56 67 68 23 CC 56 78 DD 33 55 77 AA 3333 46 BB 58 79 In above file i have 3-data sets where each set starts with... (6 Replies)
Discussion started by: prvnrk
6 Replies

10. Shell Programming and Scripting

display content between all similar tags pattern

hi, I m stuck at a point for more than 3days. My doubt is pretty simple.. I have a web page content in $content. ( got this by using LWP).. Now I want to display the content matching a pattern. I tried if($content =~ m{<div class="abc">(.*?)</div>}s){ print $1;} that will... (4 Replies)
Discussion started by: therockravi
4 Replies
Login or Register to Ask a Question