LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-27-2010, 04:13 AM   #1
ngyz86
LQ Newbie
 
Registered: Dec 2010
Posts: 2

Rep: Reputation: 0
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
 
Old 12-27-2010, 04:44 AM   #2
Trumpen
LQ Newbie
 
Registered: Dec 2008
Location: Italy
Distribution: Debian & Ubuntu
Posts: 11

Rep: Reputation: 0
Awk should just be enough:

Code:
awk '$5>999{$5=" "$5} 1' wt-test.pdb >wt-test-out.pdb
where I used a blank character to shift the fifth field one character to the right.

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.
 
Old 12-27-2010, 07:10 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Or you could put it altogether with the uniqueness too:
Code:
awk '!_[$5]++{if($5 > 999)$5=" "$5;print $5}' wt-test.pdb
 
Old 12-27-2010, 08:14 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by ngyz86 View Post
#!/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
Do you just want the "$i" to be substituted for the variable's value in the argument for sed? If so, use this:
Code:
#!/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
Note the parts in bold.
 
Old 01-05-2011, 04:03 AM   #5
ngyz86
LQ Newbie
 
Registered: Dec 2010
Posts: 2

Original Poster
Rep: Reputation: 0
perl script to alter character positions in pdb file

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.
Attached Files
File Type: txt shift-resno.txt (824 Bytes, 27 views)
File Type: txt shift-resno-2.txt (830 Bytes, 16 views)
 
Old 01-05-2011, 04:17 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
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.
Maybe you could demonstrate what you mean as the limitation would mainly be on what is written so far ... not on awk.
 
Old 01-05-2011, 07:44 AM   #7
larryhaja
Member
 
Registered: Jul 2008
Distribution: Slackware 13.1
Posts: 305

Rep: Reputation: 80
Quote:
Originally Posted by ngyz86 View Post
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'm not sure if this is the issue to your problem but it looks like the file keeps getting overwritten because of '>' character. Try replacing it with '>>'. So I would change this.
Code:
#!/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
to
Code:
#!/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
 
  


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
SH: Recursive Variable Substitution TVT Programming 7 07-01-2010 03:30 PM
[SOLVED] bash variable substitution Jerry Mcguire Programming 6 04-29-2010 09:33 AM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM
variable substitution in sed gaynut Programming 1 07-14-2008 07:38 AM
Bash variable substitution daYz Programming 3 04-14-2006 01:16 PM

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

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