It looks for certain conditions and when found, makes some modifications to the record. Most of the files I am processing contain thousands to tens of thousands of records. This is the version that writes each line to the output file as processed, the slow version.
Code:
#!/bin/sh# file to be processed
input_file=$1
# prefix to add to firstline
output_file=$2
# sdf tag with name field
name_tag=$3
# sdf tag with substitution field
sub_tag=$4
# string to check for on line following name tag line
check_for=$5
# prefix to add to firstline
prefix=$6
# create output file
touch $output_file
# location of line to replace with modified name
replace_line=0
# value collected to build replacement name
sub_value=''
# flag to check next line
check_next=0
# flag to do replacement
replace=0
# flag to indicating saving of next line for sub name
save_next=0
# initalize line counter
i=0
# to preserve spaces
IFS=""
# read file by lines
while read line
do
# store line in array
line_array[$i]="$line"
# increment counter
i=$((i+1))
# if check next was set to 1 above, the next line is the one that needs to be evaluated
if [[ $check_next == "1" ]]; then
# reset check next, do this here so we reset even if the next line is not a match
check_next=0
# check for check_for as part of line
if [[ $line =~ .*$check_for.* ]]; then
# save line number
replace_line=$i
# set flag to do replacement of name
replace=1
fi
fi
# find name tag line and check if value on next line includes check_for string
# check for name_tag as part of line
if [[ $line =~ .*$name_tag.* ]]; then
# set flag to check next line
check_next=1
fi
# save the value in the line after sub tag has been found
# this must come before save_next is set
if [[ $save_next == "1" ]]; then
# save the value from this line to use for substitute name
sub_value=$line
# reset flag
save_next=0
fi
# look for the line with the sub tag
if [[ $line =~ .*$sub_tag.* ]]; then
# set flag to save next line
save_next=1
fi
# when we get to the end of the record
if [[ $line == '$$$$' ]]; then
# if replace has been set, make replacements
if [[ $replace== "1" ]]; then
# create new first line value from stored substitute value
new_firstline=$prefix'PubChem_CID_'$sub_value
# create new name value from stored substitute value
new_name='PubChem_CID_'$sub_value
# decrement replace line value by one
replace_line=$(($replace_line-1))
# decrement line counter value by one
i=$(($i-1))
# loop through stored file
for ((j=0; j <= $i ; j++)) ; do
# for the first line, add the new firstline value
if [[ $j == "0" ]]; then
echo $new_firstline >> $output_file
# when the replace line is found, use the substitute value
elif [[ $j == "$replace_line" ]]; then
echo $new_name >> $output_file
# output all other lines as normal
else
echo ${line_array[$j]} >> $output_file
fi
done
# if replace is not set, output unmodified record
else
for ((j=0; j < $i ; j++)) ; do
echo ${line_array[$j]} >> $output_file
done
fi
# reset for next record
# line array
unset line_array
# line counter
i=0
# location of line to replace with modified name
replace_line=0
# value collected to build replacement name
sub_value=''
# flag to check next line
check_next=0
# flag to do replacement
replace=0
# flag to indicating saving of next line for sub name
save_next=0
fi
done < $input_file
This is an example of input with one record that meets the conditions to be changed,
I was trying to dump the lines of the file to a new array with the code I first posted, but that didn't work.
In short, when the value on the line after <CompoundName> contains InChI=, the name value is too long for some of the tools in the chain. I address this by making a new name from the value read from the line following <Identifier> and re-write the record using the substitution name in the required places. If the line following <CompoundName> does not contain InChI=, then the record is written unmodified.
This is what the properly modified version of the record would look like,
why do inode indices starts from 1 unlike array indexes which starts from 0
its a question from "the design of unix operating system" of maurice j bach
id be glad if i get to know the answer quickly
:) (0 Replies)
brothers why inode index starts from 1 unlike array inex which starts from 0
its a question from the design of unix operating system of maurice j.bach
i need to know the answer urgently...someone help please (1 Reply)
I come across the problems when assigning the array in the script below . How to use the array with the 'string index' correctly ? When I assign a new string index , the array elements that are previously assigned are all changed .:eek::eek::eek:
$ array=211
$ echo ${array}
211
$... (4 Replies)
hi folks
i am facing problom while trying to access sql variable as array index ina unix shell script....script goes as below..
#!/bin/ksh
MAX=3
for elem in alpha beeta gaama
do
arr=$elem
((x=x+1))
Done
SQL_SERVER='servername'
/apps/sun5/utils/sqsh -S $SQL_SERVER -U user -P pwd -b -h... (1 Reply)
Hi,
I'm just trying to use a dynamic index for some array elements that I'm accessing within a loop. Specifically, I want to access an array at variable position $counter and then also at location $counter + 1 and $counter + 2 (the second and third array positions after it) but I keep getting... (0 Replies)
Hi,
I am using KSH shell to do some programming.
I want to search array and print index value of the array.
Example..
nodeval4workflow="DESCRIPTION ="" ISENABLED ="YES" ISVALID ="YES" NAME="TESTVALIDATION"
set -A strwfVar $nodeval4workflow
strwfVar=DESCRIPTION=""... (1 Reply)
Hi,
I am new to perl and I have the following query please help here.
I have following array variables declaration
@pld1 = qw(00 01 02 03 04 05);
@pld2 = qw(10 11 12 13 14 15);
for(my $k=1;$k<=2;$k++)
{
//I want here to use @pld1 if $k is 1
// and @pld2 if $k is 2. How to do... (3 Replies)
I am trying to reformat the table by filling any missing rows. The final table will have consecutive IDs in the first column. My problem is the index of the associate array in the awk script.
infile:
S01 36407 53706 88540
S02 69343 87098 87316
S03 50133 59721 107923... (4 Replies)
I am trying to assign indexes to an associative array in a for loop but I have to use an eval command to make it work, this doesn't seem correct I don't have to do this with regular arrays
For example, the following assignment fails without the eval command:
#! /bin/bash
read -d "\0" -a... (19 Replies)