sed substitution of variable with a variable
Dear all,
I am trying to alter the character position of residue numbers above 999 in a pdb file. The following script is an attempt to: 1) Get all unique pdb residue numbers (in column 5) using awk and assign it to a variable i. 2) Loop through all the values in $i and if it is greater than 999, shift that number one character to the right using sed. However, the script only manages to alter the final residue number. Could anyone please advise how I can loop through all values in $i and shift it one character to the right? Many thanks for your help. #!/bin/bash # Script to alter position of residue number in pdb file for resid above 999 i=$(awk '{print $5}' wt-test.pdb | uniq) for i in $i; do if [ "$i" -gt "999" ]; then sed 's/ $i / $i/g' wt-test.pdb > wt-test-out.pdb ; fi done |
Awk should just be enough:
Code:
awk '$5>999{$5=" "$5} 1' wt-test.pdb >wt-test-out.pdb By the way, your sed command doesn't work mainly because you are using single quotes which don't allow $i to be expanded to the value of shell variable $i. Try with double quotes. |
Or you could put it altogether with the uniqueness too:
Code:
awk '!_[$5]++{if($5 > 999)$5=" "$5;print $5}' wt-test.pdb |
Quote:
Code:
#!/bin/bash |
perl script to alter character positions in pdb file
2 Attachment(s)
Dear all,
Many thanks for all your replies. I tried awk but after substitution, the specific character spacing between columns is no longer preserved. You can specify your output to be comma or tab or space separated etc but you cannot retain specific character spacing between different columns. I believe that is a drawback of awk. As for sed, although it can retain column spacing after substitution, but if I place it in a loop over all the variables I want replaced, it performs substitution multiple times on the same original file, resulting in only the last value of the variable being substituted in the output. One way around is to keep creating temporary files for each substitution, with the next substitution working on the previous output temporary file. I have a perl script that can help users adjust pdb column spacings. The script works by splitting each column in the original file on spaces, then reassigning columns to have specific character widths, and values in the column to be left or right aligned. Please take a look at the standard pdb format (from PDB website) before modifying the script. The script must be modified to work on different pdb files, especially if chain numbers are present or absent etc. The script can also renumber atom numbers. I hope it will be useful as a starting point for people who need to modify pdb files or others where column adjustments and alignments are needed. |
Quote:
|
Quote:
Code:
#!/bin/bash Code:
#!/bin/bash |
All times are GMT -5. The time now is 08:12 PM. |