Computing.Net > Forums > Unix > Swap columns and replace

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Swap columns and replace

Reply to Message Icon

Name: RasH
Date: October 15, 2007 at 00:03:33 Pacific
OS: Linux
CPU/Ram: 1800/512
Comment:

Hi. I have some output that i would like filtered. Columns 2 and 3 should be interchanged and the contents of column 4 (and ONLY column 4) should be replaced:
1A6Y;173;B;F; -75.1; 167.5
1A6Y;174;B;G; 58.7; 155.7
1A6Y;175;B;R; -18.1; 142.6
1A6Y;176;B;I; -72.6; 136.1
1A6Y;177;B;P; -68.1;-136.2
1A6Y;178;B;K;-162.6; 360.0
should be
1A6Y;B;173;PHE; -75.1; 167.5
1A6Y;B;174;GLY; 58.7; 155.7
1A6Y;B;175;ARG; -18.1; 142.6
1A6Y;B;176;ILE; -72.6; 136.1
1A6Y;B;177;PRO; -68.1;-136.2
1A6Y;B;178;LYS;-162.6; 360.0



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: October 15, 2007 at 09:14:30 Pacific
Reply:

You don't say what column 4 should be replaced with. Anyway, I'll leave that to you, but here is the swap of the two columns:

#!/bin/bash

awk ' BEGIN { FS=";"; OFS=";" } { ss=$2
$2=$3
$3=ss
print $0 } ' myfile.txt



0

Response Number 2
Name: RasH
Date: October 15, 2007 at 09:43:39 Pacific
Reply:

Thanks for the reply. Ill get right to it. Actually i thought my example could explain what i want to replace:
F => PHE
G => GLY
R => ARG
etc..


0

Response Number 3
Name: japh
Date: October 24, 2007 at 01:19:14 Pacific
Reply:

add the following to the BEGIN block:
repl["F"] = "PHE";
repl["G"] = "GLY";
repl["R"] = "ARG";
repl["I"] = "ILE";
repl["P"] = "PRO";
repl["K"] = "LYS";

and add the following just before the print $0:

$4 = repl[$4];

HTH


This message was produced with 100% recycled electrons


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Swap columns and replace

Help search and replace www.computing.net/answers/unix/help-search-and-replace/6886.html

One more Q. (find and replace) www.computing.net/answers/unix/one-more-q-find-and-replace/5826.html

Unix - Search and replace www.computing.net/answers/unix/unix-search-and-replace/6775.html