LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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: Tuscaloosa, AL
Posts: 126

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,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
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: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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: Tuscaloosa, AL
Posts: 126

Original Poster
Rep: Reputation: 15
Thanks!
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:41 PM.

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
Open Source Consulting | Domain Registration