Urgent


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Urgent
# 1  
Old 06-30-2007
Urgent

Hello guys,
I have a file look this. I have just posted a segment of it. I want to replace "PW" from 4th column by TRP. How do I do it? Pls help.
thanks advance
parimal
Code:
ATOM   2817  HB3  GLU   180       0.545  12.520   1.097  0.00  0.00
ATOM   2818  CG    GLU   180       0.269  14.467   0.716  0.00  0.00
ATOM   2819  HG2  GLU   180       0.545  14.662   1.771  0.00  0.00
ATOM   2820  HG3  GLU   180       0.808  15.213   0.105  0.00  0.00
ATOM   2821  CD   GLU   180      -1.213  14.737   0.611  0.00  0.00
ATOM   2822  OE1  GLU   180      -1.956  13.994   1.296  0.00  0.00
ATOM   2823  OE2  GLU   180      -1.644  15.741  -0.021  0.00  0.00
ATOM   2824  C     GLU    180       3.202  13.530   0.831  0.00  0.00
ATOM   2825  O     GLU    180       3.330  12.915   1.879  0.00  0.00
ATOM   2826  N     PW     181       4.009  14.566   0.554  0.00  0.00
ATOM   2827  H     PW     181       3.797  15.140  -0.202  0.00  0.00
ATOM   2828  CA   PW     181       4.806  15.305   1.633  0.00  0.00
ATOM   2829  HA   PW     181       5.328  14.569   2.257  0.00  0.00
ATOM   2830  CB   PW     181       5.770  16.204   0.865  0.00  0.00
ATOM   2831  HB2  PW     181       5.349  16.715   0.045  0.00  0.00
ATOM   2832  HB3  PW     181       6.030  16.995   1.540  0.00  0.00
ATOM   2833  CG   PW     181       7.119  15.606   0.500  0.00  0.00
ATOM   2834  CD1  PW     181       7.782  15.916  -0.606  0.00  0.00
ATOM   2835  HD1  PW     181       7.551  16.633  -1.455  0.00  0.00
ATOM   2836  NE1  PW     181       8.961  15.227  -0.543  0.00  0.00
ATOM   2837  HE1  PW     181       9.658  15.363  -1.269  0.00  0.00
ATOM   2838  CE2  PW     181       9.239  14.568   0.604  0.00  0.00
ATOM   2839  CZ2  PW     181      10.349  13.797   1.081  0.00  0.00
ATOM   2840  HZ2  PW     181      11.189  13.773   0.464  0.00  0.00
ATOM   2841  CH2  PW     181      10.208  13.248   2.330  0.00  0.00
ATOM   2842  HH2  PW     181      10.923  12.548   2.740  0.00  0.00
ATOM   2843  CZ3  PW     181       9.183  13.660   3.113  0.00  0.00
ATOM   2844  HZ3  PW     181       9.072  13.345   4.152  0.00  0.00
ATOM   2845  CE3  PW     181       8.112  14.454   2.673  0.00  0.00
ATOM   2846  HE3  PW     181       7.357  14.911   3.365  0.00  0.00
ATOM   2847  CD2  PW     181       8.042  14.834   1.330  0.00  0.00
ATOM   2848  C     PW     181       3.823  16.140   2.431  0.00  0.00
ATOM   2849  O     PW     181       3.156  17.024   1.894  0.00  0.00
ATOM   2850  N     SER    182       3.799  15.999   3.750  0.00  0.00
ATOM   2851  H     SER    182       4.234  15.206   4.136  0.00  0.00
ATOM   2852  CA    SER   182       3.092  16.883   4.626  0.00  0.00


Last edited by reborg; 06-30-2007 at 12:49 PM..
# 2  
Old 06-30-2007
Code:
awk '{sub(/^PW$/, "TRP", $4); print $0 } inputfile

# 3  
Old 06-30-2007
thaks aigles! it works fine. only problem is the columns are not properly alined.
# 4  
Old 06-30-2007
Quote:
Originally Posted by chuchu
thaks aigles! it works fine. only problem is the columns are not properly alined.
like your sample datas Smilie
When posting sample datas or code, you must use the [code] tag to preserve format.

What is the record format of your file ?
# 5  
Old 07-01-2007
Code:
awk '$4 ~ /^PW$/{ $4="TRP"}{print}' "file"

