LinuxQuestions.org
Help answer threads with 0 replies.
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 01-20-2013, 11:58 PM   #1
sonia102d
LQ Newbie
 
Registered: Sep 2012
Posts: 18

Rep: Reputation: Disabled
Question Selective replace help


I want help on this :
This is brief desciption of my original file.It lookslike this:

sat_1_g3_g_0_8540 . FFF 1 15501 . . . ID=sat_1_g3_g_0_8540;parentName=sat_1_g3_g_0_8540;Al=sat_1_g2_g_0_8540;
sat_1_g3_g_2_8510 . FFF 1 25501 . . . ID=sat_1_g3_g_2_8510;parentName=sat_1_g3_g_2_8510;Al=sat_1_g1_g_2_8510;
sat_1_g3_g_4_8240 . FFF 1 13501 . . . ID=sat_1_g3_g_4_8240;parentName=sat_1_g3_g_4_8240;Al=sat_1_g2_g_4_8240;

Final result that my file should look like :
string_8540 . seq 1 15501 . . . ID=sat_1_g3_g_0_8540;parentName=sat_1_g3_g_0_8540;Al=sat_1_g2_g_0_8540;
string_8510 . seq 1 25501 . . . ID=sat_1_g3_g_2_8510;parentName=sat_1_g3_g_2_8510;Al=sat_1_g1_g_2_8510;
string_8240 . seq 1 13501 . . . ID=sat_1_g3_g_4_8240;parentName=sat_1_g3_g_4_8240;Al=sat_1_g2_g_4_8240;


I want to replace the first tab by string( replace sat_1_g3_g_0_8540 by string_8540) as seen and also replace all FFF entries of 3rd tab by seq

I dont want to lose tab.I tried something which replaced my tabs by spaces .i have a code to run on this so i ned to retain my tabs.

Is there any command which will change my first tab contents without altering or removing tabs.
i would also want a coomand for the 3rd tab replacement
Thanks
 
Old 01-21-2013, 12:01 AM   #2
sonia102d
LQ Newbie
 
Registered: Sep 2012
Posts: 18

Original Poster
Rep: Reputation: Disabled
TO add on to this
I only want to replace sat_1_g3_g_0_8540 ,sat_1_g3_g_2_8510 ... and so on entries by string_8540,string_8510 only in the first tab.
Other tabs also have this entry but i want to retain those values there.

please help
 
Old 01-21-2013, 01:18 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
I am not sure I follow the "tab" references? in one part of your information it seems to actually refer to the column of data and in another it is (I think) an actual tab character.

Would you maybe try to rephrase the questions to make a little more sense?
 
Old 01-21-2013, 08:30 AM   #4
sonia102d
LQ Newbie
 
Registered: Sep 2012
Posts: 18

Original Poster
Rep: Reputation: Disabled
used method

This is brief desciption of what i did
original file
sat_1_g3_g_0_8540 . FFF 1 15501 . . . ID=sat_1_g3_g_0_8540;parentName=sat_1_g3_g_0_8540;Al=sat_1_g2_g_0_8540;
sat_1_g3_g_2_8510 . FFF 1 25501 . . . ID=sat_1_g3_g_2_8510;parentName=sat_1_g3_g_2_8510;Al=sat_1_g1_g_2_8510;
sat_1_g4_g_4_1919 . FFF 1 390481 . . .ID=sat_1_g4_g_4_1919;parentName=sat_1_g4_g_4_1919;Al=sat_1_g3_g_4_1919;


I used sed 's/^sat_1_g3_g_0/string/' infile >sortedfile and sed 's/^sat_1_g3_g_2/string/' sortedfile >sortedfile1 to get this partof result

string_8540 . FFF 1 15501 . . . ID=sat_1_g3_g_0_8540;parentName=sat_1_g3_g_0_8540;Al=sat_1_g2_g_0_8540;
string_8510 . FFF 1 25501 . . . ID=sat_1_g3_g_2_8510;parentName=sat_1_g3_g_2_8510;Al=sat_1_g1_g_2_8510;
string_1919 . FFF 1 390481 . . . ID=sat_1_g4_g_4_1919;parentName=sat_1_g4_g_4_1919;Al=sat_1_g3_g_4_1919;

I think it took off the tab thing and replaced it by spaces.
I did a mistake somewhere dont know where. Tabs are important as far as my following codes are concered since they wont work on spaces.
Then replaced FFF by seq using awk '$3=="FFF"{$3="seq"}1' infile

