extract x lines after a pattern - place each result in separate file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract x lines after a pattern - place each result in separate file
# 1  
Old 06-06-2008
extract x lines after a pattern - place each result in separate file

Hi all,

I have many files that have 1 or more occurrences of the information I want. There are two distinct sets of information. I want get this info and place each occurrence in its own file.

The 3 lines before one set are

this grid
00 01 02
16 17 18
**40 lines of code I want to place in file this_xx.out**

The 3 lines before the second set are

that grid
00 01 02
16 17 18
**40 lines of code I want to place in file that_xx.out**

I do not want to have the 00 01 02 and 16 17 18 in the .out file.

The information in the next 40 lines is a matrix of hex number so there is nothing to hook onto in there to get the info.


Thanks for the help
gobi
# 2  
Old 06-06-2008
Try this:

Code:
awk '/grid$/{getline;getline;for(i=0;i<40;i++){if(getline){print}}}' file > that_xx.out

Regards
# 3  
Old 06-06-2008
Hammer & Screwdriver One solution, or at least an approach to solve

Code:
> cat file1
patt_a
01 02 03
04 05 06
a1
a2
a3
a4
patt_c
a1
patt_b
01 02 03
04 05 06
b1
b2
b3
b4
patt_d
d1
d2
patt_a
01 02 03
04 05 06
a1
a2
a3
a4

Code:
> cat sep_to2
#! /bin/bash

inf=file1
outf1=output_a
outf2=output_b
rm $outf1 2>/dev/null
rm $outf2 2>/dev/null

flag=
count=0
while read zf
   do
   if [ "$zf" = "patt_a" ] ; then
      flag="a"
      count=1
      continue
   fi
   if [ "$zf" = "patt_b" ] ; then
      flag="b"
      count=1
      continue
   fi
   if [ "$zf" = "01 02 03" ] ; then
      continue
   fi
   if [ "$zf" = "04 05 06" ] ; then
      continue
   fi

   if [ "$flag" = "a" ] ; then
      if [ "$count" -le 4 ] ; then
         echo "$zf" >>$outf1
         count=$((count+1))
      fi
   fi
   if [ "$flag" = "b" ] ; then
      if [ "$count" -le 4 ] ; then
         echo "$zf" >>$outf2
         count=$((count+1))
      fi
   fi
done<$inf

Code:
> cat output_a
a1
a2
a3
a4
a1
a2
a3
a4

Code:
> cat output_b
b1
b2
b3
b4

# 4  
Old 06-06-2008
Franklin52,

Am I missing something because this does not work.

joeyg,

Thanks! I had something similar to this but yours is better. I was hoping to get some slick awk or sed command to do this. I just don't have the time right now to go though a tutorial.

gobi
# 5  
Old 06-06-2008
Computer sed or awk can probably do better

Gobi,

I feel the same way. I see the sample solutions on this forum using sed or awk, but haven't quite got the handle on it yet. The command lines for sed and awk just look confusing!
Perhaps it is that the solutions offered using sed/awk are samples and not working code. Thus, the confusion in trying to figure out what is happening.
# 6  
Old 06-06-2008
Quote:
Originally Posted by gobi
Franklin52,

Am I missing something because this does not work.

gobi
What does'nt work? Errors? Wrong output? No output?
Try nawk if you get errors. It should work if the file has the exact format as you described.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract some characters from lines based on pattern

Hi All, i would like to get some help regarding extracting certain characters from a line grepped. blahblah{1:F01IRVTUS30XXXX0000000001}{2:I103IRVTDEF0XXXXN}{4:blah blahblah{1:F01IRVTUS30XXXX0000000001}{2:I103IRVTDEF0XXXXN}{4:blah... (10 Replies)
Discussion started by: mad man
10 Replies

2. UNIX for Beginners Questions & Answers

Ls to text file on separate lines

hi, I'm trying to print out the contents of a folder into a .txt file. The code I'm trying amongst variations is: ls -1 > filenames.txt but it prints them all on the same line ie. image102.bmpimage103.bmpimage104.bmpimage105.bmpimage106.bmp how can I change this? Please... (2 Replies)
Discussion started by: newbie100
2 Replies

3. Shell Programming and Scripting

Extract lines that match a pattern

Hi all, I got a file that contains the following content, Actually it is a part of the file content, Installing XYZ XYZA Image, API 18, revision 2 Unzipping XYZ XYZA Image, API 18, revision 2 (1%) Unzipping XYZ XYZA Image, API 18, revision 2 (96%) Unzipping XYZ XYZA Image, API 18,... (7 Replies)
Discussion started by: Kashyap
7 Replies

4. Shell Programming and Scripting

Extract a pattern from multiple lines in a file

I have a file that has some lines starts with * I want to get these lines, then get the word between "diac" and "lex". ex. file: ;;WORD AlAx *0.942490 diac:Al>ax lex:>ax_1 bw:Al/DET+>ax/NOUN+ gloss:brother pos:noun prc3:0 prc2:0 prc1:0 prc0:Al_det per:na asp:na vox:na mod:na gen:m num:s... (4 Replies)
Discussion started by: Viernes
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

extract DDL - output every match to separate file

Hi, i want to extract the 'CREATE INDEX' or 'CREATE UNIQUE INDEX' statements from a ddl file and output each match to a separate file. i was looking around the net but couldnīt find anything. a possible sed-script could be: sed -n '/CREATE*INDEX*/,/COMMIT/p' filename.ddlbut i couldnīt find out... (11 Replies)
Discussion started by: CactusMoon
11 Replies

7. Shell Programming and Scripting

How to get awk to edit in place and join all lines in text file

Hi, I lack the utter fundamentals on how to craft an awk script. I have hundreds of text files that were mangled by .doc format so all the lines are broken up so I need to join all of the lines of text into a single line. Normally I use vim command "ggVGJ" to join all lines but with so many... (3 Replies)
Discussion started by: n00ti
3 Replies

8. Shell Programming and Scripting

extract nth line of all files and print in output file on separate lines.

Hello UNIX experts, I have 124 text files in a directory. I want to extract the 45678th line of all the files sequentialy by file names. The extracted lines should be printed in the output file on seperate lines. e.g. The input Files are one.txt, two.txt, three.txt, four.txt The cat of four... (1 Reply)
Discussion started by: yogeshkumkar
1 Replies

9. Shell Programming and Scripting

Separate lines from text file

I have a text file with lot of rows like.. Action & Adventure|2012: Supernova NR|2009-11-01 00:01:00|2010-05-01 23:59:00|Active|3 Action & Adventure|50 Dead Men Walking|2010-01-05 00:01:00|2010-06-30 23:59:00|Active|3 Action & Adventure|Afterwards|2009-11-26 00:01:00|2010-03-26... (3 Replies)
Discussion started by: ramse8pc
3 Replies

10. Shell Programming and Scripting

Outputting formatted Result log file from old 30000 lines result log<help required>

Well I have a 3000 lines result log file that contains all the machine data when it does the testing... It has 3 different section that i am intrsted in 1) starting with "20071126 11:11:11 Machine Header 1" 1000 lines... "End machine header 1" 2) starting with "20071126 12:12:12 Machine... (5 Replies)
Discussion started by: vikas.iet
5 Replies
Login or Register to Ask a Question