# 6  
Old 07-01-2007
The follwing script replace a value (From) by another value (To) in a specific field (Field).
The input columns alignments are preserved in the output (except particular case where new value is larger than the field)

The script assume that the input field separator is space.

Code:
awk -v Field=4 -v From="PW" -v To="TRP" '

   BEGIN {
      FS = " ";

      fromLen = length(From);
        toLen = length(To);

      fieldPattern = "[^ ]+ +"
      startPattern = "^";
      for (f=1; f<Field; f++)
         startPattern = startPattern fieldPattern;
      endPattern = startPattern fieldPattern;
   }

   $Field == From {
      line = $0;

      match(line, startPattern);
      startPos = RSTART + RLENGTH;

      if (Field == NF) {
         fieldLen = length(line) - startPos + 1;
         fmt  = "%-" startPos-1 "." startPos-1 "s%-" fieldLen "s";
         line = sprintf(fmt, line, To);
      }
      else {
         match(line, endPattern);
         endPos = RSTART + RLENGTH;
         fieldLen = endPos - startPos;
         if (fieldLen > toLen) {
            fmt = "%-" startPos-1 "." startPos-1 "s%-" fieldLen "." fieldLen "s%s";
            line = sprintf(fmt, line, To, substr(line, startPos+fieldLen));
         }
         else {
            fmt = "%-" startPos-1 "." startPos-1 "s%-" toLen+1 "." toLen "s%s";
            line = sprintf(fmt, line, To, substr(line, startPos+fieldLen));
         }
      }

      print line;
      next;
   }


   {
      print $0;
   }

' atom.txt

For your input sample datas, the output is:
Code:
ATOM   2817  HB3  GLU   180       0.545  12.520   1.097  0.00  0.00
ATOM   2818  CG    GLU   180       0.269  14.467   0.716  0.00  0.00
ATOM   2819  HG2  GLU   180       0.545  14.662   1.771  0.00  0.00
ATOM   2820  HG3  GLU   180       0.808  15.213   0.105  0.00  0.00
ATOM   2821  CD   GLU   180      -1.213  14.737   0.611  0.00  0.00
ATOM   2822  OE1  GLU   180      -1.956  13.994   1.296  0.00  0.00
ATOM   2823  OE2  GLU   180      -1.644  15.741  -0.021  0.00  0.00
ATOM   2824  C     GLU    180       3.202  13.530   0.831  0.00  0.00
ATOM   2825  O     GLU    180       3.330  12.915   1.879  0.00  0.00
ATOM   2826  N     TRP    181       4.009  14.566   0.554  0.00  0.00
ATOM   2827  H     TRP    181       3.797  15.140  -0.202  0.00  0.00
ATOM   2828  CA   TRP    181       4.806  15.305   1.633  0.00  0.00
ATOM   2829  HA   TRP    181       5.328  14.569   2.257  0.00  0.00
ATOM   2830  CB   TRP    181       5.770  16.204   0.865  0.00  0.00
ATOM   2831  HB2  TRP    181       5.349  16.715   0.045  0.00  0.00
ATOM   2832  HB3  TRP    181       6.030  16.995   1.540  0.00  0.00
ATOM   2833  CG   TRP    181       7.119  15.606   0.500  0.00  0.00
ATOM   2834  CD1  TRP    181       7.782  15.916  -0.606  0.00  0.00
ATOM   2835  HD1  TRP    181       7.551  16.633  -1.455  0.00  0.00
ATOM   2836  NE1  TRP    181       8.961  15.227  -0.543  0.00  0.00
ATOM   2837  HE1  TRP    181       9.658  15.363  -1.269  0.00  0.00
ATOM   2838  CE2  TRP    181       9.239  14.568   0.604  0.00  0.00
ATOM   2839  CZ2  TRP    181      10.349  13.797   1.081  0.00  0.00
ATOM   2840  HZ2  TRP    181      11.189  13.773   0.464  0.00  0.00
ATOM   2841  CH2  TRP    181      10.208  13.248   2.330  0.00  0.00
ATOM   2842  HH2  TRP    181      10.923  12.548   2.740  0.00  0.00
ATOM   2843  CZ3  TRP    181       9.183  13.660   3.113  0.00  0.00
ATOM   2844  HZ3  TRP    181       9.072  13.345   4.152  0.00  0.00
ATOM   2845  CE3  TRP    181       8.112  14.454   2.673  0.00  0.00
ATOM   2846  HE3  TRP    181       7.357  14.911   3.365  0.00  0.00
ATOM   2847  CD2  TRP    181       8.042  14.834   1.330  0.00  0.00
ATOM   2848  C     TRP    181       3.823  16.140   2.431  0.00  0.00
ATOM   2849  O     TRP    181       3.156  17.024   1.894  0.00  0.00
ATOM   2850  N     SER    182       3.799  15.999   3.750  0.00  0.00
ATOM   2851  H     SER    182       4.234  15.206   4.136  0.00  0.00
ATOM   2852  CA    SER   182       3.092  16.883   4.626  0.00  0.00

