LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-22-2010, 06:44 PM   #1
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 98

Rep: Reputation: 15
Trying to number every other line and append those numbers to end of line


Hi all,

I have a file formatted like this:
Code:
>Contig
ATGCATGCAATGCATGCAATGCACGGATGCAATGCATGCA
>Contig
CAATGCACGGATGCATGCCAATGATGCAATGCAT
>Contig
ATGCCGCAGTCTGACTTGCAACTGCCGTCACAGCTGCA
>Contig
CAATAACGCACGGATGCATGCTACCAATGTATGCAATGCAT
Every odd-numbered line is ">Contig" while the even numbered lines vary but are a DNA sequence of variable length and composition.

I am trying to give each Contig a number such that my output would be:
Code:
>Contig1
ATGCATGCAATGCATGCAATGCACGGATGCAATGCATGCA
>Contig2
CAATGCACGGATGCATGCCAATGATGCAATGCAT
>Contig3
ATGCCGCAGTCTGACTTGCAACTGCCGTCACAGCTGCA
>Contig4
ATGCCGCAGTCTGACTTGCAACTGCCGTCACAGCTGCA
I know this has to be really simple but I can't figure it out. Any suggestions would be greatly appreciated.

Thanks!
Kevin
 
Old 04-22-2010, 11:18 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Code:
#!/usr/bin/perl

use strict;
use warnings;

my @data;
my $match = 1;

open( FH, "<test.txt") or die "Cannot open source";
@data=<FH>;
close(FH);

open( FH, ">test.txt") or die "Cannot open dest";
foreach ( @data ) {
	if ( s/(>Contig)/$1$match/ ) { $match++; }
	print FH;
}

close(FH);
 
Old 04-22-2010, 11:35 PM   #3
dina3e
Member
 
Registered: Mar 2008
Location: Bangalore
Distribution: Enterprise Red Hat linux
Posts: 98

Rep: Reputation: 16
try this..
Code:
#!/bin/sh
counter=0
for line in `cat file_name.txt`
do
if [ $line = '>Contig' ];then
let counter=$counter+1
echo $line$counter
else
echo $line
fi
done
 
Old 04-22-2010, 11:55 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
I did it this way using awk.
Code:
awk -F "i=1" '{if (/>Contig/){print $0i++} else print}' file
 
Old 04-23-2010, 12:33 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,513

Rep: Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895
Same idea:
Code:
awk '/>/{print $0(++i)}!/>/' file
 
Old 04-23-2010, 04:02 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Quote:
Originally Posted by grail View Post
Same idea:
Code:
awk '/>/{print $0(++i)}!/>/' file
Thank you. I knew it could be shorter, but I couldn't quite work out the syntax. (Parentheses around the pre-increment variable...I should've guessed.)
 
Old 04-23-2010, 07:20 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,513

Rep: Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895
Quote:
Parentheses around the pre-increment variable
Took me a couple of tries
 
Old 04-23-2010, 11:17 AM   #8
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 98

Original Poster
Rep: Reputation: 15
Thanks!
 
  


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
bash programming-append single line to end of file pheasand Linux - General 4 02-28-2014 09:41 AM
Sed append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 4 10-23-2013 01:48 PM
Attempting to append a line of text to the end of the previous line market_garden Linux - General 4 12-11-2008 11:37 AM
Append variable string(s) at end of each line schaganti Linux - Newbie 2 10-19-2007 01:31 PM
bash: append string to end of line khairil Programming 6 02-27-2007 05:09 AM


All times are GMT -5. The time now is 04:55 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration