Sponsored Content
Top Forums Shell Programming and Scripting Saving output from awk into a perl variable Post 302245242 by userix on Thursday 9th of October 2008 04:01:23 PM
Old 10-09-2008
Code:
for (@test)
{
   $last = `awk '/OXT/{print \$4}' $_`;
   $lastnum = `awk '/OXT/{print \$5}' $_`;
   system("sed -i -e '/O    ${last}    ${lastnum}/{s/-0.51000\$/-0.25500/;}' $_");
   system("sed -i -e '/OXT  ${last}    ${lastnum}/{s/-0.67000\$/-0.25500/;}' $_");
}

Thanks guys for your help. The variables are saving the awk output correctly, but when I try to use it in the following sed commands to replace the values shown, it fails.

This is what I want in the end: Find the line that contains OXT and grab the 4th and 5th values and save them to the variables indicated. Then I want to locate the lines that contain OXT followed by the 4th column value (GLY in this case) and by the 5th column value (319 in this case) and replace the number in the last column with the value shown in my sed line. Then I need to make the same replacement with the line that contains O followed by the 4th column value (GLY) and then by the 5th column value (319).

Below is a segment of a single file that I need to make the 2 replacement in. The bolded values are the numbers I need to replace with the values shown in the sed line. The thing is, the line that contains the O might not be next to the OXT line. It could be a random numbers of lines above. Each file in my set is the amino acid sequence and connectivity of different proteins. So the format of each file is uniform, but the actual data is different. Each file will have a single line that contains OXT, which is always located in the last amino acid residue. So I get the residue abberviation and number of the last amino acid by storing the 4th and 5th column value. Then I need to locate the O (oxygen) that is also located in the last residue (GLY 319) in the example file below and make the replacement of the last column value for this line and the line that contains OXT.

Pardon my poor programming mannerisms and improper perl usage, but I can better understand what is going on when I use awk and sed in my Perl script.


Code:
   ATOM    4539 HCB   LEU    318   -7.05103  -5.58173  17.60062 H_     1 0  0.09000
   ATOM    4540 HCB   LEU    318   -7.27904  -5.68284  15.87021 H_     1 0  0.09000
   ATOM    4541  CG   LEU    318   -7.65157  -7.53041  16.94206 C_3    4 0 -0.09000
   ATOM    4542 HCG   LEU    318   -6.58580  -7.79332  16.96207 H_     1 0  0.09000
   ATOM    4543  CD1  LEU    318   -8.26957  -8.08733  18.25040 C_3    4 0 -0.27000
   ATOM    4544 HCD1  LEU    318   -9.36063  -8.06637  18.20792 H_     1 0  0.09000
   ATOM    4545 HCD1  LEU    318   -7.95865  -9.12417  18.39911 H_     1 0  0.09000
   ATOM    4546 HCD1  LEU    318   -7.94214  -7.50413  19.11073 H_     1 0  0.09000
   ATOM    4547  CD2  LEU    318   -8.24821  -8.25377  15.71317 C_3    4 0 -0.27000
   ATOM    4548 HCD2  LEU    318   -7.75464  -7.91803  14.79952 H_     1 0  0.09000
   ATOM    4549 HCD2  LEU    318   -8.10485  -9.33270  15.79871 H_     1 0  0.09000
   ATOM    4550 HCD2  LEU    318   -9.31410  -8.05348  15.62518 H_     1 0  0.09000
   ATOM    4551  N    GLY    319   -8.71770  -2.77439  16.71834 N_R    3 0 -0.47000
   ATOM    4552 HN    GLY    319   -8.69152  -2.87454  15.75858 H___A  1 0  0.31000
   ATOM    4553  CA   GLY    319   -8.48831  -1.42062  17.17743 C_3    4 0 -0.02000
   ATOM    4554 HCA   GLY    319   -9.23650  -1.16420  17.93092 H_     1 0  0.09000
   ATOM    4555 HCA   GLY    319   -8.65103  -0.75119  16.33306 H_     1 0  0.09000
   ATOM    4556  C    GLY    319   -7.14757  -1.08823  17.71950 C_R    3 0  0.51000
   ATOM    4557  O    GLY    319   -6.15276  -1.84709  17.63535 O_2    1 2 -0.51000
   ATOM    4558  OXT  GLY    319   -7.00093  -0.12662  18.51114 O_2    1 2 -0.67000
   45 FORMAT CONECT (a6,12i6)

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

