LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 03-12-2013, 01:06 AM   #1
wackysiya
LQ Newbie
 
Registered: Mar 2013
Posts: 12

Rep: Reputation: Disabled
Stuck !!!!


I have a sequence :

Code:
PH01000000G0240 P.he_genemodel_v1.0 CDS 120721 121773 . - . ID=PH01000000G0240.CDS;Parent=PH01000000G0240
PH01000001G0190 P.he_genemodel_v1.0 mRA 136867 137309 . - . ID=PH01000001G0190.mRNA;Parent=PH01000001G0190
.............................................
PH01278028G0010 P.he_genemodel_v1.0 CDS 27 501.. . - . ID=PH01278028G0010;Description="oereed"
PH01278104G0010 P.he_genemodel_v1.0 CDS 34 171 . - . ID=PH01278104G0010.CDS;Parent=PH01278104G0010

I want to replace PH0100000 by string but only in the first tab like
PH01000000 to string0
PH01000001 to string1
....
PH01278104 to string278104
PH01278028 to string278028


I want it to look like
Code:
string0G0240 P.he_genemodel_v1.0 CDS 120721 121773 . - . ID=PH01000000G0240.CDS;Parent=PH01000000G0240
string1G0190 P.he_genemodel_v1.0 mRA 136867 137309 . - . ID=PH01000001G0190.mRNA;Parent=PH01000001G0190
.............................................

string278028G0010 P.he_genemodel_v1.0 CDS 27 501.. . - . ID=PH01278028G0010;Description="oereed"
string278104G0010 P.he_genemodel_v1.0 CDS 34 171 . - . ID=PH01278104G0010.CDS;Parent=PH01278104G0010

I used sed command but it didnt work.
sed 's/^PH01\t/string\t/' infile > sortedfile
and then
sed 's/string0*/string/g' infile>sorted file

so tht it removes the extra 00 formed

so that any replacement of this form

PH01000000 to string00000

gets converted to this form
PH01000000 to string0


But none of my commands worked

Could some1 please help

Last edited by colucix; 03-12-2013 at 07:54 AM. Reason: Added CODE tags.
 
Old 03-12-2013, 02:10 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
You can try awk, as:
Code:
~$ gawk -F" " '{gsub(/PH01278104/,"<string>",$1); print $0}' infile.txt
Code:
~$ nawk -F" " '{gsub(/PH01278104/,"<string>",$1); print $0}' infile.txt
 
Old 03-12-2013, 10:38 AM   #3
wackysiya
LQ Newbie
 
Registered: Mar 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
I used $ gawk -F" " '{gsub(/PH01/,"string",$1); print $0}' infile.txt

It replaced all my entries by string
PH01000000G124 to string00000G124
PH01000000G144 to string00000G144
...
PH01000001G670 to string00001G670
PH01000002G450 to string00001G450



PH01278028G433 to string278028G433
THE LAST ENTRY AS IT IS ACCORDING TO MY REQUIREMENT OF STRING FOLLOWED BY NUMBER


BUT THE OTHER ENTRIES HAVE ADDITIONAL ZEROES.
PH01000000G124 to string00000G124
I WANT IT LIKE string0G124 ,string1G453


Is there anythng i could use that i could get ride of that extra zeroes in the first tab.

I used sed 's/string0*/string/g' infile>sorted file but it didnt work.

Thanks a lot!!!
 
Old 03-12-2013, 11:14 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Try just sub as gsub has the 'g' for global on the line.
 
Old 03-12-2013, 11:15 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Let's know exactly how many such entries are there in your infile.txt ?
For 2 or 3 entries, it's ok to use gsub function in awk, else we need to use some regexp to replace multiple entries.
 
Old 03-12-2013, 12:10 PM   #6
wackysiya
LQ Newbie
 
Registered: Mar 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
Yes , i have lot of entries like 10000 .

using gsub i got the first part done.

I want to now replace

string00000G670 to string0G670
string00001G270 to string1G270
string00002G634 to string2G634

THIS MODIFICATION HAS TO BEEN ONLY IN THE FIRST TAB.

I used sed but it didnt work
Could you suggest?
 
Old 03-12-2013, 12:23 PM   #7
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
You can use the same here as:
Code:
~$ gawk -F" " '{sub(/string0000/,"string",$1); print $0}' infile.txt
OR, using sed:
Code:
~$ sed -e 's/string0000/string/' infile.txt
 
Old 03-12-2013, 03:44 PM   #8
wackysiya
LQ Newbie
 
Registered: Mar 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
It worked 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
no support for locale: en_Us.itf8 Stuck found some post but still Stuck!! Suec7832 Linux - Newbie 1 08-30-2011 08:13 AM
Drive stuck, how to I un-stuck it. MikeyCarter Linux - Software 3 08-05-2009 11:57 AM
Stuck at 92% lcp Linux - Laptop and Netbook 3 05-30-2006 11:30 PM
I am stuck... SGFHK321 Linux - Newbie 11 07-12-2004 11:09 PM
Help I'm Stuck chronico Linux - Networking 0 03-26-2003 05:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:48 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
Open Source Consulting | Domain Registration