Visit Our UNIX and Linux User Community


copy range of lines in a file based on keywords from another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting copy range of lines in a file based on keywords from another file
# 1  
Old 01-25-2012
copy range of lines in a file based on keywords from another file

Hi Guys,

I have the following problem. I have original file (org.txt) that looks like this

module v_1(.....)
//arbitrary number of text lines
endmodule

module v_2(....)
//arbitrary number of text lines
endmodule

module v_3(...)
//arbitrary number of text lines
endmodule

module v_4(...)
//arbitrary number of text lines
endmodule

module v_5(...)
//arbitrary number of text lines
endmodule


I have another file keywords.txt that has keywords for original file org.txt that looks like

v_2
v_3
v_5

What i want to do is to extract modules from org.txt based on keywords.txt and store it in a new file (filter.txt). In this case, i shall be extracting only modules v_2, v_3 and v_5 and storing them in the filter.txt as

module v_2(....)
//arbitrary number of text lines
endmodule

module v_3(...)
//arbitrary number of text lines
endmodule

module v_5(...)
//arbitrary number of text lines
endmodule

How to do that using sed and awk or any other cool way. Note that this was a toy example. The actual files are huge in terms of number of lines.

Moderator's Comments:
Mod Comment Please use next time code tags for your code and data

Last edited by vbe; 01-25-2012 at 01:45 PM..
# 2  
Old 01-25-2012
I think the sed code I am using could help you. Provided you know the size of each block of text that you are extracting.

You could try.

Code:
 sed -n '/$Var1/,/$Var2/p' < intputfile | head $blocksize > outputfile

Where Var1 and var2 are the ranges you are searching between and blocksize is the number of lines you wish to read.
# 3  
Old 01-25-2012
Hi Andy
There is no range concept here. Please see the post again as there could be any number of lines between module v_* and endmodule.
# 4  
Old 01-25-2012
Code:
$ cat module.awk

BEGIN {
        # Load the list of keywords
        while(getline <"keywords.txt") ARR[++C]=$1
}

# Only run this codeblock when not printing.
!P {
        for(N in ARR)
        if(match($0, "module[ \t]+" ARR[N] "[ \t]*\\("))
        {
                P=1 # Start printing lines
                delete ARR[N]; # delete this item to make the loop faster next time
                break; # End the loop early once we find one item
        }
# Print all lines when P is nonzero.
} P

# Stop printing when we find a line with 'endmodule'.  Also, print a blank line.
/endmodule/ { P=0; printf("\n"); }

$ cat data

module v_1(.....)
//arbitrary text
endmodule

module v_2(....)
//arbitrary text
endmodule

module v_3(...)
//arbitrary text
endmodule

module v_4(...)
//arbitrary text
endmodule

module v_5(...)
//arbitrary text
endmodule

$ awk -f module.awk data


module v_2(....)
//arbitrary text
endmodule

module v_3(...)
//arbitrary text
endmodule

module v_5(...)
//arbitrary text
endmodule


Last edited by Corona688; 01-25-2012 at 01:36 PM..
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 01-25-2012
Corona688

Dude! You are awesome. I am running the script now. When it finishes. I shall let you know.

Hat off to you. Thanks so much again.

Warm Regards,
# 6  
Old 01-25-2012
If it's just freezing, make sure keywords.txt is in the same directory you run the command in.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 01-25-2012
Its warm because its from the heart Smilie

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Copy Lines between Keywords & paste them to another file

hi, I have Multiple files with the following data : File1 100414 DR1 END XXXXX Test1 Test2 Test3 Test4 Test5 Test6 END 100514 DR2 END XXXXX Test7 Test8 Test9 Test10 Test11 Test12 END 100614 DR3 (5 Replies)
Discussion started by: newageBATMAN
5 Replies

2. Shell Programming and Scripting

Extracting words and lines based on keywords

Hello! I'm trying to process a text file and am stuck at 2 extractions. Hoping someone can help me here: 1. Given a line in a text file and given a keyword, how can I extract the word preceeding the keyword using a shell command/script? For example: Given a keyword "world" in the line: ... (2 Replies)
Discussion started by: seemad
2 Replies

3. Shell Programming and Scripting

Extract lines from file using keywords using script

Hi I need some lines of text from input file using keywords. Inputfile IP IS 10.238.52.65 pun-ras-bng-mhs-01#context bsnl.in Card Status : 1:0, 2:1, 3:1, 4:1, 5:0, 6:0, 7:0, 8:0, 9:1, 10:0, 11:0, 12:0, 13:0, 14:1, Max Circuits: 1: 0, 2: 32768, ... (5 Replies)
Discussion started by: surender reddy
5 Replies

4. Shell Programming and Scripting

extracting lines based on condition and copy to another file

hi i have an input file that contains some thing like this aaa acc aa abc1 1232 aaa abc2.... poo awq aa abc1 aaa aaa abc2 bbb bcc bb abc1 3214 bbb abc3.... bab bbc bz abc1 3214 bbb abc3.... vvv ssa as abc1 o09 aaa abc4.... azx aaq aa abc1 900 aqq abc19.... aaa aa aaaa abc1 899 aa... (8 Replies)
Discussion started by: anurupa777
8 Replies

5. Shell Programming and Scripting

Sorting lines based on keywords for MySQL script

the thing which i require is very very complex.. i tried hard to find the solution but couldnt.. the thing i need to achieve is say i have a file cat delta.sql CREATE VIEW Austin Etc etc . . . CREATE VIEW Barabara AS SELECT blah blah blah FROM Austin z, Cluster s, Instance i WHERE... (4 Replies)
Discussion started by: vivek d r
4 Replies

6. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

7. Shell Programming and Scripting

copy the contents between two keywords to a new file.

Hi All, I want to edit my gate level netlists by searching for the content between two patterns eg: ff1 \test/a0 ( .CLK(\test/ClkInt0_acb_00x1 ),.D(\test/Rakicc ), .QB(\test/X ), .VDD(1'b1), .VSS(1'b0)); ff1 \test/a1 ( .CLK(\test/medis0_acb_00x1 ),.D(\test/hedwc ), .QB(\test/X ),... (6 Replies)
Discussion started by: naveen@
6 Replies

8. Shell Programming and Scripting

How to keep appending a newly created file based on some keywords

Hi Friends, I have to create a new log file everyday and append it with content based on some keywords found in another log file. Here is what I have tried so far... grep Error /parentfolder/someLogFile.log >> /parentfolder /Archive/"testlogfile_error_`date '+%d%m%y'`.txt" grep error... (6 Replies)
Discussion started by: supreet
6 Replies

9. UNIX for Advanced & Expert Users

Copy lines from a log file based on timestamp

how to copy lines from a log file based on timestamp. INFO (RbrProcessFlifoEventSessionEJB.java:processFlight:274) - E_20080521_110754_967: rbrAciInfoObjects listing complete! INFO (RbrPnrProcessEventSessionEJB.java:processFlight:197) - Event Seq: 1647575217; Carrier: UA; Flt#: 0106; Origin:... (1 Reply)
Discussion started by: ranjiadmin
1 Replies

10. Shell Programming and Scripting

Capture lines based on keywords

Hello everyone, I am trying to write a script that will capture few lines from a text file based on 2 keywords in the first line and 1 keyword in the last one. It could also be based on the first line only + the folllowing 3 lines. Could some one help or give directions. Thanks. (4 Replies)
Discussion started by: nimo
4 Replies

Featured Tech Videos