saving awk value in a bash array variable

hi all i am trying to save an awk value into an array in bash: total=`awk '{sum+=$3} END {print sum}' "$count".txt"` ((count++)) the above statement is in a while loop.. $count is to keep track of file numbers (1.txt,2.txt,3.txt,etc.) i get the following error: ./lines1:... (1 Reply)
Discussion started by: npatwardhan
1 Replies

2. UNIX for Dummies Questions & Answers

saving command output to a variable

Hello, I have a shell script containing a command string in the following format: command1 | command2 | cut -c9-16 The output from this is a record number (using characters 9-16 of the original output string) e.g. ORD-1234 I wish to save this value to a variable for use in later commands... (4 Replies)
Discussion started by: philjo
4 Replies

3. Shell Programming and Scripting

Piping and assigning output to a variable in Perl

Hi All, I am trying to convert the below Csh code into Perl. But i have the following error. Can any expert help ? Error: ls: *tac: No such file or directory Csh set $ST_file = `ls -rt *$testid*st*|tail -1`; Perl my $ST_file = `ls -rt *$testid*st*|tail -1`; (10 Replies)
Discussion started by: Raynon
10 Replies

4. Shell Programming and Scripting

Assign perl output to ksh shell variable

Hello, I am writing a ksh script on an AIX system. I need to get the date and time from a file into a variable. I found the following perl script from another post on this site and modified it slightly to output the format I need: perl -e '@d=localtime ((stat(shift)));... (4 Replies)
Discussion started by: swimp
4 Replies

5. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

6. Shell Programming and Scripting

help on awk---- need to assign the output of awk to a variable

hi i want to find the size of a folder and assign it to a variable and then compare if it is greater than 1 gb. i am doin this script, but it is throwing error.... #!/bin/ksh cd . | du -s | size = awk '{print $1}' if size >= 112000 then echo size high fi ERROR : (4 Replies)
Discussion started by: Nithz
4 Replies

7. Shell Programming and Scripting

Perl help - how to assign output of perl to variable

Hi, guys, i have a script i inherited from a coworker but i'm not perl savy. The script works but i would like it to work better. I want to run this command ./ciscomgrtest.pl -r "show version" -h hosts.router and have the script goto each router in the hosts.router file and run the command... (2 Replies)
Discussion started by: whipuras
2 Replies

8. Shell Programming and Scripting

awk question : system output to awk variable.

Hi Experts, I am trying to get system output to capture inside awk , but not working: Please advise if this is possible : I am trying something like this but not working, the output is coming wrong: echo "" | awk '{d=system ("date") ; print "Current date is:" , d }' Thanks, (5 Replies)
Discussion started by: rveri
5 Replies

9. Shell Programming and Scripting

awk - saving results of external script to variable.

So, I've been playing with speeding up some analysis we do by using multiple threads of awk (actually, mawk, but code-compatible as far as I use it) on multiple CPU cores. So, I have a big data file and I have several copies of exactly the same processor script, written in mawk. I also have a... (8 Replies)
Discussion started by: treesloth
8 Replies

10. UNIX for Beginners Questions & Answers

Using variable output in awk

Hi, I am trying to use variable output in awk to append a string to a word in a line. But that is not happening. Could you please help me on this. YouTube Video Tutorial: How to Use Code Tags and Format Posts @UNIX.com The below is the code #!/bin/ksh set -x src=/users/oracle/Temp... (2 Replies)
Discussion started by: pvmanikandan
2 Replies
A2P(1)							 Perl Programmers Reference Guide						    A2P(1)