I dont know which of these replaced my tabs with spaces.Could some 1 help ?

Thanks a lot

Final result that i want
string_8540 . seq 1 15501 . . . ID=sat_1_g3_g_0_8540;parentName=sat_1_g3_g_0_8540;Al=sat_1_g2_g_0_8540;
string_8510 . seq 1 25501 . . . ID=sat_1_g3_g_2_8510;parentName=sat_1_g3_g_2_8510;Al=sat_1_g1_g_2_8510;


I want to retain all tabs .I dont know where did i go wrong
 
Old 01-21-2013, 08:47 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
You can retain tab character using a \t as output field separator.

Code:
~$ awk 'BEGIN{OFS="\t"}; {gsub(/sat_1_g3_g_0/,"string",$0); gsub(/sat_1_g3_g_2/,"string",$0); gsub(/FFF/,"seq",$0); print $0}' infile.txt
But in your sample file, I cannot see tab spaces between words. So can you use a readable formatting while posting your sample data? Use [CODE] on your sample data/commands.

Last edited by shivaa; 01-21-2013 at 08:55 AM. Reason: Substitute added in awk code
 
Old 01-26-2013, 08:09 PM   #6
sonia102d
LQ Newbie
 
Registered: Sep 2012
Posts: 18

Original Poster
Rep: Reputation: Disabled
Unhappy

Hi,

I used the above mentioned command:

awk 'BEGIN{OFS="\t"}; {gsub(/sat_1_g3_g_0/,"string",$0); gsub(/sat_1_g3_g_2/,"string",$0); gsub(/FFF/,"seq",$0); print $0}' infile.txt

However, it replaced all occurrences of sat_1_g3_g_* in the file, although it should have replaced only those in the first column. Is it that that it will work if I use $1 instead of $0 (except for the "FFF" replacement, which should run on $3).

awk 'BEGIN{OFS="\t"}; {gsub(/sat_1_g3_g_0/,"string",$1); gsub(/sat_1_g3_g_2/,"string",$1); gsub(/FFF/,"seq",$3); print $0}' infile.txt

Am I wrong?

Please advise.
 
Old 01-26-2013, 11:15 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
What result did you get after replacing $0 with $1? I think same as you got with $0, because there's no default FS in the input file.

So if you want a replacement in first occurence only, then declare field seperator first, and then try as:
Code:
~$ awk 'BEGIN{FS="="; OFS="\t"}; {gsub(/sat_1_g3_g_0/,"string",$1); gsub(/sat_1_g3_g_2/,"string",$1); gsub(/FFF/,"seq",$0); print $1}' infile
 
Old 01-26-2013, 11:27 PM   #8
sonia102d
LQ Newbie
 
Registered: Sep 2012
Posts: 18

Original Poster
Rep: Reputation: Disabled
Hi
I dont want to replace only in the first occurance.
I want replace all the occurances of sat_1_g3_g* in the first column or tab and not any other place in file.
Also I wnt to replace FFF only in the 3rd column and no were else.

When i tried the earlier code,changes did happen ,but i coudlnt check the entire file as its a very huge file.
Subsequent codes didnt work on this file so i thought there must be some error.

I didnt know what this $0 $1 indicated so thought $1 would make changes only in first tab and so on.

please do tell me what this $0 $1 $2 and print $0 indicate?

thnks
 
Old 01-27-2013, 12:28 AM   #9
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
Quote:
Originally Posted by sonia102d View Post
Hi
I dont want to replace only in the first occurance.
I want replace all the occurances of sat_1_g3_g* in the first column or tab and not any other place in file.
Also I wnt to replace FFF only in the 3rd column and no were else.

When i tried the earlier code,changes did happen ,but i coudlnt check the entire file as its a very huge file.
Subsequent codes didnt work on this file so i thought there must be some error.
Can you once look at your sample file that you showed above? Columns are not specified properly!

Before suggesting anything more, once share a neat & clear sample file with proper spaces/tab. And use [code][code] tags on your sample data while replying (see the tool bar on top of your message box, use # symbol for [code] tags).
 
1 members found this post helpful.
  


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
How to allow selective access brokenpromises Linux - Security 3 07-31-2007 07:23 AM
X selective translucency? shishimo Linux - Software 1 06-22-2005 08:31 PM
Selective startup LemonSHARK Red Hat 1 03-02-2004 06:25 PM
How selective is your memory? xailer Linux - Newbie 12 12-04-2003 12:36 AM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 02:04 AM


All times are GMT -5. The time now is 06:47 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration