Chemist Needs Help


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Chemist Needs Help
# 36  
Old 10-28-2008
I think you probably have Bourne shell - lets' try this (changes in red)
Code:
#!/bin/sh
# FILE: chem.awk  requires execute permission
# usage: ./chem.awk infile infile2 infile3 outfile

# where parms are:
#   $1 = infile1  $2 = infile2   $3 = infile3  $4 = outfile
# do not forget to change "comment" to something menaingful if needed
echo "
parameters
$1
$2
$3
$4"


# start the FILE
printf "%s\n%s\n%s\n%s\n" "comment" "x y x" "101" "4 7" > ./newfile

# read in two constants
awk '/Done/ {print $5; exit}' "$1" | read col3
awk '/Done/ {print $5; exit}' "$2" | read col4
export col3
export col4

# loop thru the big input FILE writing 101 rows
awk '/Done/ {print $5}' "$3" | \
awk -v col4="$col4" -v col3="$col3" ' BEGIN { cnt =1.5 }
              {printf("%.1f %s %s %s\n", cnt, $0, col3, col4) 
              cnt+=.1 }'>> ./newfile
# rename the output FILE to what was required
mv ./newfile "$4"            
echo "lines in outputfile: $(wc -l "$4") created"
exit 0
# EOF: chem.awk

# 37  
Old 10-28-2008
That didn't work either but I did try to run a simple 'read' command and it wont work. Maybe thats the issue.

LEnny
# 38  
Old 10-28-2008
awk '/Done/ {print $5; exit}' "$1" | read col3

That will only work in a korn shell. Other shells will not see the new value of col3 because "read col3" is run in a subshell.
# 39  
Old 10-28-2008
This runs in a POSIX-compliant Bourne shell. Your shell obviously is not POSIX....
One more change using backticks instead of read.

Code:
#!/bin/sh
# FILE: chem.awk  requires execute permission
# usage: ./chem.awk infile infile2 infile3 outfile

# where parms are:
#   $1 = infile1  $2 = infile2   $3 = infile3  $4 = outfile
# do not forget to change "comment" to something meaningful if needed
echo "
parameters
$1
$2
$3
$4"


# start the FILE
printf "%s\n%s\n%s\n%s\n" "comment" "x y x" "101" "4 7" > ./newfile

# read in two constants
col3=`awk '/Done/ {print $5; exit}' "$1"`
col4=`awk '/Done/ {print $5; exit}' "$2"`
export col3
export col4
# loop thru the big input FILE writing 101 rows
awk '/Done/ {print $5}' "$3" | \
awk -v col4="$col4" -v col3="$col3" ' BEGIN { cnt =1.5 }
              {printf("%.1f %s %s %s\n", cnt, $0, col3, col4) 
              cnt+=.1 }'>> ./newfile
# rename the output FILE to what was required
mv ./newfile "$4"            
echo "lines in outputfile: $(wc -l "$4") created"
exit 0
# EOF: chem.awk

# 40  
Old 10-28-2008
Perderabo - I think he has non-POSIX /bin/sh. Reading on the otherside of a pipe seems to mean the variable is created and remains in the child process only. The code is fine in my Bourne - from 2004 HPUX 11.23 sh-POSIX
# 41  
Old 10-28-2008
You guys are geniuses, thanks for all the help. it seems to work great. Thanks again, Jim.

Lenny
 
Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Chemist Needs Help part II

Hello friends, I was wondering if you can help me with probably a simple function to you all: the sample looks and has this format. I was wondering how I could extract the first and second column starting including the line 'E/N and Ko' and not stop until there are no more lines. Thank you for... (5 Replies)
Discussion started by: gingburg
5 Replies
Login or Register to Ask a Question