NAME
a2p - Awk to Perl translator SYNOPSIS
a2p [options] [filename] DESCRIPTION
A2p takes an awk script specified on the command line (or from standard input) and produces a comparable perl script on the standard output. OPTIONS Options include: -D<number> sets debugging flags. -F<character> tells a2p that this awk script is always invoked with this -F switch. -n<fieldlist> specifies the names of the input fields if input does not have to be split into an array. If you were translating an awk script that processes the password file, you might say: a2p -7 -nlogin.password.uid.gid.gcos.shell.home Any delimiter can be used to separate the field names. -<number> causes a2p to assume that input will always have that many fields. -o tells a2p to use old awk behavior. The only current differences are: o Old awk always has a line loop, even if there are no line actions, whereas new awk does not. o In old awk, sprintf is extremely greedy about its arguments. For example, given the statement print sprintf(some_args), extra_args; old awk considers extra_args to be arguments to "sprintf"; new awk considers them arguments to "print". "Considerations" A2p cannot do as good a job translating as a human would, but it usually does pretty well. There are some areas where you may want to examine the perl script produced and tweak it some. Here are some of them, in no particular order. There is an awk idiom of putting int() around a string expression to force numeric interpretation, even though the argument is always integer anyway. This is generally unneeded in perl, but a2p can't tell if the argument is always going to be integer, so it leaves it in. You may wish to remove it. Perl differentiates numeric comparison from string comparison. Awk has one operator for both that decides at run time which comparison to do. A2p does not try to do a complete job of awk emulation at this point. Instead it guesses which one you want. It's almost always right, but it can be spoofed. All such guesses are marked with the comment ""#???"". You should go through and check them. You might want to run at least once with the -w switch to perl, which will warn you if you use == where you should have used eq. Perl does not attempt to emulate the behavior of awk in which nonexistent array elements spring into existence simply by being referenced. If somehow you are relying on this mechanism to create null entries for a subsequent for...in, they won't be there in perl. If a2p makes a split line that assigns to a list of variables that looks like (Fld1, Fld2, Fld3...) you may want to rerun a2p using the -n option mentioned above. This will let you name the fields throughout the script. If it splits to an array instead, the script is probably referring to the number of fields somewhere. The exit statement in awk doesn't necessarily exit; it goes to the END block if there is one. Awk scripts that do contortions within the END block to bypass the block under such circumstances can be simplified by removing the conditional in the END block and just exiting directly from the perl script. Perl has two kinds of array, numerically-indexed and associative. Perl associative arrays are called "hashes". Awk arrays are usually translated to hashes, but if you happen to know that the index is always going to be numeric you could change the {...} to [...]. Iteration over a hash is done using the keys() function, but iteration over an array is NOT. You might need to modify any loop that iterates over such an array. Awk starts by assuming OFMT has the value %.6g. Perl starts by assuming its equivalent, $#, to have the value %.20g. You'll want to set $# explicitly if you use the default value of OFMT. Near the top of the line loop will be the split operation that is implicit in the awk script. There are times when you can move this down past some conditionals that test the entire record so that the split is not done as often. For aesthetic reasons you may wish to change index variables from being 1-based (awk style) to 0-based (Perl style). Be sure to change all operations the variable is involved in to match. Cute comments that say "# Here is a workaround because awk is dumb" are passed through unmodified. Awk scripts are often embedded in a shell script that pipes stuff into and out of awk. Often the shell script wrapper can be incorporated into the perl script, since perl can start up pipes into and out of itself, and can do other things that awk can't do by itself. Scripts that refer to the special variables RSTART and RLENGTH can often be simplified by referring to the variables $`, $& and $', as long as they are within the scope of the pattern match that sets them. The produced perl script may have subroutines defined to deal with awk's semantics regarding getline and print. Since a2p usually picks correctness over efficiency. it is almost always possible to rewrite such code to be more efficient by discarding the semantic sugar. For efficiency, you may wish to remove the keyword from any return statement that is the last statement executed in a subroutine. A2p catches the most common case, but doesn't analyze embedded blocks for subtler cases. ARGV[0] translates to $ARGV0, but ARGV[n] translates to $ARGV[$n-1]. A loop that tries to iterate over ARGV[0] won't find it. ENVIRONMENT
A2p uses no environment variables. AUTHOR
Larry Wall <larry@wall.org> FILES
SEE ALSO
perl The perl compiler/interpreter s2p sed to perl translator DIAGNOSTICS
BUGS
It would be possible to emulate awk's behavior in selecting string versus numeric operations at run time by inspection of the operands, but it would be gross and inefficient. Besides, a2p almost always guesses right. Storage for the awk syntax tree is currently static, and can run out. perl v5.18.2 2014-01-06 A2P(1)
All times are GMT -4. The time now is 12:31 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy