extract lines from data


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers extract lines from data
# 1  
Old 04-05-2010
extract lines from data

I have a file that looks like this:

Code:
 >cel-miR-35 MIMAT6 C eles le
UCACCGGGUGGAAACUAGCAGU
>hsa-let-7a MI062 H sa
UGAGGUAGUAGGUUGUAUAGUU
>cel-miR-36 M007 Ca ele 
UCACCGGGUGAAAAUUCGCAUG
>hsa-let-7b MI63 Hmo sns le
UGAGGUAGUAGGUUGUGUGGUU

I would like to extract all the lines that start with '>hsa' and the next line so the output should look like this:

Code:
 >hsa-let-7a MI062 H sa
UGAGGUAGUAGGUUGUAUAGUU
>hsa-let-7b MI63 Hmo sns le
UGAGGUAGUAGGUUGUGUGGUU

# 2  
Old 04-06-2010
Code:
grep '^>hsa' file

# 3  
Old 04-06-2010
Quote:
Originally Posted by anbu23
Code:
grep '^>hsa' file

i need the next line also.
# 4  
Old 04-06-2010
Code:
 grep -A1 hsa sample

# 5  
Old 04-06-2010
Or if your grep doesn't support -A option...

Code:
awk '/^>hsa/{print;getline;print}' infile

# 6  
Old 04-06-2010
Code:
sed -n '/^>hsa/{N;p;}' file

# 7  
Old 04-06-2010
Quote:
Originally Posted by phoenix_nebula
Code:
 grep -A1 hsa sample

Thank you, it does what I want.
I have a follow up question: how do I combine the -A option with the -v option to get all the line except the one statrting with '>hsa' and the next line?

infile
Code:
>cel-miR-35 MIMAT6 C eles le
UCACCGGGUGGAAACUAGCAGU
>hsa-let-7a MI062 H sa
UGAGGUAGUAGGUUGUAUAGUU
>cel-miR-36 M007 Ca ele 
UCACCGGGUGAAAAUUCGCAUG
>hsa-let-7b MI63 Hmo sns le
UGAGGUAGUAGGUUGUGUGGUU

outfile should look like this:
Code:
 >cel-miR-35 MIMAT6 C eles le
UCACCGGGUGGAAACUAGCAGU
>cel-miR-36 M007 Ca ele 
UCACCGGGUGAAAAUUCGCAUG

I tried:
Code:
grep -A1 -v hsa infile

the outfile was the same as the infile.

---------- Post updated at 09:55 AM ---------- Previous update was at 09:48 AM ----------

Quote:
Originally Posted by malcomex999
Or if your grep doesn't support -A option...

Code:
awk '/^>hsa/{print;getline;print}' infile

I also tried to print only lines which do NOT match '>hsa' with
Code:
awk '!/^>hsa/{print;getline;print}' infile

I got
Code:
>cel-miR-35 MIMAT6 C eles le
UCACCGGGUGGAAACUAGCAGU
UGAGGUAGUAGGUUGUAUAGUU
>cel-miR-36 M007 Ca ele 
UCACCGGGUGAAAAUUCGCAUG
>hsa-let-7b MI63 Hmo sns le
UGAGGUAGUAGGUUGUGUGGUU
UGAGGUAGUAGGUUGUGUGGUU

I should get
Code:
>cel-miR-35 MIMAT6 C eles le
UCACCGGGUGGAAACUAGCAGU
>cel-miR-36 M007 Ca ele 
UCACCGGGUGAAAAUUCGCAUG

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

2. Shell Programming and Scripting

Extract header data from one file and combine it with data from another file

Hi, Great minds, I have some files, in fact header files, of CTD profiler, I tried a lot C programming, could not get output as I was expected, because my programming skills are very poor, finally, joined unix forum with the hope that, I may get what I want, from you people, Here I have attached... (17 Replies)
Discussion started by: nex_asp
17 Replies

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

4. UNIX for Dummies Questions & Answers

Extract lines with specific words with addition 2 lines before and after

Dear all, Greetings. I would like to ask for your help to extract lines with specific words in addition 2 lines before and after these lines by using awk or sed. For example, the input file is: 1 ak1 abc1.0 1 ak2 abc1.0 1 ak3 abc1.0 1 ak4 abc1.0 1 ak5 abc1.1 1 ak6 abc1.1 1 ak7... (7 Replies)
Discussion started by: Amanda Low
7 Replies

5. Shell Programming and Scripting

Help in replacing two blank lines with two lines of diff data

Hi.. I'm facing a trouble in replacing two blank lines in a file using shell script... I used sed to search a line and insert two blank lines after the searchd line using the following sed command. sed "/data/{G;G;}/" filename . In the file, after data tag, two lines got inserted blank lines..... (4 Replies)
Discussion started by: arjun_arippa
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

Extract specific data content from a long list of data

My input: Data name: ABC001 Data length: 1000 Detail info Data Direction Start_time End_time Length 1 forward 10 100 90 1 forward 15 200 185 2 reverse 50 500 450 Data name: XFG110 Data length: 100 Detail info Data Direction Start_time End_time Length 1 forward 50 100 50 ... (11 Replies)
Discussion started by: patrick87
11 Replies

8. Shell Programming and Scripting

Extract data based on match against one column data from a long list data

My input file: data_5 Ali 422 2.00E-45 102/253 140/253 24 data_3 Abu 202 60.00E-45 12/23 140/23 28 data_1 Ahmad 256 7.00E-45 120/235 140/235 22 data_4 Aman 365 8.00E-45 15/65 140/65 20 data_10 Jones 869 9.00E-45 65/253 140/253 18... (12 Replies)
Discussion started by: patrick87
12 Replies

9. Shell Programming and Scripting

how to extract the data ?

Hi, I'm trying to pick out a data field eg. from below. I need the required field as below but they are filled sometimes with weird chars like \-(. or watever. How can I accurately extract the 3rd field in shell? :confused: ID IDNO - REQUIRED FIELD ID 1447 - MAT620BR. ID 1452 -... (13 Replies)
Discussion started by: uxnoob
13 Replies

10. Shell Programming and Scripting

extract data from a data matrix with filter criteria

Here is what old matrix look like, IDs X1 X2 Y1 Y2 10914061 -0.364613333 -0.362922333 0.001691 -0.450094667 10855062 0.845956333 0.860396667 0.014440333 1.483899333... (7 Replies)
Discussion started by: ssshen
7 Replies
Login or Register to Ask a Question