LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-28-2011, 03:34 AM   #1
Lokelo
LQ Newbie
 
Registered: Nov 2011
Location: Townsville
Posts: 15

Rep: Reputation: Disabled
Separating interleaved lines into two different files


After managing to solve a few minor programming issues on my own (I assume in a rather crude way, but they work) I got stuck with another problem I'm not really sure how to solve.

I have a file with interleaved data:

Code:
@HWI-ST261:396:I0D48ABXX:8:1101:1354:2103/1
ATCCGATTGGATTAACATAGTGCTACACTTAATAGATGCCGTTGGCCTCCTCCCAACGAGACTTGACTTTAGTCT
+
HHJJJIJJIJIJJJJJIJIJIIJJIJJJJJIGIIIIIJJJJIIGGHIJJJJIHGIJHHFFEFEECEEDDDDDDDE
@HWI-ST261:396:I0D48ABXX:8:1101:1354:2103/2
CCTCGCGCGTGTGCCTTTCTGGAACGGCAAAGATTCGGAAGCGATGAAGAACGCGGAAAGGACCAAGGAGATGAT
+
HHJJJJJJJHHHIJIJJJJJJJIIHHIJJJJJJJJIHHHHFFDDDDDCDDDD=BBBDD>ABDDDDDDDBB?>@C@
@HWI-ST261:396:I0D48ABXX:8:1101:1536:2081/1
CTCCACCCACCCGATCGTGCAGCACAACTCTTACACTGCCTGGTTTGCAGAGGGAGAGGCAAGCGAGGCGCAGCT
+
HHJIJJJJJJJJJIJJHBGHIJJJJIHGIIIIJIIGEHHHGEF@DFCEEECDBBBB?B@BDD????<@><5<<@A
@HWI-ST261:396:I0D48ABXX:8:1101:1536:2081/2
GTGCGAGGAGGAAGACATTGGAGAAGACGGAGAACTGCACGACGAGCTCCCTCAGCTGCGCCTCGCTTGCCTCTC
+
HFHIJGIJEIJGGHEHIJJJJIJIGIJIHJJIIAFHIJJHIJHHFDBDDDDDD?CD>CDDB@>BDD<ABBCCCCC
@HWI-ST261:396:I0D48ABXX:8:1101:1721:2085/1
CCAGCGACATTTTCGAACGTTACTGGGTCGTTCTTCACATCTCGTGGACCATCAAATGGCTCCATGGAGCAATCG
+
HHGHGHGIIIHIIIGIIIIHIGIIIIIGHIII@GGAC==@CGGCCAEDDF<@C;>;;>CDDDBCC(>@AA(5>CB
@HWI-ST261:396:I0D48ABXX:8:1101:1721:2085/2
GCTCGAGCACCATGTGGACATCGTCAAGCTGAAGGACTGCCAGTGCGACTTGCAGCCAGAGTTTGTCGATTGCTC
+
HHJJIJIGIJJCHIIIIIJGGHHCGIIJJGHHIJGIIJIDHHIIIIIJIFHFHDDB?>@BC6>CDDD8?8>7:55
@HWI-ST261:396:I0D48ABXX:8:1101:1861:2094/1
GCCTCGCTCGCCTTCTTGCGAACCACCTTCTTCCCATCATTGCTATCACCACTATTGCCATCCTTCGAGTCCTTC
+
HHJIJJGIJJJJJJIGIJGIJIEIIIIIIIIIHHIJIFHFHHHEEECCEC@CECCDDDDDCDDDDCABC>CCDDC
@HWI-ST261:396:I0D48ABXX:8:1101:1861:2094/2
CGAACTTGTTCGATGACAATCAGACATTCGCCGGCTCTTTCATGCCCATCTTTGGTAAGGGTGACCCGATCATTA
+
HHGIIIIIHJJJGGJJJJJJJJJIJJIJJJJHHGHEGHIHIGJJHHHHHFFFFFFDC>AC?=BBDDDDDBBB>@A
The lines starting with @HWI are the ID of the data in the next 3 lines, so every 4 lines are one data entry.

I would like to have all the data of the ID's ending in /1 in one file and with /2 in another file and they need to be in the exact same order.

I know I can pull out single lines with awk and grep, but I'm not sure how I could pull out the following three lines, too.
 
Old 11-28-2011, 04:07 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Here's a "dirty", but working solution:
Code:
#!/bin/bash

awk '
/^@HWI.*\/1/ { print > "file1"
  getline ; print  > "file1"
  getline ; print  > "file1"
  getline ; print > "file1"
}
/^@HWI.*\/2/ { print > "file2"
  getline ; print  > "file2"
  getline ; print  > "file2"
  getline ; print > "file2"
}
' infile
Hope this helps.
 
1 members found this post helpful.
Old 11-28-2011, 04:26 AM   #3
Lokelo
LQ Newbie
 
Registered: Nov 2011
Location: Townsville
Posts: 15

Original Poster
Rep: Reputation: Disabled
It seems to work just fine! thanks!
 
Old 11-28-2011, 04:33 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
You're welcome
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing lines in two files? needhelp12 Linux - Newbie 3 11-13-2011 12:48 AM
Interleaved/Multichannel audio programming with ALSA on linux-2.6.23.9 kernel Mohan.Sundram Linux - Kernel 0 08-27-2008 06:31 AM
delete lines from files bharatbsharma Programming 5 10-25-2007 09:43 AM
reading non-interleaved raw data performance problem James_dean Programming 1 01-26-2006 01:20 AM
Extracting Lines from files supreme_command Linux - Newbie 1 05-12-2004 05:21 AM


All times are GMT -5. The time now is 07:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration