Home
Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Concatenate lines between lines starting with a specific pattern

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 10-22-2010
Concatenate lines between lines starting with a specific pattern

Hi,

I have a file such as:
---
>contig00001 length=35524 numreads=2944
gACGCCGCGCGCCGCGGCCAGGGCTGGCCCA
CAGGCCGCGCGGCGTCGGCTGGCTGAG
>contig00002 length=4242 numreads=43423
ATGCCGAAGGTCCGCCTGGGGCTGG
CGCCGGGAGCATGTAGCG
---
I would like to concatenate the lines not starting with ">" (concatenate any lines between lines starting with ">"). My wanted output is:
---
>contig00001 length=35524 numreads=2944
gACGCCGCGCGCCGCGGCCAGGGCTGGCCCACAGGCCGCGCGGCGTCGGCTGGCTGAG
>contig00002 length=4242 numreads=43423
ATGCCGAAGGTCCGCCTGGGGCTGGCGCCGGGAGCATGTAGCG
---

Thanks



---------- Post updated at 01:54 PM ---------- Previous update was at 01:48 PM ----------




I have tried like this:
% awk '{if(substr($0,1)==">") print $0"\n";else printf("%s",$0);}' test2.fna | fold -w60
But my output looks like:
Code:
>contig00001 length=35524   numreads=2944gACGCCGCGCGCCGCGGCC
AGGGCTGGCCCACGGCCcTCTTCCGGCGCGCTGCGCAGGCGTTCGGCCAGGCCGCGCGGC
GTCGGCTGGCTGAGCGCCCAGCGTAGCAGGCGATCGAACGGATGCCGACGGGCGCTTTCC
AGTCGTTCGCGCAAACGGGCGATCAACTGGGCGATCAACAGCGAGTCGCCGCCAGCCCCG
AAGAAGTCTTGCTCGACGCCCAGCGACGGGTTGTCCAGCACCTCCCGCCAGAGTGCCAGC

Instead of what I want which is like this:
Code:
>contig00001 length=35524   numreads=2944
gACGCCGCGCGCCGCGGCCAGGGCTGGCCCACGGCCcTCTTCCGGCGCGCTGCGCAGGCG
TTCGGCCAGGCCGCGCGGCGTCGGCTGGCTGAGCGCCCAGCGTAGCAGGCGATCGAACGG
ATGCCGACGGGCGCTTTCCAGTCGTTCGCGCAAACGGGCGATCAACTGGGCGATCAACAG
CGAGTCGCCGCCAGCCCCGAAGAAGTCTTGCTCGACGCCCAGCGACGGGTTGTCCAGCAC
CTCCCGCCAGAGTGCCAGC

# 2  
Old 10-22-2010
Try this:
Code:
awk '{printf (/>/)?RS"%s"RS:"%s",$0}END{print x}' infile

your code does not work because of this:
Code:
substr($0,1,1)

The Following User Says Thank You to Scrutinizer For This Useful Post:
s052866 (10-22-2010)
# 3  
Old 10-22-2010
Thanks. It worked. Yeah.
# 4  
Old 10-23-2010
Code:
awk '{printf /^>/?RS $0 RS:$0}' infile

# 5  
Old 10-23-2010
Quote:
Originally Posted by Scrutinizer
Try this:
Code:
awk '{printf (/>/)?RS"%s"RS:"%s",$0}END{print x}' infile

your code does not work because of this:
Code:
substr($0,1,1)

Hi Scruti ... ready for a nitpicking ?

Your code add an unexpected empty line if the input file start with a ">" line
# 6  
Old 10-23-2010
I know, but I figured it would complicate the code and it would not really matter. I did add the linefeed at the end, otherwise if the output gets written to a file, that last line becomes invalid, since the last line is not terminated with a linefeed..
Code:
awk '{printf />/?(NR>1?RS:x)"%s"RS:"%s",$0}END{print x}' infile

# 7  
Old 10-23-2010
Quote:
Originally Posted by Scrutinizer
I know, but I figured it would complicate the code and it would not really matter. I did add the linefeed at the end, otherwise if the output gets written to a file, that last line becomes invalid, since the last line is not terminated with a linefeed..
Code:
awk '{printf />/?((NR>1)?RS:x)"%s"RS:"%s",$0}END{print x}' infile

Dude, you're true, but i like to see your skill in action, that's why i challenged you
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Print all lines between two keyword if a specific pattern exist Amit Joshi Shell Programming and Scripting 8 08-14-2015 02:35 PM
Grep -v lines starting with pattern 1 and not matching pattern 2 demmel UNIX for Dummies Questions & Answers 8 07-18-2015 12:59 PM
Delete lines that contain a pattern from specific line to the end. jiam912 Shell Programming and Scripting 1 08-25-2014 05:34 AM
How to delete lines starting with specific string? huiyee1 Shell Programming and Scripting 2 07-30-2014 05:14 AM
Vi editor deleting lines with specific pattern Vishal_dba Shell Programming and Scripting 3 02-20-2014 01:15 PM
Want to get lines before specific pattern vipin auja Shell Programming and Scripting 3 01-22-2014 09:01 AM
Printing next two lines from a file after grepping a specific pattern Priya Amaresh Shell Programming and Scripting 8 07-18-2013 08:02 AM
How to concatenate lines with specific pattern? nixtime Shell Programming and Scripting 8 06-25-2013 06:39 PM
Awk, sed - concatenate lines starting with string locoroco Shell Programming and Scripting 3 01-22-2013 08:54 PM
Append lines for a specific pattern chitech Shell Programming and Scripting 7 02-23-2012 06:46 PM
Delete multiple lines starting with a specific pattern Zurd Shell Programming and Scripting 6 01-26-2012 05:14 AM
delete lines starting with a pattern yashwantkumar Shell Programming and Scripting 4 11-07-2011 02:19 AM
merging of 2 consecutive lines in a file for a specific pattern novice_man Shell Programming and Scripting 1 12-15-2010 01:47 AM
how to delete lines from a file which starts with a specific pattern mady135 Shell Programming and Scripting 3 12-02-2010 05:10 AM
shell script to remove all lines from a file before a line starting with pattern raksha.s Shell Programming and Scripting 2 03-29-2009 08:13 AM


All times are GMT -4. The time now is 04:07 PM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password