# 7  
Old 07-01-2007
thaks!! it works!!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

URGENT Reading a file and assessing the syntax shell script URGENT

I am trying to write a shell script which takes an input file as an arguement in the terminal e.g. bash shellscriptname.sh input.txt. I would like for the file to be read line by line each time checking if the .txt file contains certain words or letters(validating the syntax). If the line being... (1 Reply)
Discussion started by: Gurdza32
1 Replies

2. Shell Programming and Scripting

Urgent !!

Hello All, I want to use scp for copying multiple files ( files locations are stored in an array ) from remote server from different locations without prompting password every time . I will supply password once and it should be able to copy every file mentioned in an array. eg :- array have ... (1 Reply)
Discussion started by: manpav
1 Replies

3. Shell Programming and Scripting

Need urgent help

Hi geeks, I'm trying to write a below script, but it throws an error, please check and correct me. #!/bin/bash #The below script will extract the string error1 error2 and error3 and also it will count and list the occurrence count1='grep -i "error1" test.txt | wc -l' count2='grep -i "error2"... (4 Replies)
Discussion started by: naren nandale
4 Replies

4. Shell Programming and Scripting

Need Urgent help

Hi friends, I need urgent help here: Issue: I need to create shell script that will find the files & throw an error through job (autosys) when file not found. Daily we use to receive 3 files from a system. Obstacles: 1) All 3 files names are same. 2) Timestamp is same. 3)... (1 Reply)
Discussion started by: tush
1 Replies

5. Shell Programming and Scripting

Urgent!!

Hi I have a file containing DE 3'UTR in Homo sapiens alpha-1-B glycoprotein (A1BG), mRNA. SQ Sequence 216 BP; 37 A; 58 C; 69 G; 52 T; 0 other; DE 3'UTR in Homo sapiens alpha-1-B glycoprotein (A1BG), mRNA. SQ Sequence 1844 BP; 358 A; 483 C; 434 G; 569 T; 0 other; DE 3'UTR in Homo... (1 Reply)
Discussion started by: jyotirmoy21
1 Replies

6. Red Hat

urgent

abb 117.96.113.21 cgg 101.2.104.42 cgg 110.227.247.236 desk 203.20.35.28 png 1.39.242.241 png 1.39.242.241 rzx 101.2.104.42 rzx 115.246.160.36 abb 49.138.242.187 how to find the count of this file wtrto ip thnx in advance (4 Replies)
Discussion started by: himanshu1.singh
4 Replies

7. UNIX for Advanced & Expert Users

need help urgent...........

Hi friends.. I am using the below command to search few files from many folders which is under one folder.. i mean let say the path is A/B/C...and inside C...i have 1-10 folder... the below command is working fine.... for i in 1 3 5 7; do find /A/B/C/${i} -name "*.txt" -o -name "*.csv"... (3 Replies)
Discussion started by: sapan123
3 Replies

8. UNIX for Advanced & Expert Users

URGENT,URGENT- Need help tape drive installation

Hi, I am trying to attach tape drive to sun V890 running Solaris 9 on it. I have installed HBA(qlogic) in slot 1 of 0-8 slots and booted the system. I do not see HBAin prtdiag output. The tape drive is not attached to HBA. The tape drive I am going to attach is Sony AIT3. 1.How can I make... (3 Replies)
Discussion started by: sriny
3 Replies

9. Solaris

Need Help Urgent

Hi. My E250 server (Solaris 8) running oracle database gets hangup in between. I checked the logs in /var/adm/messages but could not find anything related to this. can anyone help me out? bala (3 Replies)
Discussion started by: balaji_prk
3 Replies
Login or Register to Ask a Question