Help please, extract multiple lines from a text file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help please, extract multiple lines from a text file
# 1  
Old 04-14-2010
Data Help please, extract multiple lines from a text file

Hi all,

I need to extract lines between the lines 'RD' and 'QA' from a text file (following). there are more that one of such pattern in the file and I need to extract all of them. however, the number of lines between them is varied in the file. Therefore, I can not just use 'grep -A' command. Any suggestion would be appreciated.

Many thanks

---------------------------------------------------------
........
RD FTLH3RB02II3DW 333 0 0
GG**A***AC*AC*ACACACACAC*ACACACACACATGCTTGCACACAAT
CT*GAAATCTTCACTAA*TCTTTATCTTAAATC*AGTT*GGA*T*GTAGA
CAGACACCTGAG*TCATTTAGTCTAATGATGGC*GT*CCCTCACTC*AGG
GCAAGAAATGCTTTCCTCCA*T*GG*A*GAGGTT*AC*AG*TC*TGTT*G
*CC*TCAA*C*TCC*TGTCAGCCTCCAGCTGCTGACA*GG*A**T*CCTG
T*GCCC*TCTC*TT*GT*AGGATGGAGA*GA*TGT*GGTCCTCTATCAAT
TGTACCATCTGTGCTTGTGTGAAAGGCAGG*AC

QA 1 333 1 333
DS CHROMAT_FILE: none PHD_FILE: none TIME: Mon Jan 01 00:00:00 000
........
--------------------------------------------------
# 2  
Old 04-14-2010
try this:
Code:
sed -n '/^RD/,/^QA/p' filename | grep -v -e '^RD' -e '^QA' > newfile

The grep bit is because you did not want the QA and RD lines. There are other more terse ways to do this with awk, but they would be hard for non-awk people to generalize to other solutions. IMO.
# 3  
Old 04-14-2010
Just easy as the sed commands Smilie

Including the RD and QA lines:
Code:
awk '/^RD/{p=1}/^QA/{p=0;print}p' file

Without the RD and QA lines:
Code:
awk '/^RD/{p=1;next}/^QA/{p=0}p' file

# 4  
Old 04-14-2010
See? That's what I meant. Great code Franklin! I love it.

I love awk, too, but I don't think a DNA researcher is going to going to fall in love with awk's implied and somewhat arecane logic like the trailing {....}p trick.
I think its neat, too, but the OP probably probably doesn't. And cannot maintain it, either, which is the whole point.
# 5  
Old 04-14-2010
Quote:
Originally Posted by Franklin52
Just easy as the sed commands Smilie

Including the RD and QA lines:
Code:
awk '/^RD/{p=1}/^QA/{p=0;print}p' file

if i want these two in run time input
Code:
#!/bin/sh
file=~/test/imm_del.txt
read -p "provide your first input" read1
read -p "provide your 2nd input" read2
awk -v var1="$read1" -v var2="$read2" "/^var1/{p=1}/^var2/{p=0;print }p" $file

But it not working Smilie
# 6  
Old 04-14-2010
Quote:
Originally Posted by posix
if i want these two in run time input
Code:
#!/bin/sh
file=~/test/imm_del.txt
read -p "provide your first input" read1
read -p "provide your 2nd input" read2
awk -v var1="$read1" -v var2="$read2" "/^var1/{p=1}/^var2/{p=0;print }p" $file

But it not working Smilie
With awk variables it should be like:
Code:
#!/bin/sh

file=~/test/imm_del.txt
read -p "provide your first input" read1
read -p "provide your 2nd input" read2

awk -v var1="$read1" -v var2="$read2" '$1==var1{p=1}$1==var2{p=0;print}p' $file

# 7  
Old 04-14-2010
Thank both of you, Jim and Franklin, very much. You are so kind.Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. UNIX for Dummies Questions & Answers

How to grep multiple lines from a text file using another text file?

I would like to use grep to select multiple lines from a text file using a single-column text file. Basically I want to only select lines from the first text file where the second column of the first text file matches the second text file. How do I go about doing that? Thanks! (5 Replies)
Discussion started by: evelibertine
5 Replies

3. Shell Programming and Scripting

Process multiple lines in a text file

Hi All I have text file like this: a=21ej c=3tiu32 e=hydkehw f=hgdiuw g=jhdkj a=klkjhvl b=dlkjhyfd a=yo c=8732 Any way I can process data from first a to just before of second a, and then second a to just before of 3rd one. Just fetching records like that will help, I mean... (3 Replies)
Discussion started by: sandipjee
3 Replies

4. Shell Programming and Scripting

Extracting Multiple Lines from a Text File

Hello. I am sorry if this is a common question but through all my searching, I haven't found an answer which matches what I want to do. I am looking for a sed command that will parse through a large text file and extract lines that start with specific words (which are repeated throughout the... (4 Replies)
Discussion started by: MrDumbQuestion
4 Replies

5. Shell Programming and Scripting

Extract strings from multiple lines into one csv file

Hi all, Please go through my requirement. I have a log file in the location /opt/WebSphere61/AppServer/profiles/EMQbatchprofile/logs/EMQbatch This file contains the follwing pattern data <af type="tenured" id="42" timestamp="May 14 13:44:13 2011" intervalms="955.624"> <minimum... (8 Replies)
Discussion started by: satish.vampire
8 Replies

6. Shell Programming and Scripting

Extract strings from multiple lines into one file -

input file Desired csv output gc_type, date/time, milli secs af, Mar 17 13:09:04 2011, 144.596 af, Mar 20 00:37:37 2011, 144.242 af, ar 20 21:30:59 2011, 108.518 Hi All, Any help in acheiving the above would be appreciated. I would like to parse through lines within one file and... (5 Replies)
Discussion started by: satish.vampire
5 Replies

7. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

8. Shell Programming and Scripting

extract particular lines from text file

I have two files file A which have a number in every row and file B which contains few hundred thousand rows with about 300 characters in each row (csv) What I need is to extract whole rows from B file (only these which numbers are indicated in A file) I also need to use cygwin. Any... (7 Replies)
Discussion started by: gunio
7 Replies

9. Shell Programming and Scripting

extract the lines between specific line number from a text file

Hi I want to extract certain text between two line numbers like 23234234324 and 54446655567567 How do I do this with a simple sed or awk command? Thank you. ---------- Post updated at 06:16 PM ---------- Previous update was at 05:55 PM ---------- found it: sed -n '#1,#2p'... (1 Reply)
Discussion started by: return_user
1 Replies

10. Shell Programming and Scripting

Extract multiple repeated data from a text file

Hi, I need to extract data from a text file in which data has a pattern. I need to extract all repeated pattern and then save it to different files. example: input is: ST*867*000352214 BPT*00*1000352214*090311 SE*1*1 ST*867*000352215 BPT*00*1000352214*090311 SE*1*2 ... (5 Replies)
Discussion started by: apjneeraj
5 Replies
Login or Register to Ask a Question